UNPKG

jmd-scripts

Version:

CLI for common scripts for my projects

94 lines (86 loc) 2.35 kB
"use strict"; var _jestInCase = _interopRequireDefault(require("jest-in-case")); var _serializers = require("./helpers/serializers"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } jest.mock('jest', () => ({ run: jest.fn() })); jest.mock('../../config/jest.config', () => ({ builtInConfig: true })); let mockIsCI = false; jest.mock('is-ci', () => mockIsCI); expect.addSnapshotSerializer(_serializers.unquoteSerializer); (0, _jestInCase.default)('test', ({ args = [], utils = require('../../utils'), pkgHasJestProp = false, hasJestConfigFile = false, setup = () => () => {}, ci = false, precommit = 'false' }) => { // beforeEach // eslint-disable-next-line jest/no-jest-import const { run: jestRunMock } = require('jest'); const originalArgv = process.argv; const prevCI = mockIsCI; const prevPrecommit = process.env.SCRIPTS_PRECOMMIT; mockIsCI = ci; process.env.SCRIPTS_PRECOMMIT = precommit; Object.assign(utils, { hasPkgProp: () => pkgHasJestProp, hasFile: () => hasJestConfigFile }); process.exit = jest.fn(); const teardown = setup(); process.argv = ['node', '../test', ...args]; jestRunMock.mockClear(); try { // tests require('../test'); expect(jestRunMock).toHaveBeenCalledTimes(1); const [firstCall] = jestRunMock.mock.calls; const [jestArgs] = firstCall; expect(jestArgs.join(' ')).toMatchSnapshot(); } catch (error) { throw error; } finally { teardown(); // afterEach process.argv = originalArgv; mockIsCI = prevCI; process.env.SCRIPTS_PRECOMMIT = prevPrecommit; jest.resetModules(); } }, { 'calls jest.run with default args': {}, 'does not watch on CI': { ci: true }, 'does not watch on SCRIPTS_PRECOMMIT': { precommit: 'true' }, 'does not watch with --no-watch': { args: ['--no-watch'] }, 'does not watch with --coverage': { args: ['--coverage'] }, 'does not watch --updateSnapshot': { args: ['--updateSnapshot'] }, 'uses custom config with --config': { args: ['--config', './my-config.js'] }, 'uses custom config with jest prop in pkg': { pkgHasJestProp: true }, 'uses custom config with jest.config.js file': { hasJestConfigFile: true }, 'forwards args': { args: ['--coverage', '--watch'] } });