@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
98 lines (90 loc) • 2.98 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _serializers = require("../__tests__/helpers/serializers");
jest.mock('../../config/babelrc', () => ({
builtInConfig: true
}));
jest.mock('../../utils.js', () => (0, _extends2.default)({}, jest.requireActual('../../utils'), {
resolveBin: (modName, {
executable = modName
} = {}) => executable,
cleanDir: jest.fn()
}));
expect.addSnapshotSerializer(_serializers.unquoteSerializer);
let originalArgv, originalExit, crossSpawnSyncMock;
describe('build babel', () => {
beforeEach(() => {
({
sync: crossSpawnSyncMock
} = require('cross-spawn'));
originalArgv = process.argv;
originalExit = process.exit;
process.exit = jest.fn();
});
afterEach(() => {
process.argv = originalArgv;
process.exit = originalExit;
jest.resetModules();
});
it.each`
title | params
${'calls babel with default args'} | ${{}}
${'does not copy files with --no-copy-files'} | ${{
args: ['--no-copy-files']
}}
${'uses custom config with --presets'} | ${{
args: ['--presets', './my-config.js']
}}
${'uses custom ignores with --ignore'} | ${{
args: ['--ignore', 'my-first-ignore,my-second-ignore']
}}
${'uses custom output dir with --out-dir'} | ${{
args: ['--out-dir', '/my-custom-out-dir'],
noClean: true
}}
${'does not cleans out dir with --no-clean'} | ${{
args: ['--no-clean'],
noClean: true
}}
${'uses custom config with babel prop in pkg'} | ${{
pkgHasBabelProp: true
}}
${'uses custom config with .babelrc file'} | ${{
hasFile: '.babelrc'
}}
${'uses custom config with .babelrc.js file'} | ${{
hasFile: '.babelrc.js'
}}
${'uses custom config with babel.config.js file'} | ${{
hasFile: 'babel.config.js'
}}
${'forwards args'} | ${{
args: ['--my-fancy-arg', '--mycustomarg']
}}
`('$title', ({
params
}) => {
const {
args = [],
pkgHasBabelProp = false,
hasFile = '',
noClean = false
} = params;
const utils = require('../../utils');
const {
cleanDir: cleanDirMock
} = utils;
Object.assign(utils, {
hasPkgProp: () => pkgHasBabelProp,
hasFile: file => file === hasFile
});
process.argv = ['node', './babel', ...args];
require('./babel');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1);
if (noClean) expect(cleanDirMock).toHaveBeenCalledTimes(0);else expect(cleanDirMock).toHaveBeenCalledTimes(1);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [script, jestArgs] = firstCall;
expect([script, ...jestArgs].join(' ').replace(process.cwd(), '/blah-root')).toMatchSnapshot();
});
});