Skip to content

Commit

Permalink
tests(serve): completed startDevServer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed Aug 25, 2020
1 parent 97bd5f2 commit bf2f415
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 9 deletions.
84 changes: 84 additions & 0 deletions packages/serve/__tests__/__snapshots__/startDevServer.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`startDevServer should set default port and host if not provided 1`] = `
Object {
"host": "localhost",
"port": 8080,
}
`;

exports[`startDevServer should set default port and host if not provided 2`] = `
Array [
8080,
"localhost",
[Function],
]
`;

exports[`startDevServer should start dev server correctly for multi compiler with 1 devServer config 1`] = `
Object {
"bonjour": true,
"host": "my.host",
"hot": true,
"port": 9000,
"progress": true,
}
`;

exports[`startDevServer should start dev server correctly for multi compiler with 1 devServer config 2`] = `
Array [
9000,
"my.host",
[Function],
]
`;

exports[`startDevServer should start dev server correctly for single compiler 1`] = `
Object {
"bonjour": true,
"host": "my.host",
"hot": true,
"port": 9000,
"progress": true,
}
`;

exports[`startDevServer should start dev server correctly for single compiler 2`] = `
Array [
9000,
"my.host",
[Function],
]
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 1`] = `
Object {
"host": "localhost",
"port": 9000,
"progress": true,
}
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 2`] = `
Object {
"host": "localhost",
"port": 9001,
"progress": true,
}
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 3`] = `
Array [
9000,
"localhost",
[Function],
]
`;

exports[`startDevServer should start dev servers correctly for multi compiler with 2 devServer configs 4`] = `
Array [
9001,
"localhost",
[Function],
]
`;
1 change: 1 addition & 0 deletions packages/serve/__tests__/parseArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('parseArgs', () => {

beforeEach(() => {
errorMock.mockClear();
processExitSpy.mockClear();
});

it('parses webpack and dev server args', () => {
Expand Down
146 changes: 137 additions & 9 deletions packages/serve/__tests__/startDevServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,159 @@

import startDevServer from '../src/startDevServer';

jest.mock('webpack-dev-server/lib/Server');

describe('startDevServer', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const webpack = require('webpack');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const DevServer = require('webpack-dev-server/lib/Server');

beforeEach(() => {
DevServer.mockClear();
});

it('should start dev server correctly for single compiler', () => {
const config = {
devServer: {
// TODO: switch to static for dev server v4
watchContentBase: false,
port: 9000,
hot: false,
bonjour: true,
},
};
const compiler = webpack(config);

startDevServer(compiler, ['--hot-only']);
const servers = startDevServer(compiler, {
host: 'my.host',
hot: true,
progress: true,
});

expect(servers.length).toEqual(1);
expect(servers).toEqual(DevServer.mock.instances);

// this is the constructor
expect(DevServer.mock.calls.length).toEqual(1);
// the 2nd argument is the options
expect(DevServer.mock.calls[0][1]).toMatchSnapshot();

// the server should listen on correct host and port
expect(DevServer.mock.instances[0].listen.mock.calls.length).toEqual(1);
expect(DevServer.mock.instances[0].listen.mock.calls[0]).toMatchSnapshot();
});

it('should set default port and host if not provided', () => {
const config = {
devServer: {},
};
const compiler = webpack(config);

const servers = startDevServer(compiler, {});

expect(servers.length).toEqual(1);
expect(servers).toEqual(DevServer.mock.instances);

// this is the constructor
expect(DevServer.mock.calls.length).toEqual(1);
// the 2nd argument is the options
expect(DevServer.mock.calls[0][1]).toMatchSnapshot();

// the server should listen on correct host and port
expect(DevServer.mock.instances[0].listen.mock.calls.length).toEqual(1);
expect(DevServer.mock.instances[0].listen.mock.calls[0]).toMatchSnapshot();
});

it('should start dev server correctly for multi compiler with 1 devServer config', () => {
const config = [
{
devServer: {
port: 9000,
hot: false,
bonjour: true,
},
},
{},
];
const compiler = webpack(config);

const servers = startDevServer(compiler, {
host: 'my.host',
hot: true,
progress: true,
});

expect(servers.length).toEqual(1);
expect(servers).toEqual(DevServer.mock.instances);

// this is the constructor
expect(DevServer.mock.calls.length).toEqual(1);
// the 2nd argument is the options
expect(DevServer.mock.calls[0][1]).toMatchSnapshot();

// the server should listen on correct host and port
expect(DevServer.mock.instances[0].listen.mock.calls.length).toEqual(1);
expect(DevServer.mock.instances[0].listen.mock.calls[0]).toMatchSnapshot();
});

// it('should start dev server correctly for multi compiler with 1 devServer config', () => {
it('should start dev servers correctly for multi compiler with 2 devServer configs', () => {
const config = [
{
devServer: {
port: 9000,
// here to show that it will be overridden
progress: false,
},
},
{
devServer: {
port: 9001,
},
},
];
const compiler = webpack(config);

const servers = startDevServer(compiler, {
progress: true,
});

// });
// there are 2 devServer configs, so both are run
expect(servers.length).toEqual(2);
expect(servers).toEqual(DevServer.mock.instances);

// it('should start dev servers correctly for multi compiler with 2 devServer configs', () => {
// this is the constructor
expect(DevServer.mock.calls.length).toEqual(2);
// the 2nd argument is the options
expect(DevServer.mock.calls[0][1]).toMatchSnapshot();
expect(DevServer.mock.calls[1][1]).toMatchSnapshot();

// });
// both servers should listen on correct host and port

// it('should handle 2 multi compiler devServer configs with conflicting ports', () => {
expect(DevServer.mock.instances[0].listen.mock.calls.length).toEqual(1);
expect(DevServer.mock.instances[0].listen.mock.calls[0]).toMatchSnapshot();

// });
expect(DevServer.mock.instances[1].listen.mock.calls.length).toEqual(1);
expect(DevServer.mock.instances[1].listen.mock.calls[0]).toMatchSnapshot();
});

it('should handle 2 multi compiler devServer configs with conflicting ports', () => {
expect(() => {
const config = [
{
devServer: {
port: 9000,
},
},
{
devServer: {
port: 9000,
},
},
];
const compiler = webpack(config);

startDevServer(compiler, {});
}).toThrow(
'Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --config-name flag to specify your desired config.',
);
});
});

0 comments on commit bf2f415

Please sign in to comment.