@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
61 lines (53 loc) • 2.37 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _serializers = require("./__tests__/helpers/serializers");
expect.addSnapshotSerializer(_serializers.unquoteSerializer);
expect.addSnapshotSerializer(_serializers.winPathSerializer);
const originalExit = process.exit;
const originalArgv = process.argv;
let crossSpawnSyncMock;
describe('format', () => {
beforeEach(() => {
({
sync: crossSpawnSyncMock
} = require('cross-spawn'));
jest.mock('../utils', () => (0, _extends2.default)({}, jest.requireActual('../utils'), {
resolveBin: jest.fn((modName, {
executable = modName
} = {}) => executable)
}));
process.exit = jest.fn();
});
afterEach(() => {
process.exit = originalExit;
process.argv = originalArgv;
jest.resetModules();
});
it.each`
title | args
${'calls prettier with args'} | ${['my-src/**/*.js']}
${'should not add --write with --no-write argument'} | ${['--no-write']}
${'should use a custom config with --config argument'} | ${['--config', './my-config.js']}
${'should use a custom ignore file with --ignore-path argument'} | ${['--ignore-path', './.myignore']}
`('$title', ({
args
}) => {
// Before each
const utils = require('../utils');
const hasFileOrig = utils.hasFile; // Cause we are testing in the root of this project here and a
// .prettierignore for the project is given, we need to ignore the
// availability of our projects .prettierignore. Otherwise we get wrong
// snapshots for the cli calls within our tests.
// TODO: Move tests into a temp dir or mock the appDirectory to remove the workaround
Object.assign(utils, {
hasFile: filename => /prettier/i.test(filename) ? false : hasFileOrig(filename)
});
process.argv = ['node', './format', ...args];
require('./format');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [script, calledArgs] = firstCall;
expect([script, ...calledArgs].join(' ').replace(process.cwd(), '/blah-root')).toMatchSnapshot();
});
});