Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit and integration tests #67

Merged
merged 14 commits into from
May 19, 2023
Merged
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ orbs:
jobs:
notify_slack:
docker:
- image: cimg/node:17.2.0
- image: cimg/node:18.12.0
steps:
- run:
command: |
curl -X POST -H 'Content-type: application/json' --data '{"text":"New Build Tools Release","blocks":[{"type":"header","text":{"type":"plain_text","text":"New Build Tools Release"}},{"type":"section","fields":[{"type":"mrkdwn","text":"*Version:* <https://github.com/'${CIRCLE_PROJECT_USERNAME}'/'${CIRCLE_PROJECT_REPONAME}'/releases/tag/'${CIRCLE_TAG}'|'${CIRCLE_TAG}'>"},{"type":"mrkdwn","text":"<https://github.com/'${CIRCLE_PROJECT_USERNAME}'/'${CIRCLE_PROJECT_REPONAME}'/issues|Issues> *|* <https://github.com/'${CIRCLE_PROJECT_USERNAME}'/'${CIRCLE_PROJECT_REPONAME}'/pulls|Pull Requests>"}]}]}' ${SLACK_WEBHOOK}
name: Send Slack update to channel
install_tests:
docker:
- image: cimg/node:17.2.0
- image: cimg/node:18.12.0
steps:
- checkout
- restore_cache:
Expand Down Expand Up @@ -50,7 +50,7 @@ jobs:
- .
self_test:
docker:
- image: cimg/node:17.2.0
- image: cimg/node:18.12.0
steps:
- checkout
- node/install-packages:
Expand Down
207 changes: 207 additions & 0 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
'use strict';
const path = require('path');
const mockFs = require('mock-fs');

jest.mock('ora', () => () => ({
start: jest.fn(),
}));

const requiredRealDirs = {
node_modules: mockFs.load(path.resolve(__dirname, '../node_modules')),
src: mockFs.load(path.resolve(__dirname, '../src')),
configs: mockFs.load(path.resolve(__dirname, '../configs')),
};

describe('CLI Build Command', () => {
let originalArgv;
let originalWrite;
let originalExit;

let mockWebpack;

beforeEach(() => {
jest.resetModules();

originalArgv = process.argv;
originalWrite = process.stdout.write;
originalExit = process.exit;

process.stdout.write = jest.fn();
process.exit = jest.fn();

mockWebpack = jest.fn().mockImplementation((config) => {
return {
run: jest.fn(),
watch: jest.fn(),
};
});
jest.mock('webpack', () => {
const webpack = mockWebpack;
webpack.DefinePlugin = jest.fn().mockImplementation((params) => params);
return webpack;
});
});

afterEach(() => {
process.argv = originalArgv;
process.stdout.write = originalWrite;
process.exit = originalExit;

mockFs.restore();
jest.resetAllMocks();
});

it('can read global vars', () => {
mockFs({
...requiredRealDirs,
'src/entrypoints': {
'some-file.js': 'console.log("file content here");',
'empty-dir': {
/** empty directory */
},
},
'package.json': JSON.stringify({
name: 'test-project',
}),
});

runCommand('build', '--once');

const packagePath = process.cwd() + '/package.json';

expect(global.targetDirs).toEqual(['client-mu-plugins', 'plugins', 'themes']);
expect(global.packageList).toEqual({
[packagePath]: {
absolutePath: packagePath,
json: { name: 'test-project' },
name: 'test-project',
path: process.cwd(),
relativePath: 'build-tools/package.json',
},
});
});

it('detects single project mode based on filesystem', () => {
mockFs({
...requiredRealDirs,
'src/entrypoints': {
'some-file.js': 'console.log("file content here");',
'empty-dir': {
/** empty directory */
},
},
'package.json': JSON.stringify({
name: 'test-project',
}),
});

runCommand('build', '--once');

expect(mockWebpack).toHaveBeenCalled();
expect(process.stdout.write).toHaveBeenCalledWith(
`\x1b[1mCompiling \x1b[4msingle\x1b[0m\x1b[1m project in development mode.\x1b[0m\n`,
);
});

it('detects all projects mode based on filesystem', () => {
mockFs({
...requiredRealDirs,
plugins: {
'my-plugin': {
'package.json': JSON.stringify({
name: 'my-plugin',
}),
src: {
entrypoints: {
'some-file.js': 'console.log("file content here");',
},
},
},
},
});

runCommand('build', '--once');

expect(mockWebpack).toHaveBeenCalled();
expect(process.stdout.write).toHaveBeenCalledWith(
`\x1b[1mCompiling \x1b[4mall\x1b[0m\x1b[1m projects in development mode.\x1b[0m\n`,
);
});

it('runs specific projects mode when requested', () => {
mockFs({
...requiredRealDirs,
plugins: {
'my-plugin': {
'package.json': JSON.stringify({
name: 'my-plugin',
}),
src: {
entrypoints: {
'some-file.js': 'console.log("file content here");',
},
},
},
},
themes: {
'my-theme': {
'package.json': JSON.stringify({
name: 'my-theme',
}),
src: {
entrypoints: {
'some-file.js': 'console.log("file content here");',
},
},
},
},
});

runCommand('build', '--once', 'my-plugin,my-theme');

expect(mockWebpack).toHaveBeenCalled();
expect(process.stdout.write).toHaveBeenCalledWith(
`\x1b[1mCompiling \x1b[4mlist\x1b[0m\x1b[1m of projects in development mode.\x1b[0m\n`,
);
expect(process.stdout.write).toHaveBeenCalledWith('Processing the following projects:\n');
expect(process.stdout.write).toHaveBeenCalledWith(` * my-plugin `);
expect(process.stdout.write).toHaveBeenCalledWith(` * my-theme `);
});

it('fails to run specific projects mode if a project is not found', () => {
mockFs({
...requiredRealDirs,
plugins: {
'my-plugin': {
'package.json': JSON.stringify({
name: 'my-plugin',
}),
src: {
entrypoints: {
'some-file.js': 'console.log("file content here");',
},
},
},
},
});

runCommand('build', '--once', 'my-plugin,my-theme');

expect(mockWebpack).toHaveBeenCalled();
expect(process.stdout.write).toHaveBeenCalledWith(
`\x1b[1mCompiling \x1b[4mlist\x1b[0m\x1b[1m of projects in development mode.\x1b[0m\n`,
);
expect(process.stdout.write).toHaveBeenCalledWith('Processing the following projects:\n');
expect(process.stdout.write).toHaveBeenCalledWith(`Error: Project my-theme does not exist.`);
expect(process.exit).toHaveBeenCalledWith(1);
});
});

async function runCommand(...args) {
process.argv = [
'node', // Not used but a value is required at this index in the array
'cli.js', // Not used but a value is required at this index in the array
...args,
];
return require('../src/cli.js');
}
Loading