Skip to content

test: add unit test for DevServerEntryPlugin #2840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions test/server/utils/DevServerEntryPlugin.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
65 changes: 65 additions & 0 deletions test/server/utils/__snapshots__/DevServerEntryPlugin.test.js.snap
Original file line number Diff line number Diff line change
@@ -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 {},
],
]
`;