Skip to content

Commit

Permalink
jest refactor @gasket/plugin-manifest (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmason2-godaddy authored Jan 18, 2023
1 parent 3b8eefb commit 9d5b3f5
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 128 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

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

18 changes: 8 additions & 10 deletions packages/gasket-plugin-manifest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"test": "npm run test:runner",
"test:runner": "mocha \"test/**/*.test.js\"",
"test:coverage": "nyc --reporter=text --reporter=json-summary npm run test:runner",
"test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"posttest": "npm run lint"
},
"repository": {
Expand Down Expand Up @@ -45,20 +45,18 @@
},
"devDependencies": {
"@gasket/engine": "^6.36.1",
"assume": "^2.3.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",
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"sinon": "^14.0.0"
"jest": "^29.3.1"
},
"eslintConfig": {
"extends": [
"godaddy"
"godaddy",
"plugin:jest/recommended"
],
"plugins": [
"unicorn"
Expand Down
53 changes: 24 additions & 29 deletions packages/gasket-plugin-manifest/test/build.test.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,67 @@
const assume = require('assume');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const mockWriteFileStub = jest.fn();
const mockMkdirpStub = jest.fn();

describe('build', function () {
const writeFileStub = sinon.stub();
const mkdirpStub = sinon.stub();
jest.mock('fs', () => ({
promises: {
writeFile: mockWriteFileStub
}
}));
jest.mock('mkdirp', () => mockMkdirpStub);

const build = proxyquire('../lib/build', {
fs: {
promises: {
writeFile: writeFileStub
}
},
mkdirp: mkdirpStub,
replace: sinon.stub()
});
const build = require('../lib/build');

describe('build', function () {
let gasket;

beforeEach(function () {
gasket = {
execWaterfall: sinon.stub().resolves([]),
execWaterfall: jest.fn().mockResolvedValue([]),
config: {
root: 'test',
manifest: {
staticOutput: '/custom/manifest.json'
}
},
logger: {
debug: sinon.stub(),
error: sinon.stub(),
log: sinon.stub()
debug: jest.fn(),
error: jest.fn(),
log: jest.fn()
}
};
});

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

it('is a function', function () {
assume(build).is.a('asyncfunction');
assume(build).has.length(1);
expect(typeof build).toBe('function');
expect(build).toHaveLength(1);
});

it('skips logic when staticOutput config is not set', async function () {
gasket.config.manifest = {};
await build(gasket);

assume(mkdirpStub.called).false();
expect(mockMkdirpStub).not.toHaveBeenCalled();
});

it('creates custom output directory', async function () {
gasket.config.manifest.staticOutput = '/super/cool/custom/path/manifest.json';
await build(gasket);
assume(mkdirpStub.calledOnce).true();
assume(mkdirpStub.args[0][0]).eqls('/super/cool/custom/path/');
expect(mockMkdirpStub).toHaveBeenCalled();
expect(mockMkdirpStub.mock.calls[0][0]).toEqual('/super/cool/custom/path/');
});

it('writes manifest to specified path', async function () {
await build(gasket);
assume(writeFileStub.calledOnce).true();
assume(writeFileStub.args[0]).eqls(['/custom/manifest.json', '[]', 'utf-8']);
expect(mockWriteFileStub.mock.calls.length).toBe(1);
expect(mockWriteFileStub.mock.calls[0]).toEqual(['/custom/manifest.json', '[]', 'utf-8']);
});

it('logs completion message', async function () {
await build(gasket);
assume(gasket.logger.log.calledOnce).true();
assume(gasket.logger.log.args[0][0]).includes('custom/manifest.json).');
expect(gasket.logger.log.mock.calls.length).toBe(1);
expect(gasket.logger.log.mock.calls[0][0]).toContain('custom/manifest.json).');
});
});
37 changes: 16 additions & 21 deletions packages/gasket-plugin-manifest/test/configure.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
const assume = require('assume');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const mockJoinStub = jest.fn();

jest.mock('path', () => ({
join: mockJoinStub
}));

const configure = require('../lib/configure');
const baseConfig = require('../lib/base-config');

describe('configure', function () {
const joinStub = sinon.stub();

const configure = proxyquire('../lib/configure', {
path: {
join: joinStub
}
});

let gasket;

beforeEach(function () {
gasket = {
execWaterfall: sinon.stub().resolves([]),
execWaterfall: jest.fn().mockResolvedValue([]),
config: {
manifest: {
name: 'Walter White',
Expand All @@ -29,18 +24,18 @@ describe('configure', function () {
root: 'test/'
},
logger: {
debug: sinon.stub()
debug: jest.fn()
}
};
});

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

it('is a function', function () {
assume(configure).is.a('function');
assume(configure).has.length(1);
expect(typeof configure).toBe('function');
expect(configure).toHaveLength(1);
});

it('merges base config with the manifest config', function () {
Expand All @@ -49,7 +44,7 @@ describe('configure', function () {

const results = configure(gasket, config);

assume(results.manifest).eqls(
expect(results.manifest).toEqual(
{ ...baseConfig, scope }
);
});
Expand All @@ -59,7 +54,7 @@ describe('configure', function () {

const results = configure(gasket, config);

assume(results.manifest).eqls(baseConfig);
expect(results.manifest).toEqual(baseConfig);
});

it('uses default path when staticOutput is true', function () {
Expand All @@ -68,7 +63,7 @@ describe('configure', function () {

configure(gasket, config);

assume(joinStub.args[0][1]).eqls('public/manifest.json');
expect(mockJoinStub.mock.calls[0][1]).toEqual('public/manifest.json');
});

it('uses custom path when passed from manifest', function () {
Expand All @@ -77,11 +72,11 @@ describe('configure', function () {

configure(gasket, config);

assume(joinStub.args[0][1]).eqls(staticOutput);
expect(mockJoinStub.mock.calls[0][1]).toEqual(staticOutput);
});

it('sets static output to false when not configured', function () {
configure(gasket, { manifest: {} });
assume(joinStub.called).false();
expect(mockJoinStub).not.toHaveBeenCalled();
});
});
14 changes: 6 additions & 8 deletions packages/gasket-plugin-manifest/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const assume = require('assume');

const plugin = require('../lib');

describe('Plugin', function () {
it('is an object', function () {
assume(plugin).is.an('object');
expect(typeof plugin).toBe('object');
});

it('has expected name', function () {
assume(plugin).to.have.property('name', require('../package').name);
expect(plugin).toHaveProperty('name', require('../package').name);
});

it('has expected hooks', function () {
Expand All @@ -21,17 +19,17 @@ describe('Plugin', function () {
'metadata'
];

assume(plugin).to.have.property('hooks');
expect(plugin).toHaveProperty('hooks');

const hooks = Object.keys(plugin.hooks);
assume(hooks).eqls(expected);
assume(hooks).is.length(expected.length);
expect(hooks).toEqual(expected);
expect(hooks).toHaveLength(expected.length);
});

describe('meta', function () {
it('returns lifecycle metadata', function () {
const results = plugin.hooks.metadata({}, {});
assume(results.lifecycles).is.an('array');
expect(Array.isArray(results.lifecycles)).toBe(true);
});
});
});
29 changes: 13 additions & 16 deletions packages/gasket-plugin-manifest/test/middleware.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const assume = require('assume');
const sinon = require('sinon');

const middleware = require('../lib/middleware');

describe('middleware', function () {
Expand All @@ -9,7 +6,7 @@ describe('middleware', function () {

beforeEach(function () {
gasket = {
execWaterfall: sinon.stub().resolves([]),
execWaterfall: jest.fn().mockResolvedValue([]),
config: {
manifest: {
name: 'Walter White',
Expand All @@ -20,33 +17,33 @@ describe('middleware', function () {
}
},
logger: {
debug: sinon.stub()
debug: jest.fn()
}
};
});

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

describe('#timing', function () {

it('is set to last', function () {
assume(timing).property('last', true);
expect(timing).toHaveProperty('last', true);
});
});

describe('#handler', function () {

it('is a function', function () {
assume(handler).is.a('function');
assume(handler).has.length(1);
expect(typeof handler).toBe('function');
expect(handler).toHaveLength(1);
});

it('returns middleware', function () {
const fn = handler(gasket, {});
assume(fn).is.an('asyncfunction');
assume(fn).has.length(3);
expect(typeof fn).toBe('function');
expect(fn).toHaveLength(3);
});

it('gathers manifest data if looking for manifest.json', async function () {
Expand All @@ -55,7 +52,7 @@ describe('middleware', function () {
path: 'manifest.json'
};
await fn(req, {}, function () { });
assume(gasket.execWaterfall.calledOnce).is.true();
expect(gasket.execWaterfall).toHaveBeenCalledTimes(1);
});

it('gathers manifest data if looking for the service worker script', async function () {
Expand All @@ -64,7 +61,7 @@ describe('middleware', function () {
path: 'sw.js'
};
await fn(req, {}, function () { });
assume(gasket.execWaterfall.calledOnce).is.true();
expect(gasket.execWaterfall).toHaveBeenCalledTimes(1);
});

it('passes the incoming request to the manifest hook', async function () {
Expand All @@ -74,8 +71,8 @@ describe('middleware', function () {
path: 'manifest.json'
};
await fn(req, {}, function () { });
assume(gasket.execWaterfall.args[0]).has.length(3);
assume(gasket.execWaterfall.args[0][2]).deep.equals(context);
expect(gasket.execWaterfall.mock.calls[0]).toHaveLength(3);
expect(gasket.execWaterfall.mock.calls[0][2]).toEqual(context);
});

it('takes precedence from gasket config over base config', async function () {
Expand All @@ -85,7 +82,7 @@ describe('middleware', function () {
path: 'manifest.json'
};
await fn(req, {}, function () { });
assume(gasket.execWaterfall.args[0][1].display).equals('BOGUS');
expect(gasket.execWaterfall.mock.calls[0][1].display).toEqual('BOGUS');
});
});
});
Loading

0 comments on commit 9d5b3f5

Please sign in to comment.