1
+ import path from 'path'
1
2
import cases from 'jest-in-case'
3
+ import yargsParser from 'yargs-parser'
2
4
import { unquoteSerializer , winPathSerializer } from './helpers/serializers'
3
5
4
6
expect . addSnapshotSerializer ( unquoteSerializer )
5
7
expect . addSnapshotSerializer ( winPathSerializer )
6
8
9
+ jest . mock ( 'os' , ( ) => ( {
10
+ ...jest . requireActual ( 'os' ) ,
11
+ tmpdir : jest . fn ( ( ) => '.test-tmp' ) ,
12
+ } ) )
13
+
14
+ jest . mock ( 'fs' , ( ) => ( {
15
+ ...jest . requireActual ( 'fs' ) ,
16
+ mkdtempSync : jest . fn ( prefix => `${ prefix } TMPSUFFIX` ) ,
17
+ rmdirSync : jest . fn ( ) ,
18
+ writeFileSync : jest . fn ( ) ,
19
+ } ) )
20
+
7
21
cases (
8
22
'pre-commit' ,
9
23
( {
@@ -12,9 +26,15 @@ cases(
12
26
hasPkgProp = ( ) => false ,
13
27
hasFile = ( ) => false ,
14
28
ifScript = ( ) => true ,
29
+ expectError = false ,
15
30
} ) => {
16
31
// beforeEach
17
32
const { sync : crossSpawnSyncMock } = require ( 'cross-spawn' )
33
+ const {
34
+ writeFileSync : writeFileSyncMock ,
35
+ rmdirSync : rmdirSyncMock ,
36
+ } = require ( 'fs' )
37
+
18
38
const originalArgv = process . argv
19
39
const originalExit = process . exit
20
40
Object . assign ( utils , {
@@ -34,12 +54,42 @@ cases(
34
54
call => `${ call [ 0 ] } ${ call [ 1 ] . join ( ' ' ) } ` ,
35
55
)
36
56
expect ( commands ) . toMatchSnapshot ( )
57
+
58
+ // Specific tests for when a custom test command is supplied
59
+ if ( ! ! yargsParser ( args ) . testCommand ) {
60
+ // ensure we don't pass `--testCommand` through to `lint-staged`
61
+ expect (
62
+ crossSpawnSyncMock . mock . calls . some ( ( [ _command , commandArgs ] ) =>
63
+ commandArgs . some (
64
+ a => a === '--testCommand' || a === '--test-command' ,
65
+ ) ,
66
+ ) ,
67
+ ) . not . toBeTruthy ( )
68
+
69
+ const [ writeFileSyncCall ] = writeFileSyncMock . mock . calls
70
+
71
+ // Snapshot the config file we use with the custom test command
72
+ expect ( writeFileSyncCall ) . toMatchSnapshot ( )
73
+
74
+ // Make sure we clean up the temporary config file
75
+ expect ( rmdirSyncMock ) . toHaveBeenCalledWith (
76
+ path . dirname ( writeFileSyncCall [ 0 ] ) ,
77
+ { recursive : true } ,
78
+ )
79
+ }
37
80
} catch ( error ) {
38
- throw error
81
+ if ( expectError ) {
82
+ expect ( error ) . toMatchSnapshot ( )
83
+ } else {
84
+ throw error
85
+ }
39
86
} finally {
40
87
// afterEach
41
88
process . exit = originalExit
42
89
process . argv = originalArgv
90
+
91
+ writeFileSyncMock . mockClear ( )
92
+
43
93
jest . resetModules ( )
44
94
}
45
95
} ,
@@ -63,5 +113,38 @@ cases(
63
113
[ `does not run validate script if it's not defined` ] : {
64
114
ifScript : ( ) => false ,
65
115
} ,
116
+ 'throws an error when `--config` and `--testCommand` are used together' : {
117
+ expectError : true ,
118
+ args : [
119
+ '--config' ,
120
+ 'some-config.js' ,
121
+ '--testCommand' ,
122
+ '"yarn test:unit --findRelatedTests"' ,
123
+ ] ,
124
+ } ,
125
+ 'throws an error when invalid `--testCommand` is provided' : {
126
+ expectError : true ,
127
+ args : [ '--testCommand' , '--config' , 'some-config.js' ] ,
128
+ } ,
129
+ 'overrides built-in test command with --testCommand' : {
130
+ args : [ '--testCommand' , '"yarn test:custom --findRelatedTests foo.js"' ] ,
131
+ } ,
132
+ 'overrides built-in test command with --test-command' : {
133
+ args : [ '--test-command' , '"yarn test:custom --findRelatedTests foo.js"' ] ,
134
+ } ,
135
+ 'overrides built-in test command with --testCommand and forwards args' : {
136
+ args : [
137
+ '--verbose' ,
138
+ '--testCommand' ,
139
+ '"yarn test:custom --findRelatedTests foo.js"' ,
140
+ ] ,
141
+ } ,
142
+ 'overrides built-in test command with --test-command and forwards args' : {
143
+ args : [
144
+ '--verbose' ,
145
+ '--test-command' ,
146
+ '"yarn test:custom --findRelatedTests foo.js"' ,
147
+ ] ,
148
+ } ,
66
149
} ,
67
150
)
0 commit comments