|
| 1 | +import cases from 'jest-in-case' |
| 2 | +import {unquoteSerializer, winPathSerializer} from './helpers/serializers' |
| 3 | + |
| 4 | +expect.addSnapshotSerializer(unquoteSerializer) |
| 5 | +expect.addSnapshotSerializer(winPathSerializer) |
| 6 | + |
| 7 | +cases( |
| 8 | + 'commit-msg', |
| 9 | + ({ |
| 10 | + args = [], |
| 11 | + utils = require('../../utils'), |
| 12 | + hasFile = () => false, |
| 13 | + env = {}, |
| 14 | + }) => { |
| 15 | + // beforeEach |
| 16 | + const {sync: crossSpawnSyncMock} = require('cross-spawn') |
| 17 | + const originalArgv = process.argv |
| 18 | + const originalExit = process.exit |
| 19 | + const originalEnv = process.env |
| 20 | + |
| 21 | + Object.assign(utils, { |
| 22 | + hasFile, |
| 23 | + resolveBin: (modName, {executable = modName} = {}) => executable, |
| 24 | + }) |
| 25 | + |
| 26 | + process.exit = jest.fn() |
| 27 | + process.argv = ['node', '../commit-msg', ...args] |
| 28 | + process.env = env |
| 29 | + |
| 30 | + try { |
| 31 | + // tests |
| 32 | + require('../commit-msg') |
| 33 | + |
| 34 | + expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1) |
| 35 | + |
| 36 | + const [call] = crossSpawnSyncMock.mock.calls |
| 37 | + const [script, calledArgs] = call |
| 38 | + |
| 39 | + expect([script, ...calledArgs].join(' ')).toMatchSnapshot() |
| 40 | + } catch (error) { |
| 41 | + throw error |
| 42 | + } finally { |
| 43 | + // afterEach |
| 44 | + process.exit = originalExit |
| 45 | + process.argv = originalArgv |
| 46 | + process.env = originalEnv |
| 47 | + |
| 48 | + jest.resetModules() |
| 49 | + } |
| 50 | + }, |
| 51 | + { |
| 52 | + 'calls @commitlint/cli with default args': {}, |
| 53 | + 'does not use built-in config with --config': { |
| 54 | + args: ['--config', './custom-config.js'], |
| 55 | + }, |
| 56 | + 'does not use built-in config with commitlint.config.js file': { |
| 57 | + hasFile: filename => filename === 'commitlint.config.js', |
| 58 | + }, |
| 59 | + 'forwards args': { |
| 60 | + args: ['--verbose'], |
| 61 | + }, |
| 62 | + 'adds env flag with HUSKY_GIT_PARAMS when available': { |
| 63 | + env: {HUSKY_GIT_PARAMS: 'husky-git-params'}, |
| 64 | + }, |
| 65 | + }, |
| 66 | +) |
0 commit comments