Skip to content

Commit de2196d

Browse files
test: fix
1 parent 620b5b1 commit de2196d

File tree

5 files changed

+70
-118
lines changed

5 files changed

+70
-118
lines changed

smoketests/helpers.js

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,66 +21,63 @@ const swapPkgName = (current, isSubPackage = false) => {
2121

2222
const CLI_ENTRY_PATH = path.resolve(ROOT_PATH, "./packages/webpack-cli/bin/cli.js");
2323

24-
const runTest = (pkg, cliArgs = [], logMessage = undefined, isSubPackage = false) => {
24+
const runTest = async (pkg, cliArgs = [], logMessage = undefined, isSubPackage = false) => {
2525
// Simulate package missing
2626
swapPkgName(pkg, isSubPackage);
2727

28-
return Promise.resolve()
29-
.then(() => import("execa"))
30-
.then(({ execa }) =>
31-
execa(CLI_ENTRY_PATH, cliArgs, {
32-
cwd: __dirname,
33-
}),
34-
)
35-
.then((proc) => {
36-
proc.stdin.setDefaultEncoding("utf8");
28+
const { execa } = await import("execa");
29+
const proc = execa(CLI_ENTRY_PATH, cliArgs, {
30+
cwd: __dirname,
31+
});
3732

38-
proc.stdout.on("data", (chunk) => {
39-
console.log(` stdout: ${chunk.toString()}`);
40-
});
41-
return new Promise((resolve) => {
42-
const timeout = setTimeout(() => {
43-
console.log(" timeout: killing process");
44-
proc.kill();
45-
}, 60000);
33+
proc.stdin.setDefaultEncoding("utf8");
4634

47-
const prompt = "Would you like to install";
48-
let hasLogMessage = false;
49-
let hasPrompt = false;
50-
let hasPassed = false;
35+
proc.stdout.on("data", (chunk) => {
36+
console.log(` stdout: ${chunk.toString()}`);
37+
});
5138

52-
proc.stderr.on("data", (chunk) => {
53-
const data = stripVTControlCharacters(chunk.toString());
39+
return new Promise((resolve) => {
40+
const timeout = setTimeout(() => {
41+
console.log(" timeout: killing process");
42+
proc.kill();
43+
}, 60000);
5444

55-
console.log(` stderr: ${data}`);
45+
const prompt = "Would you like to install";
46+
let hasLogMessage = false;
47+
let hasPrompt = false;
48+
let hasPassed = false;
5649

57-
if (data.includes(logMessage)) {
58-
hasLogMessage = true;
59-
}
50+
proc.stderr.on("data", (chunk) => {
51+
const data = stripVTControlCharacters(chunk.toString());
6052

61-
if (data.includes(prompt)) {
62-
hasPrompt = true;
63-
}
53+
console.log(` stderr: ${data}`);
6454

65-
if (hasLogMessage && hasPrompt) {
66-
hasPassed = true;
67-
proc.kill();
68-
}
69-
});
55+
if (data.includes(logMessage)) {
56+
hasLogMessage = true;
57+
}
7058

71-
proc.on("exit", () => {
72-
swapPkgName(`.${pkg}`, isSubPackage);
73-
clearTimeout(timeout);
74-
resolve(hasPassed);
75-
});
59+
if (data.includes(prompt)) {
60+
hasPrompt = true;
61+
}
7662

77-
proc.on("error", () => {
78-
swapPkgName(`.${pkg}`, isSubPackage);
79-
clearTimeout(timeout);
80-
resolve(false);
81-
});
82-
});
63+
if (hasLogMessage && hasPrompt) {
64+
hasPassed = true;
65+
proc.kill();
66+
}
67+
});
68+
69+
proc.on("exit", () => {
70+
swapPkgName(`.${pkg}`, isSubPackage);
71+
clearTimeout(timeout);
72+
resolve(hasPassed);
73+
});
74+
75+
proc.on("error", () => {
76+
swapPkgName(`.${pkg}`, isSubPackage);
77+
clearTimeout(timeout);
78+
resolve(false);
8379
});
80+
});
8481
};
8582

8683
const runTestStdout = ({ packageName, cliArgs, logMessage, isSubPackage } = {}) => {

test/create-webpack-app/init/init.test.js

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -664,44 +664,6 @@ describe("create-webpack-app cli", () => {
664664
expect(readFromWebpackConfig(assetsPath)).toMatchSnapshot();
665665
});
666666

667-
it("should generate react template with --force", async () => {
668-
const assetsPath = await uniqueDirectoryForTest();
669-
const { stdout } = await run(assetsPath, ["init", "--template=react", "--force"]);
670-
671-
expect(stdout).toContain("Project has been initialised with webpack!");
672-
expect(stdout).toContain("webpack.config.js");
673-
674-
// Test files
675-
for (const file of reactTemplateFiles) {
676-
expect(existsSync(resolve(assetsPath, file))).toBeTruthy();
677-
}
678-
679-
// Check if the generated package.json file content matches the snapshot
680-
expect(readFromPkgJSON(assetsPath)).toMatchSnapshot();
681-
682-
// Check if the generated webpack configuration matches the snapshot
683-
expect(readFromWebpackConfig(assetsPath)).toMatchSnapshot();
684-
});
685-
686-
it("should generate vue template with --force", async () => {
687-
const assetsPath = await uniqueDirectoryForTest();
688-
const { stdout } = await run(assetsPath, ["init", "--template=vue", "--force"]);
689-
690-
expect(stdout).toContain("Project has been initialised with webpack!");
691-
expect(stdout).toContain("webpack.config.js");
692-
693-
// Test files
694-
for (const file of vueTemplateFiles) {
695-
expect(existsSync(resolve(assetsPath, file))).toBeTruthy();
696-
}
697-
698-
// Check if the generated package.json file content matches the snapshot
699-
expect(readFromPkgJSON(assetsPath)).toMatchSnapshot();
700-
701-
// Check if the generated webpack configuration matches the snapshot
702-
expect(readFromWebpackConfig(assetsPath)).toMatchSnapshot();
703-
});
704-
705667
it("should generate vue template with store and router support on prompt answers", async () => {
706668
const assetsPath = await uniqueDirectoryForTest();
707669
const { stdout } = await runPromptWithAnswers(
@@ -750,15 +712,15 @@ describe("create-webpack-app cli", () => {
750712
expect(readFromWebpackConfig(assetsPath)).toMatchSnapshot();
751713
});
752714

753-
it("should generate svelte template with --force", async () => {
715+
it("should generate template with --force", async () => {
754716
const assetsPath = await uniqueDirectoryForTest();
755-
const { stdout } = await run(assetsPath, ["init", "--template=svelte", "--force"]);
717+
const { stdout } = await run(assetsPath, ["init", "--template=react", "--force"]);
756718

757719
expect(stdout).toContain("Project has been initialised with webpack!");
758720
expect(stdout).toContain("webpack.config.js");
759721

760722
// Test files
761-
for (const file of svelteTemplateFiles) {
723+
for (const file of reactTemplateFiles) {
762724
expect(existsSync(resolve(assetsPath, file))).toBeTruthy();
763725
}
764726

test/create-webpack-app/loader/loader.test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ jest.setTimeout(480000);
88

99
const {
1010
createPathDependentUtils,
11-
normalizeStderr,
1211
normalizeStdout,
1312
uniqueDirectoryForTest,
1413
} = require("../test.utils");
@@ -37,15 +36,6 @@ const dataForTests = (rootAssetsPath) => ({
3736
});
3837

3938
describe("loader command", () => {
40-
it("should ask the loader name when invoked", async () => {
41-
const assetsPath = await uniqueDirectoryForTest();
42-
const { stdout, stderr } = await runPromptWithAnswers(assetsPath, ["loader", "."]);
43-
44-
expect(stdout).toBeTruthy();
45-
expect(normalizeStderr(stderr)).toBeFalsy();
46-
expect(normalizeStdout(stdout)).toContain(firstPrompt);
47-
});
48-
4939
it("should scaffold loader with default name if no loader name provided", async () => {
5040
const assetsPath = await uniqueDirectoryForTest();
5141
const { defaultLoaderPath, defaultTemplateFiles } = dataForTests(assetsPath);

test/create-webpack-app/plugin/plugin.test.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { existsSync, mkdirSync } = require("node:fs");
22
const { join, resolve } = require("node:path");
3-
const { normalizeStderr, normalizeStdout, uniqueDirectoryForTest } = require("../test.utils");
3+
const { normalizeStdout, uniqueDirectoryForTest } = require("../test.utils");
44
const { createPathDependentUtils } = require("../test.utils");
55

66
// eslint-disable-next-line jest/no-confusing-set-timeout
@@ -32,15 +32,6 @@ const dataForTests = (rootAssetsPath) => ({
3232
});
3333

3434
describe("plugin command", () => {
35-
it("should ask the plugin name when invoked", async () => {
36-
const assetsPath = await uniqueDirectoryForTest();
37-
const { stdout, stderr } = await runPromptWithAnswers(assetsPath, ["plugin", "."]);
38-
39-
expect(stdout).toBeTruthy();
40-
expect(normalizeStderr(stderr)).toBeFalsy();
41-
expect(normalizeStdout(stdout)).toContain(firstPrompt);
42-
});
43-
4435
it("should scaffold plugin with default name if no plugin name provided", async () => {
4536
const assetsPath = await uniqueDirectoryForTest();
4637
const { defaultPluginPath, defaultTemplateFiles } = dataForTests(assetsPath);

test/utils/test-utils.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,45 @@ const runWatch = async (cwd, args = [], options = {}) => {
144144
*/
145145
const runPromptWithAnswers = async (cwd, args, answers = [], options = {}) => {
146146
const execa = await import("execa");
147-
const process = createProcess(execa, cwd, args, options);
147+
const proc = createProcess(execa, cwd, args, {
148+
...options,
149+
// TODO remove me when node@18 will be removed from support
150+
cleanup: false,
151+
});
148152

149-
process.stdin.setDefaultEncoding("utf8");
153+
proc.stdin.setDefaultEncoding("utf8");
150154

151155
let currentAnswer = 0;
152156
let waitAnswer = true;
153157

154158
const writeAnswer = (output) => {
155159
if (answers.length === 0) {
156-
process.stdin.write(output);
157-
processKill(process);
160+
proc.stdin.write(output);
161+
processKill(proc);
158162

159163
return;
160164
}
161165

162166
if (currentAnswer < answers.length) {
163-
process.stdin.write(answers[currentAnswer]);
167+
proc.stdin.write(answers[currentAnswer]);
164168
currentAnswer++;
165169
waitAnswer = true;
166170
}
167171
};
168172

169-
process.stdout.pipe(
173+
proc.stdout.pipe(
170174
new Writable({
171175
write(chunk, encoding, callback) {
172176
const output = chunk.toString("utf8");
173177

174-
if (output && stripVTControlCharacters(output).trim().length > 0 && waitAnswer) {
178+
if (!output) {
179+
callback("No output");
180+
return;
181+
}
182+
183+
const text = stripVTControlCharacters(output).trim();
184+
185+
if (text.length > 0 && waitAnswer) {
175186
waitAnswer = false;
176187
writeAnswer(output);
177188
}
@@ -182,19 +193,20 @@ const runPromptWithAnswers = async (cwd, args, answers = [], options = {}) => {
182193
);
183194

184195
return new Promise((resolve) => {
196+
/** @type {{ stdout: string, stderr: string }} */
185197
const obj = {};
186198

187199
let stdoutDone = false;
188200
let stderrDone = false;
189201

190202
const complete = () => {
191203
if (stdoutDone && stderrDone) {
192-
processKill(process);
204+
processKill(proc);
193205
resolve(obj);
194206
}
195207
};
196208

197-
process.stdout.pipe(
209+
proc.stdout.pipe(
198210
concat((result) => {
199211
stdoutDone = true;
200212
obj.stdout = result.toString();
@@ -203,7 +215,7 @@ const runPromptWithAnswers = async (cwd, args, answers = [], options = {}) => {
203215
}),
204216
);
205217

206-
process.stderr.pipe(
218+
proc.stderr.pipe(
207219
concat((result) => {
208220
stderrDone = true;
209221
obj.stderr = result.toString();

0 commit comments

Comments
 (0)