Skip to content

Commit f999b2f

Browse files
test: fix
1 parent 7d8371a commit f999b2f

File tree

3 files changed

+152
-19
lines changed

3 files changed

+152
-19
lines changed

test/create-webpack-app/init/__snapshots__/init.test.js.snap.webpack5

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,117 @@ module.exports = () => {
976976
"
977977
`;
978978

979+
exports[`create-webpack-app cli should generate template with --force 1`] = `
980+
{
981+
"description": "My webpack project",
982+
"devDependencies": {
983+
"@babel/core": "x.x.x",
984+
"@babel/preset-env": "x.x.x",
985+
"@babel/preset-react": "x.x.x",
986+
"@types/react-router-dom": "x.x.x",
987+
"babel-loader": "x.x.x",
988+
"css-loader": "x.x.x",
989+
"html-webpack-plugin": "x.x.x",
990+
"react": "x.x.x",
991+
"react-dom": "x.x.x",
992+
"react-router-dom": "x.x.x",
993+
"style-loader": "x.x.x",
994+
"webpack": "x.x.x",
995+
"webpack-cli": "x.x.x",
996+
"webpack-dev-server": "x.x.x",
997+
"workbox-webpack-plugin": "x.x.x",
998+
},
999+
"name": "my-webpack-project",
1000+
"scripts": {
1001+
"build": "webpack --mode=production --node-env=production",
1002+
"build:dev": "webpack --mode=development",
1003+
"serve": "webpack serve",
1004+
"watch": "webpack --watch",
1005+
},
1006+
"version": "1.0.0",
1007+
}
1008+
`;
1009+
1010+
exports[`create-webpack-app cli should generate template with --force 2`] = `
1011+
"// Generated using webpack-cli https://github.com/webpack/webpack-cli
1012+
1013+
const path = require('path');
1014+
const HtmlWebpackPlugin = require('html-webpack-plugin');
1015+
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
1016+
1017+
const isProduction = process.env.NODE_ENV === 'production';
1018+
1019+
1020+
const stylesHandler = 'style-loader';
1021+
1022+
1023+
1024+
/** @type {import("webpack").Configuration} */
1025+
const config = {
1026+
entry: './src/index.jsx',
1027+
output: {
1028+
path: path.resolve(__dirname, 'dist'),
1029+
},
1030+
devServer: {
1031+
open: true,
1032+
host: 'localhost',
1033+
},
1034+
plugins: [
1035+
new HtmlWebpackPlugin({
1036+
template: 'index.html',
1037+
}),
1038+
1039+
// Add your plugins here
1040+
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
1041+
],
1042+
module: {
1043+
rules: [
1044+
{
1045+
test: /\\.(js|jsx)$/,
1046+
exclude: /node_modules/,
1047+
use: {
1048+
loader: "babel-loader",
1049+
options: {
1050+
presets: ["@babel/preset-env", "@babel/preset-react"],
1051+
},
1052+
},
1053+
},
1054+
{
1055+
test: /\\.css$/i,
1056+
use: [stylesHandler,'css-loader'],
1057+
},
1058+
{
1059+
test: /\\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
1060+
type: 'asset',
1061+
},
1062+
1063+
// Add your rules for custom modules here
1064+
// Learn more about loaders from https://webpack.js.org/loaders/
1065+
],
1066+
},
1067+
resolve: {
1068+
alias: {
1069+
"@": path.resolve(__dirname, "./src/"),
1070+
},
1071+
extensions: ['.jsx', '.js'],
1072+
},
1073+
};
1074+
1075+
module.exports = () => {
1076+
if (isProduction) {
1077+
config.mode = 'production';
1078+
1079+
1080+
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
1081+
1082+
} else {
1083+
config.mode = 'development';
1084+
}
1085+
return config;
1086+
};
1087+
"
1088+
`;
1089+
9791090
exports[`create-webpack-app cli should generate typescript project correctly 1`] = `
9801091
{
9811092
"description": "My webpack project",

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe("create-webpack-app cli", () => {
133133
const { stdout, stderr } = await runPromptWithAnswers(
134134
assetsPath,
135135
["init", "--force", "--template=apple"],
136-
[`${ENTER}`],
136+
[ENTER],
137137
);
138138

139139
expect(stdout).toContain("Project has been initialised with webpack!");
@@ -154,7 +154,7 @@ describe("create-webpack-app cli", () => {
154154
const { stdout } = await runPromptWithAnswers(
155155
assetsPath,
156156
["init", "."],
157-
[`${DOWN}${DOWN}${ENTER}`, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, `${UP}${ENTER}`, ENTER],
157+
[DOWN, DOWN, ENTER, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, UP, ENTER, ENTER],
158158
);
159159

160160
expect(stdout).toContain("Project has been initialised with webpack!");
@@ -184,7 +184,7 @@ describe("create-webpack-app cli", () => {
184184
const { stdout } = await runPromptWithAnswers(
185185
assetsPath,
186186
["init", "."],
187-
[`${DOWN}${ENTER}`, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, `${UP}${ENTER}`, ENTER],
187+
[DOWN, ENTER, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, UP, ENTER, ENTER],
188188
);
189189

190190
expect(stdout).toContain("Project has been initialised with webpack!");
@@ -211,11 +211,12 @@ describe("create-webpack-app cli", () => {
211211
assetsPath,
212212
["init", "."],
213213
[
214-
`${ENTER}`,
214+
ENTER,
215215
`n${ENTER}`,
216216
`n${ENTER}`,
217217
`n${ENTER}`,
218-
`${DOWN}${ENTER}`,
218+
DOWN,
219+
ENTER,
219220
`n${ENTER}`,
220221
`n${ENTER}`,
221222
`n${ENTER}`,
@@ -244,11 +245,12 @@ describe("create-webpack-app cli", () => {
244245
assetsPath,
245246
["init", "."],
246247
[
247-
`${ENTER}`,
248+
ENTER,
248249
`n${ENTER}`,
249250
`n${ENTER}`,
250251
`n${ENTER}`,
251-
`${DOWN}${ENTER}`,
252+
DOWN,
253+
ENTER,
252254
`n${ENTER}`,
253255
`y${ENTER}`,
254256
`n${ENTER}`,
@@ -283,7 +285,8 @@ describe("create-webpack-app cli", () => {
283285
`n${ENTER}`,
284286
`n${ENTER}`,
285287
`n${ENTER}`,
286-
`${DOWN}${ENTER}`,
288+
DOWN,
289+
ENTER,
287290
`n${ENTER}`,
288291
`n${ENTER}`,
289292
`y${ENTER}`,
@@ -316,7 +319,8 @@ describe("create-webpack-app cli", () => {
316319
`n${ENTER}`,
317320
`n${ENTER}`,
318321
`n${ENTER}`,
319-
`${DOWN}${ENTER}`,
322+
DOWN,
323+
ENTER,
320324
`y${ENTER}`,
321325
`y${ENTER}`,
322326
`n${ENTER}`,
@@ -347,11 +351,13 @@ describe("create-webpack-app cli", () => {
347351
assetsPath,
348352
["init", "."],
349353
[
350-
`${ENTER}`,
354+
ENTER,
351355
`n${ENTER}`,
352356
`n${ENTER}`,
353357
`n${ENTER}`,
354-
`${DOWN}${DOWN}${ENTER}`,
358+
DOWN,
359+
DOWN,
360+
ENTER,
355361
`n${ENTER}`,
356362
`n${ENTER}`,
357363
`n${ENTER}`,
@@ -380,11 +386,14 @@ describe("create-webpack-app cli", () => {
380386
assetsPath,
381387
["init", "."],
382388
[
383-
`${ENTER}`,
389+
ENTER,
384390
`n${ENTER}`,
385391
`n${ENTER}`,
386392
`n${ENTER}`,
387-
`${DOWN}${DOWN}${DOWN}${ENTER}`,
393+
DOWN,
394+
DOWN,
395+
DOWN,
396+
ENTER,
388397
`n${ENTER}`,
389398
`n${ENTER}`,
390399
`n${ENTER}`,
@@ -412,7 +421,7 @@ describe("create-webpack-app cli", () => {
412421
const { stdout } = await runPromptWithAnswers(
413422
assetsPath,
414423
["init", "."],
415-
[ENTER, ENTER, `n${ENTER}`, `n${ENTER}`, `${UP}${ENTER}`, ENTER],
424+
[ENTER, ENTER, `n${ENTER}`, `n${ENTER}`, UP, ENTER, ENTER],
416425
);
417426

418427
expect(stdout).toContain("Would you like to use Webpack Dev server?");
@@ -461,7 +470,7 @@ describe("create-webpack-app cli", () => {
461470
const { stdout } = await runPromptWithAnswers(
462471
assetsPath,
463472
["init", "."],
464-
[ENTER, `n${ENTER}`, ENTER, `n${ENTER}`, `${UP}${ENTER}`, ENTER],
473+
[ENTER, `n${ENTER}`, ENTER, `n${ENTER}`, UP, ENTER, ENTER],
465474
);
466475

467476
expect(stdout).toContain("Do you want to simplify the creation of HTML files for your bundle?");
@@ -646,7 +655,7 @@ describe("create-webpack-app cli", () => {
646655
const { stdout } = await runPromptWithAnswers(
647656
assetsPath,
648657
["init", ".", "--template=react"],
649-
[ENTER, `y${ENTER}`, `y${ENTER}`, `${ENTER}`, `y${ENTER}`, ENTER, ENTER],
658+
[ENTER, `y${ENTER}`, `y${ENTER}`, ENTER, `y${ENTER}`, ENTER, ENTER, ENTER],
650659
);
651660

652661
expect(stdout).toContain("Project has been initialised with webpack!");
@@ -694,7 +703,7 @@ describe("create-webpack-app cli", () => {
694703
const { stdout } = await runPromptWithAnswers(
695704
assetsPath,
696705
["init", ".", "--template=svelte"],
697-
[ENTER, `y${ENTER}`, `${ENTER}`, `y${ENTER}`, ENTER, ENTER],
706+
[ENTER, `y${ENTER}`, ENTER, `y${ENTER}`, ENTER, ENTER],
698707
);
699708

700709
expect(stdout).toContain("Project has been initialised with webpack!");

test/utils/test-utils.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,20 @@ const runPromptWithAnswers = async (cwd, args, answers = [], options = {}) => {
164164
}
165165

166166
if (currentAnswer < answers.length) {
167-
proc.stdin.write(answers[currentAnswer]);
167+
const answer = answers[currentAnswer];
168+
169+
if (!answer.includes("\u000D")) {
170+
waitAnswer = true;
171+
}
172+
173+
// console.log(output, answer);
174+
proc.stdin.write(answer);
168175
currentAnswer++;
169-
waitAnswer = true;
170176
}
171177
};
172178

179+
proc.stderr.pipe(process.stdout);
180+
173181
proc.stdout.pipe(
174182
new Writable({
175183
write(chunk, encoding, callback) {
@@ -183,9 +191,14 @@ const runPromptWithAnswers = async (cwd, args, answers = [], options = {}) => {
183191
const text = stripVTControlCharacters(output).trim();
184192

185193
if (text.length > 0 && waitAnswer) {
194+
console.log(output);
186195
waitAnswer = false;
187196
writeAnswer(output);
188197
}
198+
// After each write into stdin, stdout outputs question and answer, after this we will wait the new answer
199+
else if (text.length > 0 && !waitAnswer) {
200+
waitAnswer = true;
201+
}
189202

190203
callback();
191204
},

0 commit comments

Comments
 (0)