Skip to content

Commit 95af584

Browse files
committed
fix: don't attempt to run validate from precommit unless it exists
1 parent 4708b6b commit 95af584

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`pre-commit calls lint-staged CLI with default args 1`] = `lint-staged --config ./src/config/lintstagedrc.js`;
4-
5-
exports[`pre-commit calls lint-staged CLI with default args 2`] = `npm run validate`;
6-
7-
exports[`pre-commit does not use built-in config with .lintstagedrc file 1`] = `lint-staged`;
8-
9-
exports[`pre-commit does not use built-in config with .lintstagedrc file 2`] = `npm run validate`;
10-
11-
exports[`pre-commit does not use built-in config with --config 1`] = `lint-staged --config ./custom-config.js`;
12-
13-
exports[`pre-commit does not use built-in config with --config 2`] = `npm run validate`;
14-
15-
exports[`pre-commit does not use built-in config with lint-staged pkg prop 1`] = `lint-staged`;
16-
17-
exports[`pre-commit does not use built-in config with lint-staged pkg prop 2`] = `npm run validate`;
18-
19-
exports[`pre-commit does not use built-in config with lint-staged.config.js file 1`] = `lint-staged`;
20-
21-
exports[`pre-commit does not use built-in config with lint-staged.config.js file 2`] = `npm run validate`;
22-
23-
exports[`pre-commit forwards args 1`] = `lint-staged --config ./src/config/lintstagedrc.js --verbose`;
24-
25-
exports[`pre-commit forwards args 2`] = `npm run validate`;
3+
exports[`pre-commit calls lint-staged CLI with default args 1`] = `
4+
Array [
5+
lint-staged --config ./src/config/lintstagedrc.js,
6+
npm run validate,
7+
]
8+
`;
9+
10+
exports[`pre-commit does not run validate script if it's not defined 1`] = `
11+
Array [
12+
lint-staged --config ./src/config/lintstagedrc.js,
13+
]
14+
`;
15+
16+
exports[`pre-commit does not use built-in config with .lintstagedrc file 1`] = `
17+
Array [
18+
lint-staged ,
19+
npm run validate,
20+
]
21+
`;
22+
23+
exports[`pre-commit does not use built-in config with --config 1`] = `
24+
Array [
25+
lint-staged --config ./custom-config.js,
26+
npm run validate,
27+
]
28+
`;
29+
30+
exports[`pre-commit does not use built-in config with lint-staged pkg prop 1`] = `
31+
Array [
32+
lint-staged ,
33+
npm run validate,
34+
]
35+
`;
36+
37+
exports[`pre-commit does not use built-in config with lint-staged.config.js file 1`] = `
38+
Array [
39+
lint-staged ,
40+
npm run validate,
41+
]
42+
`;
43+
44+
exports[`pre-commit forwards args 1`] = `
45+
Array [
46+
lint-staged --config ./src/config/lintstagedrc.js --verbose,
47+
npm run validate,
48+
]
49+
`;

src/scripts/__tests__/precommit.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cases(
1111
utils = require('../../utils'),
1212
hasPkgProp = () => false,
1313
hasFile = () => false,
14+
ifScript = () => true,
1415
}) => {
1516
// beforeEach
1617
const {sync: crossSpawnSyncMock} = require('cross-spawn')
@@ -19,6 +20,7 @@ cases(
1920
Object.assign(utils, {
2021
hasPkgProp,
2122
hasFile,
23+
ifScript,
2224
resolveBin: (modName, {executable = modName} = {}) => executable,
2325
})
2426
process.exit = jest.fn()
@@ -28,12 +30,10 @@ cases(
2830
try {
2931
// tests
3032
require('../pre-commit')
31-
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(2)
32-
const [firstCall, secondCall] = crossSpawnSyncMock.mock.calls
33-
const [scriptOne, calledArgsOne] = firstCall
34-
expect([scriptOne, ...calledArgsOne].join(' ')).toMatchSnapshot()
35-
const [scriptTwo, calledArgsTwo] = secondCall
36-
expect([scriptTwo, ...calledArgsTwo].join(' ')).toMatchSnapshot()
33+
const commands = crossSpawnSyncMock.mock.calls.map(
34+
call => `${call[0]} ${call[1].join(' ')}`,
35+
)
36+
expect(commands).toMatchSnapshot()
3737
} catch (error) {
3838
throw error
3939
} finally {
@@ -60,5 +60,8 @@ cases(
6060
'forwards args': {
6161
args: ['--verbose'],
6262
},
63+
[`does not run validate script if it's not defined`]: {
64+
ifScript: () => false,
65+
},
6366
},
6467
)

src/scripts/pre-commit.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path')
22
const spawn = require('cross-spawn')
3-
const {hasPkgProp, hasFile, resolveBin} = require('../utils')
3+
const {hasPkgProp, hasFile, ifScript, resolveBin} = require('../utils')
44

55
const here = p => path.join(__dirname, p)
66
const hereRelative = p => here(p).replace(process.cwd(), '.')
@@ -23,7 +23,7 @@ const lintStagedResult = spawn.sync(
2323
{stdio: 'inherit'},
2424
)
2525

26-
if (lintStagedResult.status === 0) {
26+
if (lintStagedResult.status === 0 && ifScript('validate')) {
2727
const validateResult = spawn.sync('npm', ['run', 'validate'], {
2828
stdio: 'inherit',
2929
})

0 commit comments

Comments
 (0)