Skip to content

Commit 5594637

Browse files
[PFX-186] - jest refactor @gasket/plugin-webpack (#538)
1 parent a7df763 commit 5594637

File tree

6 files changed

+122
-148
lines changed

6 files changed

+122
-148
lines changed

package-lock.json

Lines changed: 6 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/gasket-plugin-webpack/package.json

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
"scripts": {
1212
"lint": "eslint .",
1313
"lint:fix": "npm run lint -- --fix",
14-
"test": "npm run test:runner",
15-
"test:runner": "mocha --require ./test/setup.js \"test/**/*.test.js\"",
16-
"test:watch": "npm run test:runner -- --watch",
17-
"test:coverage": "nyc --reporter=text --reporter=json-summary npm run test:runner",
18-
"posttest": "npm run lint",
19-
"report": "nyc report --reporter=lcov"
14+
"test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' jest",
15+
"test:watch": "jest --watch",
16+
"test:coverage": "jest --coverage",
17+
"posttest": "npm run lint"
2018
},
2119
"repository": {
2220
"type": "git",
@@ -45,23 +43,20 @@
4543
},
4644
"devDependencies": {
4745
"@gasket/engine": "^6.36.1",
48-
"assume": "^2.3.0",
49-
"assume-sinon": "^1.1.0",
46+
"cross-env": "^7.0.3",
5047
"eslint": "^8.7.0",
5148
"eslint-config-godaddy": "^6.0.0",
49+
"eslint-plugin-jest": "^27.2.1",
5250
"eslint-plugin-json": "^3.1.0",
53-
"eslint-plugin-mocha": "^10.0.3",
5451
"eslint-plugin-unicorn": "^44.0.0",
55-
"mocha": "^10.0.0",
52+
"jest": "^29.3.1",
5653
"mock-require": "^3.0.3",
57-
"nyc": "^15.1.0",
58-
"proxyquire": "^2.1.3",
59-
"sinon": "^14.0.0",
6054
"webpack": "^5.21.2"
6155
},
6256
"eslintConfig": {
6357
"extends": [
64-
"godaddy"
58+
"godaddy",
59+
"plugin:jest/recommended"
6560
],
6661
"plugins": [
6762
"unicorn"
Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
const assume = require('assume');
2-
const sinon = require('sinon');
3-
const proxyquire = require('proxyquire');
4-
const deprecatedSmartMerge = require('webpack-merge').smart;
1+
const mockSmartStub = jest.fn();
2+
3+
jest.mock('webpack-merge', () => ({
4+
smart: mockSmartStub
5+
}));
6+
7+
const deprecatedMerges = require('../lib/deprecated-merges');
58

69
describe('deprecatedMerges', function () {
7-
let deprecatedMerges, smartStub, mockGasket, mockContext, mockConfig;
10+
let mockGasket, mockContext, mockConfig;
811
let mockChain, webpackChainCb, webpackCb;
912

1013
beforeEach(function () {
1114
mockChain = {
12-
toConfig: sinon.stub().returns({})
15+
toConfig: jest.fn().mockReturnValue({})
1316
};
1417
mockGasket = {
15-
execApplySync: sinon.stub().callsFake((name, callback) => {
18+
execApplySync: jest.fn().mockImplementation((name, callback) => {
1619
if (name === 'webpackChain') {
1720
webpackChainCb = callback;
1821
return mockChain;
@@ -22,46 +25,40 @@ describe('deprecatedMerges', function () {
2225
}
2326
}),
2427
logger: {
25-
warning: sinon.stub()
28+
warning: jest.fn()
2629
},
2730
config: {}
2831
};
2932
mockContext = {};
3033
mockConfig = {};
31-
32-
smartStub = sinon.stub().callsFake(deprecatedSmartMerge);
33-
deprecatedMerges = proxyquire('../lib/deprecated-merges', {
34-
'webpack-merge': {
35-
smart: smartStub
36-
}
37-
});
34+
mockSmartStub.mockReturnValue({});
3835
});
3936

4037
afterEach(function () {
41-
sinon.restore();
38+
jest.clearAllMocks();
4239
});
4340

4441
it('returns webpack config object', function () {
4542
const results = deprecatedMerges(mockGasket, mockConfig, mockContext);
46-
assume(results).is.an('object');
43+
expect(typeof results).toBe('object');
4744
});
4845

4946
it('smart merges', function () {
5047
deprecatedMerges(mockGasket, mockConfig, mockContext);
51-
assume(smartStub).called();
48+
expect(mockSmartStub).toHaveBeenCalled();
5249
});
5350

5451
describe('gasket.config.webpack', function () {
5552

5653
it('logs deprecated warning if set', function () {
5754
mockGasket.config.webpack = {};
5855
deprecatedMerges(mockGasket, mockConfig, mockContext);
59-
assume(mockGasket.logger.warning).calledWithMatch(/DEPRECATED `webpack` in Gasket config/);
56+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/DEPRECATED `webpack` in Gasket config/));
6057
});
6158

6259
it('does not log warning if not set', function () {
6360
deprecatedMerges(mockGasket, mockConfig, mockContext);
64-
assume(mockGasket.logger.warning).not.called();
61+
expect(mockGasket.logger.warning).not.toHaveBeenCalled();
6562
});
6663
});
6764

@@ -72,24 +69,24 @@ describe('deprecatedMerges', function () {
7269
});
7370

7471
it('logs deprecated warning', function () {
75-
webpackChainCb({ name: 'mock-plugin' }, sinon.stub());
76-
assume(mockGasket.logger.warning).calledWithMatch(/DEPRECATED `webpackChain` lifecycle/);
72+
webpackChainCb({ name: 'mock-plugin' }, jest.fn());
73+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/DEPRECATED `webpackChain` lifecycle/));
7774
});
7875

7976
it('logs plugin name', function () {
80-
webpackChainCb({ name: 'mock-plugin' }, sinon.stub());
81-
assume(mockGasket.logger.warning).calledWithMatch(/mock-plugin/);
77+
webpackChainCb({ name: 'mock-plugin' }, jest.fn());
78+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/mock-plugin/));
8279
});
8380

8481
it('logs unnamed plugin', function () {
85-
webpackChainCb({}, sinon.stub());
86-
assume(mockGasket.logger.warning).calledWithMatch(/unnamed plugin/);
82+
webpackChainCb({}, jest.fn());
83+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/unnamed plugin/));
8784
});
8885

8986
it('logs app lifecycle', function () {
9087
// eslint-disable-next-line no-undefined
91-
webpackChainCb(undefined, sinon.stub());
92-
assume(mockGasket.logger.warning).calledWithMatch(/app lifecycle/);
88+
webpackChainCb(undefined, jest.fn());
89+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/app lifecycle/));
9390
});
9491
});
9592

@@ -100,24 +97,24 @@ describe('deprecatedMerges', function () {
10097
});
10198

10299
it('logs deprecated warning', function () {
103-
webpackCb({ name: 'mock-plugin' }, sinon.stub());
104-
assume(mockGasket.logger.warning).calledWithMatch(/DEPRECATED `webpack` lifecycle/);
100+
webpackCb({ name: 'mock-plugin' }, jest.fn());
101+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/DEPRECATED `webpack` lifecycle/));
105102
});
106103

107104
it('logs plugin name', function () {
108-
webpackCb({ name: 'mock-plugin' }, sinon.stub());
109-
assume(mockGasket.logger.warning).calledWithMatch(/mock-plugin/);
105+
webpackCb({ name: 'mock-plugin' }, jest.fn());
106+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/mock-plugin/));
110107
});
111108

112109
it('logs unnamed plugin', function () {
113-
webpackCb({}, sinon.stub());
114-
assume(mockGasket.logger.warning).calledWithMatch(/unnamed plugin/);
110+
webpackCb({}, jest.fn());
111+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/unnamed plugin/));
115112
});
116113

117114
it('logs app lifecycle', function () {
118115
// eslint-disable-next-line no-undefined
119-
webpackCb(undefined, sinon.stub());
120-
assume(mockGasket.logger.warning).calledWithMatch(/app lifecycle/);
116+
webpackCb(undefined, jest.fn());
117+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/app lifecycle/));
121118
});
122119
});
123120
});

packages/gasket-plugin-webpack/test/init-webpack.test.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,110 @@
11
/* eslint-disable no-sync */
2-
const assume = require('assume');
3-
const sinon = require('sinon');
4-
const proxyquire = require('proxyquire');
5-
const WebpackMetricsPlugin = require('../lib/webpack-metrics-plugin');
2+
jest.mock('../lib/deprecated-merges', () => (_, config) => config);
63

4+
const initWebpack = require('../lib/init-webpack');
75

86
describe('deprecated merges', function () {
9-
let initWebpack, mockGasket, mockContext, mockConfig;
7+
let mockGasket, mockContext, mockConfig;
108

119
beforeEach(function () {
1210
mockGasket = {
13-
execApplySync: sinon.stub(),
11+
execApplySync: jest.fn(),
1412
logger: {
15-
warning: sinon.stub()
16-
}
13+
warning: jest.fn()
14+
},
15+
config: {}
1716
};
1817
mockContext = {};
1918
mockConfig = {};
20-
21-
initWebpack = proxyquire('../lib/init-webpack', {
22-
'./deprecated-merges': sinon.stub().callsFake((_, config) => config)
23-
});
2419
});
2520

2621
afterEach(function () {
27-
sinon.restore();
22+
jest.clearAllMocks();
23+
jest.resetModules();
2824
});
2925

3026
it('returns webpack config object', function () {
3127
const results = initWebpack(mockGasket, mockConfig, mockContext);
32-
assume(results).is.an('object');
28+
expect(typeof results).toBe('object');
3329
});
3430

3531
it('configures webpack metrics plugin', function () {
3632
const results = initWebpack(mockGasket, mockConfig, mockContext);
37-
assume(results).property('plugins');
38-
assume(results.plugins[0]).instanceof(WebpackMetricsPlugin);
33+
expect(results).toHaveProperty('plugins');
34+
expect(results.plugins[0].constructor.name).toBe('WebpackMetricsPlugin');
3935
});
4036

4137
it('executes webpackConfig lifecycle', function () {
4238
initWebpack(mockGasket, mockConfig, mockContext);
43-
assume(mockGasket.execApplySync).calledWith('webpackConfig');
39+
expect(mockGasket.execApplySync).toHaveBeenCalledWith('webpackConfig', expect.any(Function));
4440
});
4541

4642
describe('webpackConfig lifecycle callback', function () {
4743
let applyFn, mockPlugin, handlerStub, baseConfig;
4844

4945
beforeEach(function () {
5046
mockPlugin = { name: 'mock-plugin' };
51-
handlerStub = sinon.stub();
47+
handlerStub = jest.fn();
5248

5349
baseConfig = initWebpack(mockGasket, {}, mockContext);
54-
applyFn = mockGasket.execApplySync.getCall(0).args[1];
50+
applyFn = mockGasket.execApplySync.mock.calls[0][1];
5551
});
5652

5753
it('called with baseConfig', function () {
5854
applyFn(mockPlugin, handlerStub);
59-
assume(handlerStub).calledWith(baseConfig);
55+
expect(handlerStub).toHaveBeenCalledWith(baseConfig, expect.any(Object));
6056
});
6157

6258
it('called with context', function () {
6359
applyFn(mockPlugin, handlerStub);
64-
const context = handlerStub.getCall(0).args[1];
65-
assume(context).is.an('object');
60+
const context = handlerStub.mock.calls[0][1];
61+
expect(typeof context).toBe('object');
6662
});
6763

6864
// TODO: remove in next major version
6965
describe('context.webpackMerge', function () {
7066
it('getter logs deprecated warning', function () {
7167
applyFn(mockPlugin, handlerStub);
72-
const context = handlerStub.getCall(0).args[1];
73-
assume(mockGasket.logger.warning).not.called();
68+
const context = handlerStub.mock.calls[0][1];
69+
expect(mockGasket.logger.warning).not.toHaveBeenCalled();
7470
context.webpackMerge;
75-
assume(mockGasket.logger.warning).calledWithMatch(/DEPRECATED/);
71+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/DEPRECATED/));
7672
});
7773

7874
it('logs plugin name', function () {
7975
applyFn(mockPlugin, handlerStub);
80-
const context = handlerStub.getCall(0).args[1];
76+
const context = handlerStub.mock.calls[0][1];
8177
context.webpackMerge;
82-
assume(mockGasket.logger.warning).calledWithMatch(/mock-plugin/);
78+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/mock-plugin/));
8379
});
8480

8581
it('logs recommendation', function () {
8682
applyFn(mockPlugin, handlerStub);
87-
const context = handlerStub.getCall(0).args[1];
83+
const context = handlerStub.mock.calls[0][1];
8884
context.webpackMerge;
89-
assume(mockGasket.logger.warning).calledWithMatch(/Use `require\('webpack-merge'\)`/);
85+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/Use `require\('webpack-merge'\)`/));
9086
});
9187

9288
it('logs `unnamed plugin` if plugin name not set', function () {
9389
applyFn({}, handlerStub);
94-
const context = handlerStub.getCall(0).args[1];
90+
const context = handlerStub.mock.calls[0][1];
9591
context.webpackMerge;
96-
assume(mockGasket.logger.warning).calledWithMatch(/unnamed plugin/);
92+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/unnamed plugin/));
9793
});
9894

9995
it('logs app lifecycle', function () {
10096
// eslint-disable-next-line no-undefined
10197
applyFn(undefined, handlerStub);
102-
const context = handlerStub.getCall(0).args[1];
98+
const context = handlerStub.mock.calls[0][1];
10399
context.webpackMerge;
104-
assume(mockGasket.logger.warning).calledWithMatch(/app lifecycle/);
100+
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/app lifecycle/));
105101
});
106102
});
107103

108104
it('context.webpack returns webpack', function () {
109105
applyFn(mockPlugin, handlerStub);
110-
const context = handlerStub.getCall(0).args[1];
111-
assume(context.webpack).equals(require('webpack'));
106+
const context = handlerStub.mock.calls[0][1];
107+
expect(context.webpack).toEqual(require('webpack'));
112108
});
113109

114110
});

0 commit comments

Comments
 (0)