Skip to content

Commit

Permalink
[PFX-186] - jest refactor @gasket/plugin-docs (#504)
Browse files Browse the repository at this point in the history
* updating docs plugin with jest

* refactor testing to jest

* config-set-builder refactor

* linting, docs plugin jest refactor

* remove comment
  • Loading branch information
mmason2-godaddy authored Jan 9, 2023
1 parent f7112c1 commit 427f73a
Show file tree
Hide file tree
Showing 15 changed files with 1,382 additions and 1,307 deletions.
1,664 changes: 850 additions & 814 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions packages/gasket-plugin-docs/lib/utils/config-set-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class DocsConfigSetBuilder {
const fileSet = new Set([]);

const tryAdd = maybeFile => {
if (Boolean(maybeFile) && !isUrl.test(maybeFile)) {
if (Boolean(maybeFile) && !isUrl.test(maybeFile) && typeof maybeFile === 'string') {
fileSet.add(noHash(maybeFile));
}
};
Expand All @@ -110,7 +110,6 @@ class DocsConfigSetBuilder {

let { files = [] } = docsSetup;
files = Array.isArray(files) ? files : [files];

(await Promise.all(files.map(async g => await glob(g, { cwd: sourceRoot }))))
.reduce((acc, cur) => acc.concat(cur), [])
.forEach(file => tryAdd(file));
Expand Down
25 changes: 12 additions & 13 deletions packages/gasket-plugin-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"test": "npm run test:runner",
"test:runner": "mocha --recursive --require test/setup.js test",
"test:watch": "npm run test:runner -- --watch",
"test:coverage": "nyc --reporter=text --reporter=lcov --all 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",
"prepack": "npm run docs",
"docs": "jsdoc2md --plugin @godaddy/dmd --files lib/*.js > docs/api.md"
Expand Down Expand Up @@ -51,28 +50,28 @@
"@gasket/engine": "^6.36.1",
"@gasket/plugin-metadata": "^6.36.1",
"@godaddy/dmd": "^1.0.0",
"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.0.0",
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-mocha": "^10.0.3",
"eslint-plugin-unicorn": "^44.0.0",
"jsdoc-to-markdown": "^7.1.0",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"sinon": "^14.0.0"
"jest": "^29.3.1",
"jsdoc-to-markdown": "^7.1.0"
},
"eslintConfig": {
"extends": [
"godaddy"
"godaddy",
"plugin:jest/recommended"
],
"plugins": [
"unicorn"
],
"rules": {
"unicorn/filename-case": "error"
}
},
"jest": {
"testEnvironment": "node"
}
}
9 changes: 4 additions & 5 deletions packages/gasket-plugin-docs/test/configure.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const assume = require('assume');
const configure = require('../lib/configure');

describe('configure', () => {
Expand All @@ -8,25 +7,25 @@ describe('configure', () => {

it('returns object', () => {
const results = configure(mockGasket);
assume(results).is.an('object');
expect(results).toBeInstanceOf(Object);
});

it('adds docs to config', () => {
const results = configure(mockGasket);
assume(results).property('docs');
expect(results).toHaveProperty('docs');
});

it('merges user config with defaults', () => {
const results = configure(mockGasket, { docs: { user: 'stuff' } });
assume(results.docs).eqls({
expect(results.docs).toEqual({
user: 'stuff',
outputDir: '.docs'
});
});

it('user config overrides defaults', () => {
const results = configure(mockGasket, { docs: { user: 'stuff', outputDir: 'custom' } });
assume(results.docs).eqls({
expect(results.docs).toEqual({
user: 'stuff',
outputDir: 'custom'
});
Expand Down
22 changes: 10 additions & 12 deletions packages/gasket-plugin-docs/test/docs-setup.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
const assume = require('assume');
const docsSetup = require('../lib/docs-setup');

describe('docsSetup', () => {

it('returns object', () => {
const results = docsSetup();
assume(results).is.an('object');
expect(results).toBeInstanceOf(Object);
});

it('includes link', () => {
const results = docsSetup();
assume(results).property('link');
expect(results).toHaveProperty('link');
});

it('includes files', () => {
const results = docsSetup();
assume(results).property('files');
assume(results.files).an('array');
expect(results).toHaveProperty('files');
expect(results.files).toBeInstanceOf(Array);
});

it('includes specific files', () => {
const results = docsSetup();
assume(results.files).lengthOf(3);
assume(results.files)
.includes('README.md')
.includes('docs/**/*')
.includes('LICENSE.md');
expect(results.files).toHaveLength(3);
expect(results.files).toContain('README.md');
expect(results.files).toContain('docs/**/*');
expect(results.files).toContain('LICENSE.md');
});

it('includes expected transforms', () => {
Expand All @@ -36,8 +34,8 @@ describe('docsSetup', () => {
} = require('../lib/utils/transforms');

const results = docsSetup();
assume(results).property('transforms');
assume(results.transforms).eqls([
expect(results).toHaveProperty('transforms');
expect(results.transforms).toEqual([
txGasketPackageLinks, txGasketUrlLinks, txAbsoluteLinks
]);
});
Expand Down
45 changes: 20 additions & 25 deletions packages/gasket-plugin-docs/test/get-commands.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const assume = require('assume');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const getCommands = require('../lib/get-commands');
const buildDocsConfigSet = require('../lib/utils/build-config-set');
const collateFiles = require('../lib/utils/collate-files');
const generateIndex = require('../lib/utils/generate-index');

const mockGasket = {
exec: sinon.stub()
exec: jest.fn()
};

class MockGasketCommand {
Expand All @@ -12,78 +13,72 @@ class MockGasketCommand {
}
}

const mockData = { GasketCommand: MockGasketCommand, flags: { boolean: sinon.stub() } };
const mockData = { GasketCommand: MockGasketCommand, flags: { boolean: jest.fn() } };
const mockDocsConfigSet = {
docsRoot: '/path/to/app/.docs',
guides: []
};

const buildConfigSetStub = sinon.stub().resolves(mockDocsConfigSet);
const collateFilesStub = sinon.stub();
const generateIndexStub = sinon.stub();

const getCommands = proxyquire('../lib/get-commands', {
'./utils/build-config-set': buildConfigSetStub,
'./utils/collate-files': collateFilesStub,
'./utils/generate-index': generateIndexStub
});
jest.mock('../lib/utils/build-config-set', () => jest.fn(() => Promise.resolve(mockDocsConfigSet)));
jest.mock('../lib/utils/collate-files');
jest.mock('../lib/utils/generate-index');

describe('getCommands', () => {

it('returns a command', () => {
const results = getCommands(mockGasket, mockData);
assume(results.prototype).instanceOf(MockGasketCommand);
expect(results.prototype).toBeInstanceOf(MockGasketCommand);
});

it('command has id', () => {
const results = getCommands(mockGasket, mockData);
assume(results).property('id', 'docs');
expect(results).toHaveProperty('id', 'docs');
});

it('command has description', () => {
const results = getCommands(mockGasket, mockData);
assume(results).property('description');
expect(results).toHaveProperty('description');
});

it('command implements gasketRun', () => {
const results = getCommands(mockGasket, mockData);
assume(results.prototype).property('gasketRun');
expect(results.prototype).toHaveProperty('gasketRun');
});

describe('instance', () => {
const DocsCommand = getCommands(mockGasket, mockData);
const instance = new DocsCommand();

beforeEach(() => {
sinon.resetHistory();
instance.parsed = { flags: { view: true } };
jest.clearAllMocks();
});

it('builds docsConfigSet', async () => {
await instance.gasketRun();
assume(buildConfigSetStub).calledWith(mockGasket);
expect(buildDocsConfigSet).toHaveBeenCalledWith(mockGasket);
});

it('collates files', async () => {
await instance.gasketRun();
assume(collateFilesStub).calledWith(mockDocsConfigSet);
expect(collateFiles).toHaveBeenCalledWith(mockDocsConfigSet);
});

it('generates index', async () => {
await instance.gasketRun();
assume(generateIndexStub).calledWith(mockDocsConfigSet);
expect(generateIndex).toHaveBeenCalledWith(mockDocsConfigSet);
});

it('executes docsView lifecycle', async () => {
await instance.gasketRun();
assume(mockGasket.exec).calledWith('docsView', mockDocsConfigSet);
expect(mockGasket.exec).toHaveBeenCalledWith('docsView', mockDocsConfigSet);
});

it('does not execute docsView if --no-view flag', async () => {
instance.parsed.flags.view = false;
await instance.gasketRun();
assume(mockGasket.exec.callCount).equals(1);
assume(mockGasket.exec.calledWith('docsView')).is.false();
expect(mockGasket.exec).toHaveBeenCalledTimes(1);
expect(mockGasket.exec).toHaveBeenCalledWith('docsGenerate', mockDocsConfigSet);
});
});
});
11 changes: 5 additions & 6 deletions packages/gasket-plugin-docs/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const plugin = require('../');
const assume = require('assume');

describe('Plugin', function () {

it('is an object', () => {
assume(plugin).is.an('object');
expect(plugin).toBeInstanceOf(Object);
});

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

it('has expected hooks', () => {
Expand All @@ -19,10 +18,10 @@ describe('Plugin', function () {
'docsSetup'
];

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.length).toBe(expected.length);
});
});
45 changes: 22 additions & 23 deletions packages/gasket-plugin-docs/test/metadata.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const assume = require('assume');
const metadata = require('../lib/metadata');
const { defaultConfig } = require('../lib/configure');

Expand All @@ -17,48 +16,48 @@ describe('metadata', () => {

it('returns object', () => {
const results = metadata(mockGasket, mockMeta);
assume(results).is.an('object');
expect(results).toBeInstanceOf(Object);
});

it('includes initial meta', () => {
const results = metadata(mockGasket, mockMeta);
assume(results).property('bogus', true);
expect(results).toHaveProperty('bogus', true);
});

it('includes commands', () => {
const results = metadata(mockGasket, mockMeta);
assume(results.commands).lengthOf(1);
assume(results.commands[0]).property('name', 'docs');
expect(results.commands).toHaveLength(1);
expect(results.commands[0]).toHaveProperty('name', 'docs');
});

it('includes lifecycles', () => {
const results = metadata(mockGasket, mockMeta);
assume(results.lifecycles).lengthOf(3);
assume(results.lifecycles[0]).property('name', 'docsSetup');
assume(results.lifecycles[0]).property('link');
assume(results.lifecycles[0]).property('description');
assume(results.lifecycles[0]).property('command');
assume(results.lifecycles[1]).property('name', 'docsView');
assume(results.lifecycles[1]).property('link');
assume(results.lifecycles[1]).property('description');
assume(results.lifecycles[1]).property('command');
assume(results.lifecycles[2]).property('name', 'docsGenerate');
assume(results.lifecycles[2]).property('link');
assume(results.lifecycles[2]).property('description');
assume(results.lifecycles[2]).property('command');
expect(results.lifecycles).toHaveLength(3);
expect(results.lifecycles[0]).toHaveProperty('name', 'docsSetup');
expect(results.lifecycles[0]).toHaveProperty('link');
expect(results.lifecycles[0]).toHaveProperty('description');
expect(results.lifecycles[0]).toHaveProperty('command');
expect(results.lifecycles[1]).toHaveProperty('name', 'docsView');
expect(results.lifecycles[1]).toHaveProperty('link');
expect(results.lifecycles[1]).toHaveProperty('description');
expect(results.lifecycles[1]).toHaveProperty('command');
expect(results.lifecycles[2]).toHaveProperty('name', 'docsGenerate');
expect(results.lifecycles[2]).toHaveProperty('link');
expect(results.lifecycles[2]).toHaveProperty('description');
expect(results.lifecycles[2]).toHaveProperty('command');
});

it('includes structures with configured outputDir', () => {
const results = metadata(mockGasket, mockMeta);
assume(results.structures).lengthOf(1);
assume(results.structures[0]).property('name', mockGasket.config.docs.outputDir + '/');
assume(results.structures[0]).property('description');
expect(results.structures).toHaveLength(1);
expect(results.structures[0]).toHaveProperty('name', mockGasket.config.docs.outputDir + '/');
expect(results.structures[0]).toHaveProperty('description');
});

it('falls back to defaults if not configured', () => {
delete mockGasket.config.docs;
const results = metadata(mockGasket, mockMeta);
assume(results).is.an('object');
assume(results.structures[0]).property('name', defaultConfig.outputDir + '/');
expect(results).toBeInstanceOf(Object);
expect(results.structures[0]).toHaveProperty('name', defaultConfig.outputDir + '/');
});
});
4 changes: 0 additions & 4 deletions packages/gasket-plugin-docs/test/setup.js

This file was deleted.

Loading

0 comments on commit 427f73a

Please sign in to comment.