diff --git a/test/server/utils/DevServerEntryPlugin.test.js b/test/server/utils/DevServerEntryPlugin.test.js new file mode 100644 index 0000000000..df4c3d4283 --- /dev/null +++ b/test/server/utils/DevServerEntryPlugin.test.js @@ -0,0 +1,72 @@ +'use strict'; + +const isWebpack5 = require('../../helpers/isWebpack5'); + +(isWebpack5 ? describe : describe.skip)('DevServerEntryPlugin', () => { + let plugin; + const options = {}; + const entries = ['./foo.js', './bar.js']; + + const createDependency = jest.fn(); + const tapPromise = jest.fn(); + const compiler = { + hooks: { + make: { + tapPromise, + }, + }, + }; + const compilation = { + addEntry: jest.fn((_context, _dep, _options, cb) => cb()), + }; + + beforeEach(() => { + jest.setMock('webpack/lib/EntryPlugin.js', { createDependency }); + const DevServerEntryPlugin = require('../../../lib/utils/DevServerEntryPlugin'); + plugin = new DevServerEntryPlugin('context', entries, options); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should set property', () => { + expect(plugin.context).toBe('context'); + expect(plugin.entries).toBe(entries); + expect(plugin.options).toBe(options); + }); + + it('should add hooks to add entries', async () => { + plugin.apply(compiler); + expect(tapPromise).toBeCalledTimes(1); + expect(tapPromise.mock.calls[0]).toMatchSnapshot(); + + await tapPromise.mock.calls[0][1](compilation); + expect(compilation.addEntry).toBeCalledTimes(entries.length); + expect(compilation.addEntry.mock.calls).toMatchSnapshot(); + + expect(createDependency).toBeCalledTimes(entries.length); + expect(createDependency.mock.calls).toMatchSnapshot(); + }); + + it('should allow modifying entries after creation', async () => { + plugin.apply(compiler); + jest.clearAllMocks(); + + const newEntries = ['./foobar.js']; + plugin.entries = newEntries; + expect(plugin.entries).toBe(newEntries); + + plugin.apply(compiler); + + expect(tapPromise).toBeCalledTimes(1); + expect(tapPromise.mock.calls[0]).toMatchSnapshot(); + + await tapPromise.mock.calls[0][1](compilation); + expect(compilation.addEntry).toBeCalledTimes(newEntries.length); + expect(compilation.addEntry.mock.calls).toMatchSnapshot(); + + expect(createDependency).toBeCalledTimes(newEntries.length); + expect(createDependency.mock.calls).toMatchSnapshot(); + }); +}); diff --git a/test/server/utils/__snapshots__/DevServerEntryPlugin.test.js.snap b/test/server/utils/__snapshots__/DevServerEntryPlugin.test.js.snap new file mode 100644 index 0000000000..a5e909274b --- /dev/null +++ b/test/server/utils/__snapshots__/DevServerEntryPlugin.test.js.snap @@ -0,0 +1,65 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`DevServerEntryPlugin should add hooks to add entries 1`] = ` +Array [ + "DevServerEntryPlugin", + [Function], +] +`; + +exports[`DevServerEntryPlugin should add hooks to add entries 2`] = ` +Array [ + Array [ + "context", + undefined, + Object {}, + [Function], + ], + Array [ + "context", + undefined, + Object {}, + [Function], + ], +] +`; + +exports[`DevServerEntryPlugin should add hooks to add entries 3`] = ` +Array [ + Array [ + "./foo.js", + Object {}, + ], + Array [ + "./bar.js", + Object {}, + ], +] +`; + +exports[`DevServerEntryPlugin should allow modifying entries after creation 1`] = ` +Array [ + "DevServerEntryPlugin", + [Function], +] +`; + +exports[`DevServerEntryPlugin should allow modifying entries after creation 2`] = ` +Array [ + Array [ + "context", + undefined, + Object {}, + [Function], + ], +] +`; + +exports[`DevServerEntryPlugin should allow modifying entries after creation 3`] = ` +Array [ + Array [ + "./foobar.js", + Object {}, + ], +] +`;