Skip to content

Commit

Permalink
[PFX-186] - jest refactor @gasket/plugin-webpack (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmason2-godaddy authored Jan 23, 2023
1 parent a7df763 commit 5594637
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 148 deletions.
20 changes: 6 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 9 additions & 14 deletions packages/gasket-plugin-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"test": "npm run test:runner",
"test:runner": "mocha --require ./test/setup.js \"test/**/*.test.js\"",
"test:watch": "npm run test:runner -- --watch",
"test:coverage": "nyc --reporter=text --reporter=json-summary npm run test:runner",
"posttest": "npm run lint",
"report": "nyc report --reporter=lcov"
"test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"posttest": "npm run lint"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,23 +43,20 @@
},
"devDependencies": {
"@gasket/engine": "^6.36.1",
"assume": "^2.3.0",
"assume-sinon": "^1.1.0",
"cross-env": "^7.0.3",
"eslint": "^8.7.0",
"eslint-config-godaddy": "^6.0.0",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-mocha": "^10.0.3",
"eslint-plugin-unicorn": "^44.0.0",
"mocha": "^10.0.0",
"jest": "^29.3.1",
"mock-require": "^3.0.3",
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"sinon": "^14.0.0",
"webpack": "^5.21.2"
},
"eslintConfig": {
"extends": [
"godaddy"
"godaddy",
"plugin:jest/recommended"
],
"plugins": [
"unicorn"
Expand Down
69 changes: 33 additions & 36 deletions packages/gasket-plugin-webpack/test/deprecated-merges.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const assume = require('assume');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const deprecatedSmartMerge = require('webpack-merge').smart;
const mockSmartStub = jest.fn();

jest.mock('webpack-merge', () => ({
smart: mockSmartStub
}));

const deprecatedMerges = require('../lib/deprecated-merges');

describe('deprecatedMerges', function () {
let deprecatedMerges, smartStub, mockGasket, mockContext, mockConfig;
let mockGasket, mockContext, mockConfig;
let mockChain, webpackChainCb, webpackCb;

beforeEach(function () {
mockChain = {
toConfig: sinon.stub().returns({})
toConfig: jest.fn().mockReturnValue({})
};
mockGasket = {
execApplySync: sinon.stub().callsFake((name, callback) => {
execApplySync: jest.fn().mockImplementation((name, callback) => {
if (name === 'webpackChain') {
webpackChainCb = callback;
return mockChain;
Expand All @@ -22,46 +25,40 @@ describe('deprecatedMerges', function () {
}
}),
logger: {
warning: sinon.stub()
warning: jest.fn()
},
config: {}
};
mockContext = {};
mockConfig = {};

smartStub = sinon.stub().callsFake(deprecatedSmartMerge);
deprecatedMerges = proxyquire('../lib/deprecated-merges', {
'webpack-merge': {
smart: smartStub
}
});
mockSmartStub.mockReturnValue({});
});

afterEach(function () {
sinon.restore();
jest.clearAllMocks();
});

it('returns webpack config object', function () {
const results = deprecatedMerges(mockGasket, mockConfig, mockContext);
assume(results).is.an('object');
expect(typeof results).toBe('object');
});

it('smart merges', function () {
deprecatedMerges(mockGasket, mockConfig, mockContext);
assume(smartStub).called();
expect(mockSmartStub).toHaveBeenCalled();
});

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

it('logs deprecated warning if set', function () {
mockGasket.config.webpack = {};
deprecatedMerges(mockGasket, mockConfig, mockContext);
assume(mockGasket.logger.warning).calledWithMatch(/DEPRECATED `webpack` in Gasket config/);
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/DEPRECATED `webpack` in Gasket config/));
});

it('does not log warning if not set', function () {
deprecatedMerges(mockGasket, mockConfig, mockContext);
assume(mockGasket.logger.warning).not.called();
expect(mockGasket.logger.warning).not.toHaveBeenCalled();
});
});

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

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

it('logs plugin name', function () {
webpackChainCb({ name: 'mock-plugin' }, sinon.stub());
assume(mockGasket.logger.warning).calledWithMatch(/mock-plugin/);
webpackChainCb({ name: 'mock-plugin' }, jest.fn());
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/mock-plugin/));
});

it('logs unnamed plugin', function () {
webpackChainCb({}, sinon.stub());
assume(mockGasket.logger.warning).calledWithMatch(/unnamed plugin/);
webpackChainCb({}, jest.fn());
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/unnamed plugin/));
});

it('logs app lifecycle', function () {
// eslint-disable-next-line no-undefined
webpackChainCb(undefined, sinon.stub());
assume(mockGasket.logger.warning).calledWithMatch(/app lifecycle/);
webpackChainCb(undefined, jest.fn());
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/app lifecycle/));
});
});

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

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

it('logs plugin name', function () {
webpackCb({ name: 'mock-plugin' }, sinon.stub());
assume(mockGasket.logger.warning).calledWithMatch(/mock-plugin/);
webpackCb({ name: 'mock-plugin' }, jest.fn());
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/mock-plugin/));
});

it('logs unnamed plugin', function () {
webpackCb({}, sinon.stub());
assume(mockGasket.logger.warning).calledWithMatch(/unnamed plugin/);
webpackCb({}, jest.fn());
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/unnamed plugin/));
});

it('logs app lifecycle', function () {
// eslint-disable-next-line no-undefined
webpackCb(undefined, sinon.stub());
assume(mockGasket.logger.warning).calledWithMatch(/app lifecycle/);
webpackCb(undefined, jest.fn());
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/app lifecycle/));
});
});
});
66 changes: 31 additions & 35 deletions packages/gasket-plugin-webpack/test/init-webpack.test.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,110 @@
/* eslint-disable no-sync */
const assume = require('assume');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const WebpackMetricsPlugin = require('../lib/webpack-metrics-plugin');
jest.mock('../lib/deprecated-merges', () => (_, config) => config);

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

describe('deprecated merges', function () {
let initWebpack, mockGasket, mockContext, mockConfig;
let mockGasket, mockContext, mockConfig;

beforeEach(function () {
mockGasket = {
execApplySync: sinon.stub(),
execApplySync: jest.fn(),
logger: {
warning: sinon.stub()
}
warning: jest.fn()
},
config: {}
};
mockContext = {};
mockConfig = {};

initWebpack = proxyquire('../lib/init-webpack', {
'./deprecated-merges': sinon.stub().callsFake((_, config) => config)
});
});

afterEach(function () {
sinon.restore();
jest.clearAllMocks();
jest.resetModules();
});

it('returns webpack config object', function () {
const results = initWebpack(mockGasket, mockConfig, mockContext);
assume(results).is.an('object');
expect(typeof results).toBe('object');
});

it('configures webpack metrics plugin', function () {
const results = initWebpack(mockGasket, mockConfig, mockContext);
assume(results).property('plugins');
assume(results.plugins[0]).instanceof(WebpackMetricsPlugin);
expect(results).toHaveProperty('plugins');
expect(results.plugins[0].constructor.name).toBe('WebpackMetricsPlugin');
});

it('executes webpackConfig lifecycle', function () {
initWebpack(mockGasket, mockConfig, mockContext);
assume(mockGasket.execApplySync).calledWith('webpackConfig');
expect(mockGasket.execApplySync).toHaveBeenCalledWith('webpackConfig', expect.any(Function));
});

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

beforeEach(function () {
mockPlugin = { name: 'mock-plugin' };
handlerStub = sinon.stub();
handlerStub = jest.fn();

baseConfig = initWebpack(mockGasket, {}, mockContext);
applyFn = mockGasket.execApplySync.getCall(0).args[1];
applyFn = mockGasket.execApplySync.mock.calls[0][1];
});

it('called with baseConfig', function () {
applyFn(mockPlugin, handlerStub);
assume(handlerStub).calledWith(baseConfig);
expect(handlerStub).toHaveBeenCalledWith(baseConfig, expect.any(Object));
});

it('called with context', function () {
applyFn(mockPlugin, handlerStub);
const context = handlerStub.getCall(0).args[1];
assume(context).is.an('object');
const context = handlerStub.mock.calls[0][1];
expect(typeof context).toBe('object');
});

// TODO: remove in next major version
describe('context.webpackMerge', function () {
it('getter logs deprecated warning', function () {
applyFn(mockPlugin, handlerStub);
const context = handlerStub.getCall(0).args[1];
assume(mockGasket.logger.warning).not.called();
const context = handlerStub.mock.calls[0][1];
expect(mockGasket.logger.warning).not.toHaveBeenCalled();
context.webpackMerge;
assume(mockGasket.logger.warning).calledWithMatch(/DEPRECATED/);
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/DEPRECATED/));
});

it('logs plugin name', function () {
applyFn(mockPlugin, handlerStub);
const context = handlerStub.getCall(0).args[1];
const context = handlerStub.mock.calls[0][1];
context.webpackMerge;
assume(mockGasket.logger.warning).calledWithMatch(/mock-plugin/);
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/mock-plugin/));
});

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

it('logs `unnamed plugin` if plugin name not set', function () {
applyFn({}, handlerStub);
const context = handlerStub.getCall(0).args[1];
const context = handlerStub.mock.calls[0][1];
context.webpackMerge;
assume(mockGasket.logger.warning).calledWithMatch(/unnamed plugin/);
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/unnamed plugin/));
});

it('logs app lifecycle', function () {
// eslint-disable-next-line no-undefined
applyFn(undefined, handlerStub);
const context = handlerStub.getCall(0).args[1];
const context = handlerStub.mock.calls[0][1];
context.webpackMerge;
assume(mockGasket.logger.warning).calledWithMatch(/app lifecycle/);
expect(mockGasket.logger.warning).toHaveBeenCalledWith(expect.stringMatching(/app lifecycle/));
});
});

it('context.webpack returns webpack', function () {
applyFn(mockPlugin, handlerStub);
const context = handlerStub.getCall(0).args[1];
assume(context.webpack).equals(require('webpack'));
const context = handlerStub.mock.calls[0][1];
expect(context.webpack).toEqual(require('webpack'));
});

});
Expand Down
Loading

0 comments on commit 5594637

Please sign in to comment.