@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
127 lines (113 loc) • 3.6 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _path = _interopRequireDefault(require("path"));
var _slash = _interopRequireDefault(require("slash"));
var _serializers = require("./scripts/__tests__/helpers/serializers");
const MAX_CALL_THRESHOLD = 1;
const projectRoot = _path.default.join(__dirname, '../');
expect.addSnapshotSerializer(_serializers.unquoteSerializer);
expect.addSnapshotSerializer({
print: val => (0, _slash.default)(val.replace(projectRoot, '<PROJECT_ROOT>/')),
test: val => typeof val === 'string' && val.includes(projectRoot)
});
let crossSpawnSyncMock, originalArgv, originalLog, originalExit, originalVersion;
describe('run scripts', () => {
beforeEach(() => {
({
sync: crossSpawnSyncMock
} = require('cross-spawn'));
originalLog = console.log;
console.log = jest.fn();
originalExit = process.exit;
process.exit = jest.fn();
originalArgv = process.argv;
originalVersion = process.version;
});
afterEach(() => {
console.log = originalLog;
process.argv = originalArgv;
Object.defineProperty(process, 'version', {
value: originalVersion
});
process.exit = originalExit;
jest.resetModules();
});
it.each`
title | params
${'calls node with the script path and args'} | ${{
args: ['test', '--no-watch']
}}
${'throws unknown script'} | ${{
args: ['unknown-script'],
throws: true
}}
${'logs help with no args'} | ${{
snapshotLog: true
}}
${'logs for SIGKILL signal'} | ${{
args: ['lint'],
signal: 'SIGKILL'
}}
${'logs for SIGTERM signal'} | ${{
args: ['build'],
signal: 'SIGTERM'
}}
${'does not log for other signals'} | ${{
args: ['test'],
signal: 'SIGBREAK'
}}
`('$title', ({
params = {}
}) => {
const {
snapshotLog = false,
throws = false,
signal = false,
args = []
} = params;
try {
process.argv = ['node', '.', ...args];
crossSpawnSyncMock.mockClear();
if (signal) {
crossSpawnSyncMock.mockReturnValueOnce({
result: 1,
signal
});
}
require('.');
if (snapshotLog) {
expect(console.log.mock.calls).toMatchSnapshot();
return;
}
if (signal) {
expect(process.exit).toHaveBeenCalledTimes(MAX_CALL_THRESHOLD);
expect(process.exit).toHaveBeenCalledWith(MAX_CALL_THRESHOLD);
expect(console.log.mock.calls).toMatchSnapshot();
return;
}
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(MAX_CALL_THRESHOLD);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [script, calledArgs] = firstCall;
expect([script, ...calledArgs].join(' ')).toMatchSnapshot();
} catch (error) {
if (throws) expect(error.message).toMatchSnapshot();else throw error;
}
});
it('should throw an error if node version is lower than supported and dogfood self', () => {
jest.mock('./utils', () => (0, _extends2.default)({}, jest.requireActual('./utils'), {
pkg: {
name: '@interaktiv/dia-scripts',
engines: {
node: '>=8'
}
}
}));
Object.defineProperty(process, 'version', {
value: '7.0.0'
});
expect(() => {
require('.');
}).toThrow(/You must use Node version/);
});
});