@@ -18,10 +18,28 @@ jest.mock('path', () => ({
1818 normalize : mockNormalize ,
1919} ) )
2020
21- import { isCreateReactAppTestCommand , pathToJest , pathToJestPackageJSON } from '../src/helpers'
21+ import { isCreateReactAppTestCommand , pathToJest , pathToJestPackageJSON , nodeBinExtension } from '../src/helpers'
2222import * as path from 'path'
2323
24+ // Manually (forcefully) set the executable's file extension to test its addition independendly of the operating system.
25+ ; ( nodeBinExtension as string ) = '.TEST'
26+
2427describe ( 'ModuleHelpers' , ( ) => {
28+ describe ( 'nodeBinExtension' , ( ) => {
29+ // Since `nodeBinExtension` is a variable, we have to reload the module in order to re-evaluate it.
30+ it ( 'should return an empty string on Linux' , ( ) => {
31+ jest . resetModules ( )
32+ mockPlatform . mockReturnValueOnce ( 'linux' )
33+ expect ( require ( '../src/helpers' ) . nodeBinExtension ) . toBe ( '' )
34+ } )
35+
36+ it ( 'should equal ".cmd" on Windows' , ( ) => {
37+ jest . resetModules ( )
38+ mockPlatform . mockReturnValueOnce ( 'win32' )
39+ expect ( require ( '../src/helpers' ) . nodeBinExtension ) . toBe ( '.cmd' )
40+ } )
41+ } )
42+
2543 describe ( 'pathToJestPackageJSON' , ( ) => {
2644 const defaultPathToJest = null
2745 const defaultSettings : any = {
@@ -145,6 +163,12 @@ describe('ModuleHelpers', () => {
145163 rootPath : '' ,
146164 }
147165
166+ beforeEach ( ( ) => {
167+ mockJoin . mockImplementation ( require . requireActual ( 'path' ) . join )
168+ mockNormalize . mockImplementation ( require . requireActual ( 'path' ) . normalize )
169+ mockExistsSync . mockImplementation ( require . requireActual ( 'path' ) . existsSync )
170+ } )
171+
148172 it ( 'returns "npm test --" when bootstrapped with create-react-app' , ( ) => {
149173 mockReadFileSync . mockReturnValueOnce (
150174 JSON . stringify ( {
@@ -170,52 +194,24 @@ describe('ModuleHelpers', () => {
170194 expect ( mockNormalize ) . toBeCalledWith ( settings . pathToJest )
171195 } )
172196
173- describe ( 'os platform: linux' , ( ) => {
174- it ( 'defaults to "node_modules/.bin/jest" when Jest is locally installed' , ( ) => {
175- const expected = 'node_modules/.bin/jest'
197+ it ( 'defaults to "node_modules/.bin/jest" when Jest is locally installed' , ( ) => {
198+ const expected = 'node_modules/.bin/jest.TEST'
176199
177- mockJoin . mockImplementation ( require . requireActual ( 'path' ) . posix . join )
178- mockPlatform . mockReturnValue ( 'linux' )
179- mockNormalize . mockImplementationOnce ( arg => arg )
180- mockExistsSync . mockImplementation ( path => path === expected )
200+ mockJoin . mockImplementation ( require . requireActual ( 'path' ) . posix . join )
201+ mockNormalize . mockImplementation ( arg => arg )
202+ mockExistsSync . mockImplementation ( path => path === expected )
181203
182- expect ( pathToJest ( defaultSettings ) ) . toBe ( expected )
183- } )
184-
185- it ( 'defaults to "jest" when Jest is locally installed' , ( ) => {
186- const expected = 'jest'
187-
188- mockJoin . mockImplementation ( require . requireActual ( 'path' ) . posix . join )
189- mockPlatform . mockReturnValue ( 'linux' )
190- mockNormalize . mockImplementationOnce ( arg => arg )
191- mockExistsSync . mockImplementation ( _ => false )
192-
193- expect ( pathToJest ( defaultSettings ) ) . toBe ( expected )
194- } )
204+ expect ( pathToJest ( defaultSettings ) ) . toBe ( expected )
195205 } )
196206
197- describe ( 'os platform: win32' , ( ) => {
198- it ( 'defaults to "node_modules/.bin/jest.cmd" when Jest is locally installed' , ( ) => {
199- const expected = 'node_modules/.bin/jest.cmd'
207+ it ( 'defaults to "jest" when Jest is not locally installed' , ( ) => {
208+ const expected = 'jest.TEST'
200209
201- mockJoin . mockImplementation ( require . requireActual ( 'path' ) . posix . join )
202- mockPlatform . mockReturnValue ( 'win32' )
203- mockNormalize . mockImplementationOnce ( arg => arg )
204- mockExistsSync . mockImplementation ( path => path === expected )
210+ mockJoin . mockImplementation ( require . requireActual ( 'path' ) . posix . join )
211+ mockNormalize . mockImplementation ( arg => arg )
212+ mockExistsSync . mockImplementation ( _ => false )
205213
206- expect ( pathToJest ( defaultSettings ) ) . toBe ( expected )
207- } )
208-
209- it ( 'defaults to "jest.cmd" when Jest is locally installed' , ( ) => {
210- const expected = 'jest.cmd'
211-
212- mockJoin . mockImplementation ( require . requireActual ( 'path' ) . posix . join )
213- mockPlatform . mockReturnValue ( 'win32' )
214- mockNormalize . mockImplementationOnce ( arg => arg )
215- mockExistsSync . mockImplementation ( _ => false )
216-
217- expect ( pathToJest ( defaultSettings ) ) . toBe ( expected )
218- } )
214+ expect ( pathToJest ( defaultSettings ) ) . toBe ( expected )
219215 } )
220216 } )
221217} )
0 commit comments