1+ import path from 'path'
12import cases from 'jest-in-case'
3+ import yargsParser from 'yargs-parser'
24import { unquoteSerializer , winPathSerializer } from './helpers/serializers'
35
46expect . addSnapshotSerializer ( unquoteSerializer )
57expect . addSnapshotSerializer ( winPathSerializer )
68
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+
721cases (
822 'pre-commit' ,
923 ( {
@@ -12,9 +26,15 @@ cases(
1226 hasPkgProp = ( ) => false ,
1327 hasFile = ( ) => false ,
1428 ifScript = ( ) => true ,
29+ expectError = false ,
1530 } ) => {
1631 // beforeEach
1732 const { sync : crossSpawnSyncMock } = require ( 'cross-spawn' )
33+ const {
34+ writeFileSync : writeFileSyncMock ,
35+ rmdirSync : rmdirSyncMock ,
36+ } = require ( 'fs' )
37+
1838 const originalArgv = process . argv
1939 const originalExit = process . exit
2040 Object . assign ( utils , {
@@ -34,12 +54,42 @@ cases(
3454 call => `${ call [ 0 ] } ${ call [ 1 ] . join ( ' ' ) } ` ,
3555 )
3656 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+ }
3780 } catch ( error ) {
38- throw error
81+ if ( expectError ) {
82+ expect ( error ) . toMatchSnapshot ( )
83+ } else {
84+ throw error
85+ }
3986 } finally {
4087 // afterEach
4188 process . exit = originalExit
4289 process . argv = originalArgv
90+
91+ writeFileSyncMock . mockClear ( )
92+
4393 jest . resetModules ( )
4494 }
4595 } ,
@@ -63,5 +113,38 @@ cases(
63113 [ `does not run validate script if it's not defined` ] : {
64114 ifScript : ( ) => false ,
65115 } ,
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+ } ,
66149 } ,
67150)
0 commit comments