Skip to content

Commit d11298a

Browse files
authored
test(e2e): fix DevServer test (#3096)
* refactor(test): extract ExitOnDonePlugin to helpers * test(e2e): add ExitOnDonePlugin to dev-server fixtures * test(e2e): set config paths * test(e2e): show orphaned modules in empty entry test * refactor: remove debug code
1 parent 1dde662 commit d11298a

10 files changed

+45
-43
lines changed

test/e2e/DevServer.test.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('DevServer', () => {
77
const webpack5Test = isWebpack5 ? it : it.skip;
88

99
it('should add devServer entry points to a single entry point', (done) => {
10-
testBin('--config ./test/fixtures/dev-server/default-config.js')
10+
testBin(null, './test/fixtures/dev-server/default-config.js')
1111
.then((output) => {
1212
expect(output.exitCode).toEqual(0);
1313
expect(output.stdout).toContain('client/default/index.js?');
@@ -19,9 +19,7 @@ describe('DevServer', () => {
1919
webpack5Test(
2020
'should add devServer entry points to a multi entry point object',
2121
(done) => {
22-
testBin(
23-
'--config ./test/fixtures/dev-server/multi-entry.js --stats=verbose'
24-
)
22+
testBin('--stats=verbose', './test/fixtures/dev-server/multi-entry.js')
2523
.then((output) => {
2624
expect(output.exitCode).toEqual(0);
2725
expect(output.stdout).toContain('client/default/index.js?');
@@ -35,7 +33,7 @@ describe('DevServer', () => {
3533
webpack5Test(
3634
'should add devServer entry points to an empty entry object',
3735
(done) => {
38-
testBin('--config ./test/fixtures/dev-server/empty-entry.js')
36+
testBin(null, './test/fixtures/dev-server/empty-entry.js')
3937
.then((output) => {
4038
expect(output.exitCode).toEqual(0);
4139
expect(output.stdout).toContain('client/default/index.js?');
@@ -47,7 +45,8 @@ describe('DevServer', () => {
4745

4846
webpack5Test('should supports entry as descriptor', (done) => {
4947
testBin(
50-
'--config ./test/fixtures/entry-as-descriptor/webpack.config --stats detailed'
48+
'--stats detailed',
49+
'./test/fixtures/entry-as-descriptor/webpack.config'
5150
)
5251
.then((output) => {
5352
expect(output.exitCode).toEqual(0);
@@ -58,9 +57,7 @@ describe('DevServer', () => {
5857
});
5958

6059
it('should only prepends devServer entry points to "web" target', (done) => {
61-
testBin(
62-
'--config ./test/fixtures/dev-server/default-config.js --target web'
63-
)
60+
testBin('--target web', './test/fixtures/dev-server/default-config.js')
6461
.then((output) => {
6562
expect(output.exitCode).toEqual(0);
6663
expect(output.stdout).toContain('client/default/index.js?');
@@ -71,9 +68,7 @@ describe('DevServer', () => {
7168
});
7269

7370
it('should not prepend devServer entry points to "node" target', (done) => {
74-
testBin(
75-
'--config ./test/fixtures/dev-server/default-config.js --target node'
76-
)
71+
testBin('--target node', './test/fixtures/dev-server/default-config.js')
7772
.then((output) => {
7873
expect(output.exitCode).toEqual(0);
7974
expect(output.stdout).not.toContain('client/default/index.js?');
@@ -85,7 +80,8 @@ describe('DevServer', () => {
8580

8681
it('should prepends the hot runtime to "node" target as well', (done) => {
8782
testBin(
88-
'--config ./test/fixtures/dev-server/default-config.js --target node --hot'
83+
'--target node --hot',
84+
'./test/fixtures/dev-server/default-config.js'
8985
)
9086
.then((output) => {
9187
expect(output.exitCode).toEqual(0);
@@ -96,7 +92,7 @@ describe('DevServer', () => {
9692
});
9793

9894
it('does not use client.path when default', (done) => {
99-
testBin('--config ./test/fixtures/dev-server/client-default-path-config.js')
95+
testBin(null, './test/fixtures/dev-server/client-default-path-config.js')
10096
.then((output) => {
10197
expect(output.exitCode).toEqual(0);
10298
expect(output.stdout).not.toContain('&path=/ws');
@@ -106,7 +102,7 @@ describe('DevServer', () => {
106102
});
107103

108104
it('should use client.path when custom', (done) => {
109-
testBin('--config ./test/fixtures/dev-server/client-custom-path-config.js')
105+
testBin(null, './test/fixtures/dev-server/client-custom-path-config.js')
110106
.then((output) => {
111107
expect(output.exitCode).toEqual(0);
112108
expect(output.stdout).toContain('&path=/custom/path');
@@ -118,7 +114,7 @@ describe('DevServer', () => {
118114
webpack5Test(
119115
'should prepend devServer entry points depending on targetProperties',
120116
(done) => {
121-
testBin('--config ./test/fixtures/dev-server/target-config.js')
117+
testBin(null, './test/fixtures/dev-server/target-config.js')
122118
.then((output) => {
123119
expect(output.exitCode).toEqual(0);
124120
expect(output.stdout).toContain('client/default/index.js');

test/fixtures/cli/webpack.config.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
'use strict';
22

3+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
4+
35
module.exports = {
46
mode: 'development',
57
stats: 'detailed',
68
context: __dirname,
79
entry: './foo.js',
8-
plugins: [
9-
{
10-
apply(compiler) {
11-
compiler.hooks.done.tap('webpack-dev-server', (stats) => {
12-
let exitCode = 0;
13-
if (stats.hasErrors()) {
14-
exitCode = 1;
15-
}
16-
setTimeout(() => process.exit(exitCode));
17-
});
18-
},
19-
},
20-
],
10+
plugins: [ExitOnDonePlugin],
2111
};

test/fixtures/dev-server/client-custom-path-config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { resolve } = require('path');
4+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
45

56
module.exports = {
67
mode: 'development',
@@ -15,4 +16,5 @@ module.exports = {
1516
client: 'sockjs',
1617
},
1718
},
19+
plugins: [ExitOnDonePlugin],
1820
};

test/fixtures/dev-server/client-default-path-config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { resolve } = require('path');
4+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
45

56
module.exports = {
67
mode: 'development',
@@ -15,4 +16,5 @@ module.exports = {
1516
client: 'sockjs',
1617
},
1718
},
19+
plugins: [ExitOnDonePlugin],
1820
};

test/fixtures/dev-server/default-config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { resolve } = require('path');
4+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
45

56
module.exports = {
67
mode: 'development',
@@ -15,4 +16,5 @@ module.exports = {
1516
client: 'sockjs',
1617
},
1718
},
19+
plugins: [ExitOnDonePlugin],
1820
};
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
'use strict';
22

3+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
4+
35
module.exports = {
46
mode: 'development',
5-
stats: 'detailed',
7+
stats: { orphanModules: true, preset: 'detailed' },
68
entry: {},
79
devServer: {
810
transportMode: {
911
server: 'sockjs',
1012
client: 'sockjs',
1113
},
1214
},
15+
plugins: [ExitOnDonePlugin],
1316
};

test/fixtures/dev-server/multi-entry.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { resolve } = require('path');
4+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
45

56
module.exports = {
67
mode: 'development',
@@ -16,4 +17,5 @@ module.exports = {
1617
client: 'sockjs',
1718
},
1819
},
20+
plugins: [ExitOnDonePlugin],
1921
};

test/fixtures/dev-server/target-config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { resolve } = require('path');
4+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
45

56
module.exports = {
67
mode: 'development',
@@ -21,4 +22,5 @@ module.exports = {
2122
client: 'sockjs',
2223
},
2324
},
25+
plugins: [ExitOnDonePlugin],
2426
};

test/fixtures/entry-as-descriptor/webpack.config.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const ExitOnDonePlugin = require('../../helpers/ExitOnDonePlugin');
4+
35
module.exports = {
46
mode: 'development',
57
context: __dirname,
@@ -8,19 +10,7 @@ module.exports = {
810
import: './foo.js',
911
},
1012
},
11-
plugins: [
12-
{
13-
apply(compiler) {
14-
compiler.hooks.done.tap('webpack-dev-server', (stats) => {
15-
let exitCode = 0;
16-
if (stats.hasErrors()) {
17-
exitCode = 1;
18-
}
19-
setTimeout(() => process.exit(exitCode));
20-
});
21-
},
22-
},
23-
],
13+
plugins: [ExitOnDonePlugin],
2414
infrastructureLogging: {
2515
level: 'warn',
2616
},

test/helpers/ExitOnDonePlugin.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
module.exports = {
4+
apply(compiler) {
5+
compiler.hooks.done.tap('webpack-dev-server', (stats) => {
6+
let exitCode = 0;
7+
if (stats.hasErrors()) {
8+
exitCode = 1;
9+
}
10+
setTimeout(() => process.exit(exitCode));
11+
});
12+
},
13+
};

0 commit comments

Comments
 (0)