Skip to content

Commit e6f91ad

Browse files
authored
test: add unit test for DevServerEntryPlugin (#2840)
1 parent 21bc47a commit e6f91ad

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
const isWebpack5 = require('../../helpers/isWebpack5');
4+
5+
(isWebpack5 ? describe : describe.skip)('DevServerEntryPlugin', () => {
6+
let plugin;
7+
const options = {};
8+
const entries = ['./foo.js', './bar.js'];
9+
10+
const createDependency = jest.fn();
11+
const tapPromise = jest.fn();
12+
const compiler = {
13+
hooks: {
14+
make: {
15+
tapPromise,
16+
},
17+
},
18+
};
19+
const compilation = {
20+
addEntry: jest.fn((_context, _dep, _options, cb) => cb()),
21+
};
22+
23+
beforeEach(() => {
24+
jest.setMock('webpack/lib/EntryPlugin.js', { createDependency });
25+
const DevServerEntryPlugin = require('../../../lib/utils/DevServerEntryPlugin');
26+
plugin = new DevServerEntryPlugin('context', entries, options);
27+
});
28+
29+
afterEach(() => {
30+
jest.clearAllMocks();
31+
});
32+
33+
it('should set property', () => {
34+
expect(plugin.context).toBe('context');
35+
expect(plugin.entries).toBe(entries);
36+
expect(plugin.options).toBe(options);
37+
});
38+
39+
it('should add hooks to add entries', async () => {
40+
plugin.apply(compiler);
41+
expect(tapPromise).toBeCalledTimes(1);
42+
expect(tapPromise.mock.calls[0]).toMatchSnapshot();
43+
44+
await tapPromise.mock.calls[0][1](compilation);
45+
expect(compilation.addEntry).toBeCalledTimes(entries.length);
46+
expect(compilation.addEntry.mock.calls).toMatchSnapshot();
47+
48+
expect(createDependency).toBeCalledTimes(entries.length);
49+
expect(createDependency.mock.calls).toMatchSnapshot();
50+
});
51+
52+
it('should allow modifying entries after creation', async () => {
53+
plugin.apply(compiler);
54+
jest.clearAllMocks();
55+
56+
const newEntries = ['./foobar.js'];
57+
plugin.entries = newEntries;
58+
expect(plugin.entries).toBe(newEntries);
59+
60+
plugin.apply(compiler);
61+
62+
expect(tapPromise).toBeCalledTimes(1);
63+
expect(tapPromise.mock.calls[0]).toMatchSnapshot();
64+
65+
await tapPromise.mock.calls[0][1](compilation);
66+
expect(compilation.addEntry).toBeCalledTimes(newEntries.length);
67+
expect(compilation.addEntry.mock.calls).toMatchSnapshot();
68+
69+
expect(createDependency).toBeCalledTimes(newEntries.length);
70+
expect(createDependency.mock.calls).toMatchSnapshot();
71+
});
72+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`DevServerEntryPlugin should add hooks to add entries 1`] = `
4+
Array [
5+
"DevServerEntryPlugin",
6+
[Function],
7+
]
8+
`;
9+
10+
exports[`DevServerEntryPlugin should add hooks to add entries 2`] = `
11+
Array [
12+
Array [
13+
"context",
14+
undefined,
15+
Object {},
16+
[Function],
17+
],
18+
Array [
19+
"context",
20+
undefined,
21+
Object {},
22+
[Function],
23+
],
24+
]
25+
`;
26+
27+
exports[`DevServerEntryPlugin should add hooks to add entries 3`] = `
28+
Array [
29+
Array [
30+
"./foo.js",
31+
Object {},
32+
],
33+
Array [
34+
"./bar.js",
35+
Object {},
36+
],
37+
]
38+
`;
39+
40+
exports[`DevServerEntryPlugin should allow modifying entries after creation 1`] = `
41+
Array [
42+
"DevServerEntryPlugin",
43+
[Function],
44+
]
45+
`;
46+
47+
exports[`DevServerEntryPlugin should allow modifying entries after creation 2`] = `
48+
Array [
49+
Array [
50+
"context",
51+
undefined,
52+
Object {},
53+
[Function],
54+
],
55+
]
56+
`;
57+
58+
exports[`DevServerEntryPlugin should allow modifying entries after creation 3`] = `
59+
Array [
60+
Array [
61+
"./foobar.js",
62+
Object {},
63+
],
64+
]
65+
`;

0 commit comments

Comments
 (0)