Skip to content

Commit

Permalink
test(utils): increase coverage (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy authored and evilebottnawi committed Jul 29, 2019
1 parent 9c171d1 commit 1f9f9dc
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/utils/runBonjour.js
@@ -1,11 +1,11 @@
'use strict';

function runBonjour(options) {
function runBonjour({ port }) {
const bonjour = require('bonjour')();

bonjour.publish({
name: 'Webpack Dev Server',
port: options.port,
port,
type: 'http',
subtypes: ['webpack'],
});
Expand Down
35 changes: 35 additions & 0 deletions test/server/utils/ runBonjour.test.js
@@ -0,0 +1,35 @@
'use strict';

const runBonjour = require('../../../lib/utils/runBonjour');

describe('runBonjour', () => {
let mock;
let publish = jest.fn();
let unpublishAll = jest.fn();

beforeAll(() => {
mock = jest.mock('bonjour', () => () => {
return {
publish,
unpublishAll,
};
});
});

afterEach(() => {
publish = jest.fn();
unpublishAll = jest.fn();
});

afterAll(() => {
mock.mockRestore();
});

it('should call bonjour.publish', () => {
runBonjour({
port: 1111,
});

expect(publish.mock.calls[0][0]).toMatchSnapshot();
});
});
12 changes: 12 additions & 0 deletions test/server/utils/__snapshots__/ runBonjour.test.js.snap
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`runBonjour should call bonjour.publish 1`] = `
Object {
"name": "Webpack Dev Server",
"port": 1111,
"subtypes": Array [
"webpack",
],
"type": "http",
}
`;
9 changes: 9 additions & 0 deletions test/server/utils/__snapshots__/colors.test.js.snap
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`colors error should disable color 1`] = `"test"`;

exports[`colors error should enable color 1`] = `"test"`;

exports[`colors info should disable color 1`] = `"test"`;

exports[`colors info should enable color 1`] = `"test"`;
92 changes: 91 additions & 1 deletion test/server/utils/__snapshots__/createConfig.test.js.snap
Expand Up @@ -699,6 +699,21 @@ Object {
}
`;

exports[`createConfig liveReload option 1`] = `
Object {
"hot": true,
"hotOnly": false,
"liveReload": false,
"noInfo": true,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig mimeTypes option - with force 1`] = `
Object {
"hot": true,
Expand Down Expand Up @@ -835,7 +850,22 @@ Object {
}
`;

exports[`createConfig overlay 1`] = `
exports[`createConfig overlay option (in devServer config) 1`] = `
Object {
"hot": true,
"hotOnly": false,
"noInfo": true,
"overlay": true,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig overlay option 1`] = `
Object {
"hot": true,
"hotOnly": false,
Expand Down Expand Up @@ -967,6 +997,21 @@ Object {
}
`;

exports[`createConfig profile option 1`] = `
Object {
"hot": true,
"hotOnly": false,
"noInfo": true,
"port": 8080,
"profile": "profile",
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig progress option (devServer config) 1`] = `
Object {
"hot": true,
Expand Down Expand Up @@ -1155,6 +1200,51 @@ Object {
}
`;

exports[`createConfig sockHost option 1`] = `
Object {
"hot": true,
"hotOnly": false,
"noInfo": true,
"port": 8080,
"publicPath": "/",
"sockHost": true,
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig sockPath option 1`] = `
Object {
"hot": true,
"hotOnly": false,
"noInfo": true,
"port": 8080,
"publicPath": "/",
"sockPath": "path",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig sockPort option 1`] = `
Object {
"hot": true,
"hotOnly": false,
"noInfo": true,
"port": 8080,
"publicPath": "/",
"sockPort": "port",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig socket option (devServer config) 1`] = `
Object {
"hot": true,
Expand Down
3 changes: 0 additions & 3 deletions test/server/utils/__snapshots__/findPort.test.js.snap

This file was deleted.

25 changes: 25 additions & 0 deletions test/server/utils/colors.test.js
@@ -0,0 +1,25 @@
'use strict';

const colors = require('../../../lib/utils/colors');

describe('colors', () => {
describe('info', () => {
it('should disable color', () => {
expect(colors.info(false, 'test')).toMatchSnapshot();
});

it('should enable color', () => {
expect(colors.info(true, 'test')).toMatchSnapshot();
});
});

describe('error', () => {
it('should disable color', () => {
expect(colors.error(false, 'test')).toMatchSnapshot();
});

it('should enable color', () => {
expect(colors.error(true, 'test')).toMatchSnapshot();
});
});
});
16 changes: 16 additions & 0 deletions test/server/utils/createCertificate.test.js
@@ -0,0 +1,16 @@
'use strict';

const createCertificate = require('../../../lib/utils/createCertificate');

describe('createCertificate', () => {
it('should have keys', () => {
expect(createCertificate([{ name: 'commonName', value: 'wds' }])).toEqual(
expect.objectContaining({
private: expect.any(String),
public: expect.any(String),
cert: expect.any(String),
fingerprint: expect.any(String),
})
);
});
});
74 changes: 73 additions & 1 deletion test/server/utils/createConfig.test.js
Expand Up @@ -980,7 +980,19 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('overlay', () => {
it('overlay option', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, {
overlay: true,
}),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('overlay option (in devServer config)', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: { overlay: true },
Expand All @@ -991,4 +1003,64 @@ describe('createConfig', () => {

expect(config).toMatchSnapshot();
});

it('sockHost option', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, {
sockHost: true,
}),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('sockPath option', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, {
sockPath: 'path',
}),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('sockPort option', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, {
sockPort: 'port',
}),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('liveReload option', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, {
liveReload: false,
}),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('profile option', () => {
const config = createConfig(
webpackConfig,
Object.assign({}, argv, {
profile: 'profile',
}),
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion test/server/utils/createDomain.test.js
Expand Up @@ -7,7 +7,7 @@ const createDomain = require('../../../lib/utils/createDomain');
const [port1, port2] = require('../../ports-map').createDomain;
const config = require('./../../fixtures/simple-config/webpack.config');

describe('check utility functions', () => {
describe('createDomain', () => {
let compiler;
let server;

Expand Down
2 changes: 1 addition & 1 deletion test/server/utils/findPort.test.js
Expand Up @@ -4,7 +4,7 @@ const http = require('http');
const portfinder = require('portfinder');
const findPort = require('../../../lib/utils/findPort');

describe('findPort util', () => {
describe('findPort', () => {
let dummyServers = [];

afterEach(() => {
Expand Down
8 changes: 8 additions & 0 deletions test/server/utils/getSocketClientPath.test.js
Expand Up @@ -40,6 +40,14 @@ describe('getSocketClientPath', () => {
}).toThrow(/clientMode must be a string/);
});

it('should throw with clientMode: bad type', () => {
expect(() => {
getSocketClientPath({
clientMode: 1,
});
}).toThrow(/clientMode must be a string/);
});

it('should throw with clientMode: unimplemented client', () => {
expect(() => {
getSocketClientPath({
Expand Down

0 comments on commit 1f9f9dc

Please sign in to comment.