Skip to content

Commit 49f2956

Browse files
test: added
1 parent f7bf956 commit 49f2956

3 files changed

Lines changed: 211 additions & 1 deletion

File tree

packages/create-webpack-app/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"exclude": ["src/utils/__tests__"],
43
"compilerOptions": {
54
"outDir": "lib",
65
"rootDir": "src"

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

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,170 @@ const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
363363
const isProduction = process.env.NODE_ENV === 'production';
364364

365365

366+
/** @type {import("webpack").Configuration} */
367+
const config = {
368+
entry: './src/index.js',
369+
output: {
370+
path: path.resolve(__dirname, 'dist'),
371+
},
372+
devServer: {
373+
open: true,
374+
host: 'localhost',
375+
},
376+
plugins: [
377+
new HtmlWebpackPlugin({
378+
template: 'index.html',
379+
}),
380+
381+
// Add your plugins here
382+
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
383+
],
384+
module: {
385+
rules: [
386+
{
387+
test: /\\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
388+
type: 'asset',
389+
},
390+
391+
{
392+
test: /\\.html$/i,
393+
use: ['html-loader'],
394+
},
395+
396+
// Add your rules for custom modules here
397+
// Learn more about loaders from https://webpack.js.org/loaders/
398+
],
399+
},
400+
};
401+
402+
module.exports = () => {
403+
if (isProduction) {
404+
config.mode = 'production';
405+
406+
407+
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
408+
409+
} else {
410+
config.mode = 'development';
411+
}
412+
return config;
413+
};
414+
"
415+
`;
416+
417+
exports[`create-webpack-app cli should generate default project when nothing is passed and handle conflicts 1`] = `
418+
{
419+
"description": "My webpack project",
420+
"devDependencies": {
421+
"html-loader": "x.x.x",
422+
"html-webpack-plugin": "x.x.x",
423+
"webpack": "x.x.x",
424+
"webpack-cli": "x.x.x",
425+
"webpack-dev-server": "x.x.x",
426+
"workbox-webpack-plugin": "x.x.x",
427+
},
428+
"name": "webpack-project",
429+
"scripts": {
430+
"build": "webpack --mode=production --config-node-env=production",
431+
"build:dev": "webpack --mode=development",
432+
"serve": "webpack serve",
433+
"watch": "webpack --watch",
434+
},
435+
"version": "1.0.0",
436+
}
437+
`;
438+
439+
exports[`create-webpack-app cli should generate default project when nothing is passed and handle conflicts 2`] = `
440+
"// Generated using webpack-cli https://github.com/webpack/webpack-cli
441+
442+
const path = require('path');
443+
const HtmlWebpackPlugin = require('html-webpack-plugin');
444+
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
445+
446+
const isProduction = process.env.NODE_ENV === 'production';
447+
448+
449+
/** @type {import("webpack").Configuration} */
450+
const config = {
451+
entry: './src/index.js',
452+
output: {
453+
path: path.resolve(__dirname, 'dist'),
454+
},
455+
devServer: {
456+
open: true,
457+
host: 'localhost',
458+
},
459+
plugins: [
460+
new HtmlWebpackPlugin({
461+
template: 'index.html',
462+
}),
463+
464+
// Add your plugins here
465+
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
466+
],
467+
module: {
468+
rules: [
469+
{
470+
test: /\\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
471+
type: 'asset',
472+
},
473+
474+
{
475+
test: /\\.html$/i,
476+
use: ['html-loader'],
477+
},
478+
479+
// Add your rules for custom modules here
480+
// Learn more about loaders from https://webpack.js.org/loaders/
481+
],
482+
},
483+
};
484+
485+
module.exports = () => {
486+
if (isProduction) {
487+
config.mode = 'production';
488+
489+
490+
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
491+
492+
} else {
493+
config.mode = 'development';
494+
}
495+
return config;
496+
};
497+
"
498+
`;
499+
500+
exports[`create-webpack-app cli should generate default project when nothing is passed and handle conflicts 3`] = `
501+
{
502+
"description": "My webpack project",
503+
"devDependencies": {
504+
"@babel/core": "x.x.x",
505+
"@babel/preset-env": "x.x.x",
506+
"babel-loader": "x.x.x",
507+
"webpack": "x.x.x",
508+
"webpack-cli": "x.x.x",
509+
},
510+
"name": "webpack-project",
511+
"scripts": {
512+
"build": "webpack --mode=production --config-node-env=production",
513+
"build:dev": "webpack --mode=development",
514+
"watch": "webpack --watch",
515+
},
516+
"version": "1.0.0",
517+
}
518+
`;
519+
520+
exports[`create-webpack-app cli should generate default project when nothing is passed and handle conflicts 4`] = `
521+
"// Generated using webpack-cli https://github.com/webpack/webpack-cli
522+
523+
const path = require('path');
524+
const HtmlWebpackPlugin = require('html-webpack-plugin');
525+
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
526+
527+
const isProduction = process.env.NODE_ENV === 'production';
528+
529+
366530
/** @type {import("webpack").Configuration} */
367531
const config = {
368532
entry: './src/index.js',

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,53 @@ describe("create-webpack-app cli", () => {
117117
expect(readFromWebpackConfig(dir)).toMatchSnapshot();
118118
});
119119

120+
it("should generate default project when nothing is passed and handle conflicts", async () => {
121+
const { stdout } = await run(dir, ["init", "--force"]);
122+
123+
expect(stdout).toContain("Project has been initialised with webpack!");
124+
expect(stdout).toContain("webpack.config.js");
125+
// Test files
126+
for (const file of defaultTemplateFiles) {
127+
expect(existsSync(resolve(dir, file))).toBeTruthy();
128+
}
129+
130+
// Check if the generated package.json file content matches the snapshot
131+
expect(readFromPkgJSON(dir)).toMatchSnapshot();
132+
133+
// Check if the generated webpack configuration matches the snapshot
134+
expect(readFromWebpackConfig(dir)).toMatchSnapshot();
135+
136+
const { stdout: nextStdout } = await runPromptWithAnswers(
137+
dir,
138+
["init"],
139+
[
140+
`${DOWN}${ENTER}`,
141+
`n${ENTER}`,
142+
`n${ENTER}`,
143+
`n${ENTER}`,
144+
`${UP}${ENTER}`,
145+
ENTER,
146+
// test for conflicts
147+
`y${ENTER}`,
148+
`n${ENTER}`,
149+
`a${ENTER}`,
150+
],
151+
);
152+
153+
expect(nextStdout).toContain("Project has been initialised with webpack!");
154+
expect(nextStdout).toContain("webpack.config.js");
155+
156+
for (const file of defaultTemplateFiles) {
157+
expect(existsSync(resolve(dir, file))).toBeTruthy();
158+
}
159+
160+
// Check if the generated package.json file content matches the snapshot
161+
expect(readFromPkgJSON(dir)).toMatchSnapshot();
162+
163+
// Check if the generated webpack configuration matches the snapshot
164+
expect(readFromWebpackConfig(dir)).toMatchSnapshot();
165+
});
166+
120167
it("should generate project when generationPath is supplied", async () => {
121168
const { stdout } = await run(__dirname, ["init", dir, "--force"]);
122169

0 commit comments

Comments
 (0)