Skip to content

Commit 7a14209

Browse files
committed
test(scripts/commit): add tests for git-cz bootstrap invocation
1 parent a91c339 commit 7a14209

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

Diff for: src/scripts/__tests__/__snapshots__/commit.js.snap

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`commit bootstraps @commitlint/prompt 1`] = `
4+
Object {
5+
cliPath: <PROJECT_ROOT>/node_modules/commitizen,
6+
config: Object {
7+
path: @commitlint/prompt,
8+
},
9+
}
10+
`;
11+
12+
exports[`commit bootstraps @commitlint/prompt 2`] = `node ../commit`;
13+
14+
exports[`commit forwards arguments 1`] = `
15+
Object {
16+
cliPath: <PROJECT_ROOT>/node_modules/commitizen,
17+
config: Object {
18+
path: @commitlint/prompt,
19+
},
20+
}
21+
`;
22+
23+
exports[`commit forwards arguments 2`] = `node ../commit --retry`;
24+
25+
exports[`commit strips errant "commit" argument 1`] = `
26+
Object {
27+
cliPath: <PROJECT_ROOT>/node_modules/commitizen,
28+
config: Object {
29+
path: @commitlint/prompt,
30+
},
31+
}
32+
`;
33+
34+
exports[`commit strips errant "commit" argument 2`] = `node ../commit`;
35+
36+
exports[`commit strips errant "commit" argument and forwards arguments 1`] = `
37+
Object {
38+
cliPath: <PROJECT_ROOT>/node_modules/commitizen,
39+
config: Object {
40+
path: @commitlint/prompt,
41+
},
42+
}
43+
`;
44+
45+
exports[`commit strips errant "commit" argument and forwards arguments 2`] = `node ../commit --retry`;

Diff for: src/scripts/__tests__/commit.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import cases from 'jest-in-case'
2+
import {
3+
unquoteSerializer,
4+
winPathSerializer,
5+
relativePathSerializer,
6+
} from './helpers/serializers'
7+
8+
jest.mock('commitizen/dist/cli/git-cz')
9+
10+
expect.addSnapshotSerializer(unquoteSerializer)
11+
expect.addSnapshotSerializer(winPathSerializer)
12+
expect.addSnapshotSerializer(relativePathSerializer)
13+
14+
cases(
15+
'commit',
16+
({args = [], env = {}}) => {
17+
// beforeEach
18+
const {bootstrap: bootstrapMock} = require('commitizen/dist/cli/git-cz')
19+
20+
const originalArgv = process.argv
21+
const originalExit = process.exit
22+
const originalEnv = process.env
23+
24+
process.exit = jest.fn()
25+
process.argv = ['node', '../commit', ...args]
26+
process.env = env
27+
28+
try {
29+
// tests
30+
require('../commit')
31+
32+
expect(bootstrapMock).toHaveBeenCalledTimes(1)
33+
34+
const [call] = bootstrapMock.mock.calls
35+
const [optionsArg, argsArg] = call
36+
37+
expect(optionsArg).toMatchSnapshot()
38+
expect(argsArg.join(' ')).toMatchSnapshot()
39+
} catch (error) {
40+
throw error
41+
} finally {
42+
// afterEach
43+
process.exit = originalExit
44+
process.argv = originalArgv
45+
process.env = originalEnv
46+
47+
jest.resetModules()
48+
}
49+
},
50+
{
51+
'bootstraps @commitlint/prompt': {},
52+
'strips errant "commit" argument': {
53+
args: ['commit'],
54+
},
55+
'forwards arguments': {
56+
args: ['--retry'],
57+
},
58+
'strips errant "commit" argument and forwards arguments': {
59+
args: ['commit', '--retry'],
60+
},
61+
},
62+
)

0 commit comments

Comments
 (0)