11import chalk from 'chalk' ;
22import path from 'node:path' ;
3- import { jest } from '@jest/globals' ;
4- import type { SpyInstance } from " jest-mock" ;
3+ import { jest } from '@jest/globals' ;
4+ import type { SpyInstance } from ' jest-mock' ;
55
66import { defaultConfig as _defaultConfig } from '../src/config' ;
77import { messages } from '../src/messages' ;
88
9- import { noop , spyOnLog } from './utils' ;
9+ import {
10+ spyOnConsole ,
11+ spyOnProcess ,
12+ mockResolveProjectBasePath ,
13+ } from './utils' ;
1014
1115const sourceRoot = '__tests__' ;
1216let mockedGloblConfig ;
13- jest . mock ( '../src/utils/resolve-project-base-path' , ( ) => {
14- return {
15- resolveProjectBasePath : ( ) => {
16- return { projectBasePath : sourceRoot } ;
17- }
18- }
19- } ) ;
20- jest . mock ( '@ngneat/transloco-utils' , ( ) => {
21- return {
22- getGlobalConfig : ( ) => mockedGloblConfig
23- } ;
24- } ) ;
2517
18+ mockResolveProjectBasePath ( sourceRoot ) ;
19+
20+ jest . unstable_mockModule ( '@ngneat/transloco-utils' , ( ) => ( {
21+ getGlobalConfig : ( ) => mockedGloblConfig ,
22+ } ) ) ;
23+
24+ /**
25+ * With ESM modules, you need to mock the modules beforehand (with jest.unstable_mockModule) and import them ashynchronously afterwards.
26+ * This thing is still in WIP at Jest, so keep an eye on it.
27+ * @see https://jestjs.io/docs/ecmascript-modules#module-mocking-in-esm
28+ */
2629const { resolveConfig } = await import ( '../src/utils/resolve-config' ) ;
2730
2831describe ( 'resolveConfig' , ( ) => {
@@ -38,10 +41,8 @@ describe('resolveConfig', () => {
3841
3942 beforeAll ( ( ) => {
4043 mockedGloblConfig = { } ;
41- processExitSpy = jest
42- . spyOn ( process , 'exit' )
43- . mockImplementation ( noop as any ) ;
44- consoleLogSpy = spyOnLog ( ) ;
44+ processExitSpy = spyOnProcess ( 'exit' ) ;
45+ consoleLogSpy = spyOnConsole ( 'log' ) ;
4546 spies = [ processExitSpy , consoleLogSpy ] ;
4647 } ) ;
4748
@@ -107,7 +108,7 @@ describe('resolveConfig', () => {
107108 } ) ;
108109
109110 afterAll ( ( ) => {
110- mockedGloblConfig = { }
111+ mockedGloblConfig = { } ;
111112 } ) ;
112113
113114 it ( 'should merge the default and the transloco config' , ( ) => {
@@ -140,14 +141,14 @@ describe('resolveConfig', () => {
140141 function shouldFail ( prop : string , msg : 'pathDoesntExist' | 'pathIsNotDir' ) {
141142 expect ( processExitSpy ) . toHaveBeenCalled ( ) ;
142143 expect ( consoleLogSpy ) . toHaveBeenCalledWith (
143- chalk . bgRed . black ( `${ prop } ${ messages [ msg ] } ` )
144+ chalk . bgRed . black ( `${ prop } ${ messages [ msg ] } ` ) ,
144145 ) ;
145146 clearSpies ( ) ;
146147 }
147148
148149 function shouldPass ( ) {
149150 [ processExitSpy , consoleLogSpy ] . forEach ( ( s ) =>
150- expect ( s ) . not . toHaveBeenCalled ( )
151+ expect ( s ) . not . toHaveBeenCalled ( ) ,
151152 ) ;
152153 clearSpies ( ) ;
153154 }
@@ -195,11 +196,11 @@ describe('resolveConfig', () => {
195196 const config = resolveConfig ( { input : [ 'comments' ] } ) ;
196197 const assertPath = ( p ) =>
197198 expect ( p . startsWith ( path . resolve ( process . cwd ( ) , sourceRoot ) ) ) . toBe (
198- true
199+ true ,
199200 ) ;
200201 config . input . forEach ( assertPath ) ;
201202 [ 'output' , 'translationsPath' ] . forEach ( ( prop ) =>
202- assertPath ( config [ prop ] )
203+ assertPath ( config [ prop ] ) ,
203204 ) ;
204205 } ) ;
205206 } ) ;
0 commit comments