-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathcreate-waku.spec.ts
111 lines (106 loc) · 3.35 KB
/
create-waku.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import crypto from 'node:crypto';
import {
mkdir,
rmdir,
readdir,
cp,
readFile,
writeFile,
} from 'node:fs/promises';
import { expect } from '@playwright/test';
import { test, debugChildProcess, terminate } from './utils.js';
test('should create waku with default setup work', async () => {
const cliPath = fileURLToPath(
new URL('../packages/create-waku/dist/index.js', import.meta.url),
);
const dirname = crypto.randomUUID();
await mkdir(new URL(`./.cache/${dirname}/`, import.meta.url), {
recursive: true,
});
const cwd = fileURLToPath(new URL(`./.cache/${dirname}`, import.meta.url));
const childProcess = spawn(process.execPath, [cliPath], {
cwd,
env: process.env,
});
debugChildProcess(childProcess, fileURLToPath(import.meta.url));
const writeNewLine = async () =>
new Promise<void>((resolve) =>
childProcess.stdin.write('\n', () => resolve()),
); // will use default
for await (const data of childProcess.stdout) {
const str = data.toString();
if (str.includes('Project Name')) {
await writeNewLine();
} else if (str.includes('Choose a starter template')) {
await writeNewLine();
}
}
const paths = await readdir(cwd);
expect(paths[0]).toBe('waku-project');
expect(paths.length).toBe(1);
const files = await readdir(
new URL(`./.cache/${dirname}/waku-project`, import.meta.url),
);
expect(files).toContain('package.json');
expect(files).toContain('src');
expect(files).toContain('tsconfig.json');
try {
await terminate(childProcess.pid!);
} catch {
// ignore
}
await rmdir(cwd, { recursive: true });
});
test('should create waku with update notify work', async () => {
const oldCliDir = fileURLToPath(
new URL('../packages/create-waku/', import.meta.url),
);
const dirname = crypto.randomUUID();
const newCliDir = new URL(
`./.cache/${dirname}/create-waku/`,
import.meta.url,
);
await cp(oldCliDir, newCliDir, {
recursive: true,
});
const packageJsonPath = new URL('./package.json', newCliDir);
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8'));
packageJson.version = '0.0.1';
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
const cliPath = fileURLToPath(new URL('./dist/index.js', newCliDir));
await mkdir(new URL(`./.cache/${dirname}/`, import.meta.url), {
recursive: true,
});
const cwd = fileURLToPath(new URL(`./.cache/${dirname}`, import.meta.url));
const childProcess = spawn(process.execPath, [cliPath], {
cwd,
env: process.env,
});
debugChildProcess(childProcess, fileURLToPath(import.meta.url));
const writeNewLine = async () =>
new Promise<void>((resolve) =>
childProcess.stdin.write('\n', () => resolve()),
); // will use default
let found = false;
for await (const data of childProcess.stdout) {
const str = data.toString();
if (str.includes('Project Name')) {
await writeNewLine();
} else if (str.includes('Choose a starter template')) {
await writeNewLine();
}
if (str.includes(`A new version of 'create-waku' is available!`)) {
found = true;
break;
}
}
expect(found).toBe(true);
try {
await terminate(childProcess.pid!);
} catch {
// ignore
}
await rmdir(cwd, { recursive: true });
});