|
| 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 | +}); |
0 commit comments