@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
102 lines (98 loc) • 3.48 kB
JavaScript
;
var _serializers = require("../__tests__/helpers/serializers");
jest.mock('../../config/jest.config', () => ({
builtInConfig: true
}));
let mockIsCI = false;
jest.mock('is-ci', () => mockIsCI);
expect.addSnapshotSerializer(_serializers.unquoteSerializer);
let originalArgv, originalExit, prevCI, prevPreCommit, crossSpawnSyncMock;
describe('test javascript', () => {
beforeEach(() => {
({
sync: crossSpawnSyncMock
} = require('cross-spawn'));
originalArgv = process.argv;
originalExit = process.exit;
prevPreCommit = process.env['SCRIPTS_PRE-COMMIT'];
prevCI = mockIsCI;
process.exit = jest.fn();
});
afterEach(() => {
process.argv = originalArgv;
process.exit = originalExit;
process.env['SCRIPTS_PRE-COMMIT'] = prevPreCommit;
mockIsCI = prevCI;
jest.resetModules();
});
it.each`
title | params
${'should call jest with default args'} | ${{}}
${'should not watch on CI'} | ${{
ci: true
}}
${'should not watch on SCRIPTS_PRE-COMMIT'} | ${{
preCommit: 'true'
}}
${'should not watch with --no-watch'} | ${{
args: ['--no-watch']
}}
${'should not watch with --coverage'} | ${{
args: ['--coverage']
}}
${'should not watch --updateSnapshot'} | ${{
args: ['--updateSnapshot']
}}
${'should use custom config with --config'} | ${{
args: ['--config', './my-config.js']
}}
${'should use custom config with jest prop in pkg'} | ${{
pkgHasJestProp: true
}}
${'should use custom config with jest.config.js file'} | ${{
hasJestConfigFile: true
}}
${'should forward args'} | ${{
args: ['--coverage', '--watch']
}}
${'should call lwc-jest if in Salesforce DX project'} | ${{
isSfdx: true
}}
${'should pass without tests if --force-tests provided'} | ${{
args: ['--force-tests']
}}
${'should pass without tests if --force-tests provided for Salesforce DX project'} | ${{
args: ['--force-tests'],
isSfdx: true
}}
`('$title', ({
params
}) => {
const {
args = [],
utils = require('../../utils'),
pkgHasJestProp = false,
hasJestConfigFile = false,
isSfdx = false,
ci = false,
preCommit = 'false'
} = params;
mockIsCI = ci;
process.env['SCRIPTS_PRE-COMMIT'] = preCommit;
Object.assign(utils, {
hasPkgProp: () => pkgHasJestProp,
hasFile: () => hasJestConfigFile,
ifSfdxProject: (t, f) => isSfdx ? t : f,
isSfdxProject: () => isSfdx,
resolveBin: (modName, {
executable = modName
} = {}) => executable
});
process.argv = ['node', './javascript', ...args];
require('./javascript');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [script, jestArgs] = firstCall;
expect([script, ...jestArgs].join(' ')).toMatchSnapshot();
});
});