@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
141 lines (122 loc) • 5.07 kB
JavaScript
;
var _serializers = require("./__tests__/helpers/serializers");
expect.addSnapshotSerializer(_serializers.unquoteSerializer);
function setupWithScripts(scripts = ['test', 'lint', 'build', 'flow', 'format']) {
return function () {
const utils = require('../utils');
const originalIfScript = utils.ifScript;
utils.ifScript = (script, t, f) => scripts.includes(script) ? t : f;
return function () {
utils.ifScript = originalIfScript;
};
};
}
function setupWithArgs(args = []) {
return function () {
const utils = require('../utils');
const originalResolveBin = utils.resolveBin;
const originalArgv = process.argv;
utils.resolveBin = (modName, {
executable = modName
} = {}) => executable;
process.argv = ['node', '../validate', ...args];
return function () {
process.argv = originalArgv;
utils.resolveBin = originalResolveBin;
};
};
}
function setupWithArgsAndScripts(args = [], scripts = []) {
return function () {
const teardownArgs = setupWithArgs(args)();
const teardownScripts = setupWithScripts(scripts)();
return function () {
teardownArgs();
teardownScripts();
};
};
}
function withDefaultSetup(setupFn) {
return function () {
const utils = require('../utils');
utils.resolveBin = (modName, {
executable = modName
} = {}) => executable;
const argsTeardown = setupWithArgs()();
const teardownScripts = setupWithScripts()();
const teardownFn = setupFn();
return function () {
argsTeardown();
teardownFn();
teardownScripts();
};
};
}
const originalExit = process.exit;
let crossSpawnSyncMock, originalConsoleLog;
describe('run validate', () => {
beforeEach(() => {
({
sync: crossSpawnSyncMock
} = require('cross-spawn'));
originalConsoleLog = console.log;
console.log = jest.fn();
process.exit = jest.fn();
});
afterEach(() => {
console.log = originalConsoleLog;
process.exit = originalExit;
jest.resetModules();
});
describe('when on PRE-COMMIT', () => {
const originalValue = process.env['SCRIPTS_PRE-COMMIT'];
beforeEach(() => {
process.env['SCRIPTS_PRE-COMMIT'] = 'true';
});
afterEach(() => {
process.env['SCRIPTS_PRE-COMMIT'] = originalValue;
});
it('should not use test or lint', () => {
const setup = withDefaultSetup(() => () => {});
const teardown = setup();
try {
require('./validate');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(3);
crossSpawnSyncMock.mock.calls.map(([script, args]) => [script, args]).forEach(([script, args]) => expect([script, ...args].join(' ')).toMatchSnapshot());
} finally {
teardown();
}
});
});
describe('when not on PRE-COMMIT', () => {
const originalValue = process.env['SCRIPTS_PRE-COMMIT'];
beforeEach(() => {
process.env['SCRIPTS_PRE-COMMIT'] = 'false';
});
afterEach(() => {
process.env['SCRIPTS_PRE-COMMIT'] = originalValue;
});
it.each`
title | setup | expectedCalls
${'calls concurrently with all scripts'} | ${withDefaultSetup(setupWithScripts())} | ${5}
${`does not include "lint" if it doesn't have that script`} | ${withDefaultSetup(setupWithScripts(['test', 'build', 'flow', 'format']))} | ${4}
${`does not include "test" if it doesn't have that script`} | ${withDefaultSetup(setupWithScripts(['lint', 'build', 'flow', 'format']))} | ${4}
${`does not include "build" if it doesn't have that script`} | ${withDefaultSetup(setupWithScripts(['test', 'lint', 'flow', 'format']))} | ${4}
${`does not include "flow" if it doesn't have that script`} | ${withDefaultSetup(setupWithScripts(['test', 'build', 'lint', 'format']))} | ${4}
${`does not include "format" if it doesn't have that script`} | ${withDefaultSetup(setupWithScripts(['test', 'build', 'lint', 'flow']))} | ${4}
${'allows you to specify your own npm scripts'} | ${setupWithArgsAndScripts(['specialbuild,specialtest,speciallint'], ['specialbuild', 'specialtest', 'speciallint'])} | ${3}
`('$title', ({
setup = () => () => {},
expectedCalls = 0
}) => {
const teardown = setup();
try {
require('./validate');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(expectedCalls);
crossSpawnSyncMock.mock.calls.forEach(([script, args]) => expect([script, ...args].join(' ')).toMatchSnapshot());
} finally {
teardown();
}
});
});
});