UNPKG

@interaktiv/dia-scripts

Version:

CLI toolbox with common scripts for most sort of projects at DIA

128 lines (115 loc) 4.79 kB
"use strict"; 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); let crossSpawnSyncMock, originalArgv, originalExit, originalConsoleLog, originalPreCommit; // eslint-disable-next-line max-lines-per-function describe('run setup', () => { beforeEach(() => { ({ sync: crossSpawnSyncMock } = require('cross-spawn')); originalConsoleLog = console.log; originalExit = process.exit; originalArgv = process.argv; originalPreCommit = process.env['SCRIPTS_PRE-COMMIT']; console.log = jest.fn(); process.exit = jest.fn(); process.env['SCRIPTS_PRE-COMMIT'] = 'false'; }); afterEach(() => { console.log = originalConsoleLog; process.exit = originalExit; process.argv = originalArgv; process.env['SCRIPTS_PRE-COMMIT'] = originalPreCommit; jest.resetModules(); }); describe('when in Salesforce DX project', () => { beforeEach(() => { jest.mock('../../utils', () => (0, _extends2.default)({}, jest.requireActual('../../utils.js'), { ifSfdxProject: jest.fn(t => t) })); }); afterEach(() => { jest.resetModules(); }); it('uses pmd task', () => { process.argv = ['node', '../setup', ...[]]; require('.'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(3); const [firstCall, secondCall, thirdCall] = crossSpawnSyncMock.mock.calls; const [script, calledArgs] = firstCall; expect([script, ...calledArgs].join(' ')).toMatchSnapshot(); const [scriptTwo, calledArgsTwo] = secondCall; expect([scriptTwo, ...calledArgsTwo].join(' ')).toMatchSnapshot(); const [scriptThree, calledArgsThree] = thirdCall; expect([scriptThree, ...calledArgsThree].join(' ')).toMatchSnapshot(); }); }); describe('when in Titanium Mobile project', () => { beforeEach(() => { jest.mock('../../utils', () => (0, _extends2.default)({}, jest.requireActual('../../utils.js'), { ifSfdxProject: jest.fn((t, f) => f), ifTitaniumProject: jest.fn(t => t) })); }); afterEach(() => { jest.resetModules(); }); it('uses titanium task', () => { process.argv = ['node', '../setup', ...[]]; require('.'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(3); const [firstCall, secondCall, thirdCall] = crossSpawnSyncMock.mock.calls; const [script, calledArgs] = firstCall; expect([script, ...calledArgs].join(' ')).toMatchSnapshot(); const [scriptTwo, calledArgsTwo] = secondCall; expect([scriptTwo, ...calledArgsTwo].join(' ')).toMatchSnapshot(); const [scriptThree, calledArgsThree] = thirdCall; expect([scriptThree, ...calledArgsThree].join(' ')).toMatchSnapshot(); }); }); describe('when not in Titanium or Salesforce DX project', () => { beforeEach(() => { jest.mock('../../utils', () => (0, _extends2.default)({}, jest.requireActual('../../utils.js'), { ifSfdxProject: jest.fn((t, f) => f), ifTitaniumProject: jest.fn((t, f) => f) })); originalPreCommit = process.env['SCRIPTS_PRE-COMMIT']; }); afterEach(() => { process.env['SCRIPTS_PRE-COMMIT'] = originalPreCommit; jest.resetModules(); }); it('does not run pmd and titanium tasks', () => { process.argv = ['node', '../setup', ...[]]; require('.'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(2); const [firstCall, secondCall] = crossSpawnSyncMock.mock.calls; const [script, calledArgs] = firstCall; expect([script, ...calledArgs].join(' ')).toMatchSnapshot(); const [scriptTwo, calledArgsTwo] = secondCall; expect([scriptTwo, ...calledArgsTwo].join(' ')).toMatchSnapshot(); }); it('does not run when on pre-commit hook', () => { process.env['SCRIPTS_PRE-COMMIT'] = 'true'; process.argv = ['node', '../setup', ...[]]; expect(() => { require('.'); }).toThrow(/not allowed/); }); }); it('does run specific setup script if given', () => { jest.mock('../../utils', () => (0, _extends2.default)({}, jest.requireActual('../../utils.js'), { ifSfdxProject: jest.fn((t, f) => f), ifTitaniumProject: jest.fn((t, f) => f) })); process.argv = ['node', '../setup', ...['titanium']]; require('.'); expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1); const [firstCall] = crossSpawnSyncMock.mock.calls; const [script, calledArgs] = firstCall; expect([script, ...calledArgs].join(' ')).toMatchSnapshot(); jest.resetModules(); }); });