UNPKG

@interaktiv/dia-scripts

Version:

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

218 lines (177 loc) 6.21 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _path = _interopRequireDefault(require("path")); var _fsExtra = _interopRequireDefault(require("fs-extra")); var _yargsParser = _interopRequireDefault(require("yargs-parser")); var _tempy = _interopRequireDefault(require("tempy")); var _rimraf = _interopRequireDefault(require("rimraf")); var _serializers = require("../__tests__/helpers/serializers"); expect.addSnapshotSerializer(_serializers.unquoteSerializer); expect.addSnapshotSerializer(_serializers.winPathSerializer); jest.mock('./runCssCompile', () => jest.fn()); jest.mock('./runDeploy', () => ({ runDeploy: jest.fn() })); const originalExit = process.exit; const originalArgv = process.argv; let originalConsoleInfo, originalConsoleLog; const fromHere = (...p) => _path.default.join(__dirname, ...p); const waitRace = spies => { if (spies.length === 0) throw new TypeError('SPies zero'); return new Promise(resolve => { const isSpyReady = spy => { if (Array.isArray(spy)) { return spy[0].callCount >= spy[1]; } return spy.callCount >= 1; }; let intrvl, timeo; function finish() { clearInterval(intrvl); clearTimeout(timeo); resolve(); } intrvl = setInterval(() => { if (spies.some(isSpyReady)) finish(); }, 20); timeo = setTimeout(finish, 3500); }); }; describe('watch', () => { const originFixtures = fromHere('../__tests__/fixtures/watch'); let sourceFixtures; let allWatchers = []; let allFixtures = []; beforeEach(() => { originalConsoleInfo = console.info; originalConsoleLog = console.log; console.info = jest.fn(); console.log = jest.fn(); process.exit = jest.fn(); const fixture = _tempy.default.directory(); _fsExtra.default.ensureDirSync(fixture); _fsExtra.default.copySync(originFixtures, fixture); allFixtures = [...allFixtures, fixture]; sourceFixtures = _fsExtra.default.realpathSync(_path.default.join(fixture, 'src')); }); afterEach(() => { process.exit = originalExit; process.argv = originalArgv; console.info = originalConsoleInfo; console.log = originalConsoleLog; jest.resetModules(); // Clean up watchers if (Array.isArray(allWatchers)) { const closeWatchers = () => { const watcher = allWatchers.pop(); watcher.close(); if (allWatchers.length) closeWatchers(); }; closeWatchers(); } // Clean up fixtures if (Array.isArray(allFixtures)) { const removeFixtures = () => { const fixture = allFixtures.pop(); _rimraf.default.sync(fixture); if (allFixtures.length) removeFixtures(); }; removeFixtures(); } }); describe('file changes', () => { // TODO: Add tests for ignore file it.each` title | args ${'should run deployment for Javascript file'} | ${{ file: 'index.js' }} ${'should run deployment for HTML file'} | ${{ file: 'index.html' }} ${'should run deployment for XML file'} | ${{ file: 'index.xml' }} ${'should run deployment for Apex Class (.cls) file'} | ${{ file: 'index.cls' }} ${'should run deployment for Apex Class (.apex) file'} | ${{ file: 'index.apex' }} ${'should run deployment for Apex Trigger (.trg) file'} | ${{ file: 'index.trg' }} ${'should not run deployment for .txt file'} | ${{ file: 'no-change-trigger.txt', noChange: true }} `('$title', async ({ args }) => { const { runDeploy } = require('./runDeploy'); const watch = require('./watch'); const { file = '', noChange = false } = args; const src = _path.default.join(sourceFixtures, file); const opts = ['--source-dir', sourceFixtures, '--watch']; process.argv = ['node', './watch', ...opts]; const rawArgv = process.argv.slice(2); const argv = (0, _yargsParser.default)(rawArgv); runDeploy.mockReturnValue({ status: 0 }); const watcherList = await watch({ argv }); allWatchers = [...allWatchers, ...watcherList]; const changeSpies = watcherList.map(w => { const spy = jest.fn(); w.on('change', spy); return spy; }); _fsExtra.default.appendFileSync(src, 'Here comes the great new change'); if (noChange === false) await waitRace([changeSpies]); expect(runDeploy).toHaveBeenCalledTimes(noChange ? 0 : 1); }); it('should run CSS compiler first than deployment for .scss file', async () => { jest.setTimeout(30 * 1000); const { runDeploy } = require('./runDeploy'); const runCssCompile = require('./runCssCompile'); const watch = require('./watch'); const src = _path.default.join(sourceFixtures, 'index.scss'); const opts = ['--source-dir', sourceFixtures]; process.argv = ['node', './watch', ...opts]; const rawArgv = process.argv.slice(2); const argv = (0, _yargsParser.default)(rawArgv); runDeploy.mockReturnValue({ status: 0 }); const cssFile = src.replace('.scss', '.css'); runCssCompile.mockResolvedValue(cssFile); const watcherList = await watch({ argv }); allWatchers = [...allWatchers, ...watcherList]; const changeSpies = watcherList.map(w => { const changeMock = jest.fn(); w.on('change', changeMock); return changeMock; }); const addSpies = watcherList.map(w => { const addMock = jest.fn(); w.on('add', addMock); return addMock; }); _fsExtra.default.appendFileSync(src, 'Here comes the great new change'); await waitRace(changeSpies); _fsExtra.default.outputFileSync(cssFile, 'Here comes the great transpiled css'); await waitRace(addSpies); expect(runCssCompile).toHaveBeenCalledTimes(1); expect(runDeploy).toHaveBeenCalledTimes(1); }); }); });