1+ import { transformSync } from 'esbuild' ;
2+
13import { NgJestCompiler } from '../compiler/ng-jest-compiler' ;
24import { NgJestConfig } from '../config/ng-jest-config' ;
35import { NgJestTransformer } from '../ng-jest-transformer' ;
46
57const tr = new NgJestTransformer ( ) ;
68
9+ jest . mock ( 'esbuild' , ( ) => {
10+ return {
11+ transformSync : jest . fn ( ) . mockReturnValue ( {
12+ code : 'bla bla' ,
13+ map : JSON . stringify ( { version : 1 , sourceContent : 'foo foo' } ) ,
14+ } ) ,
15+ } ;
16+ } ) ;
17+
718describe ( 'NgJestTransformer' , ( ) => {
819 test ( 'should create NgJestCompiler and NgJestConfig instances' , ( ) => {
920 // @ts -expect-error testing purpose
@@ -22,43 +33,26 @@ describe('NgJestTransformer', () => {
2233 expect ( cs ) . toBeInstanceOf ( NgJestConfig ) ;
2334 } ) ;
2435
25- test ( 'should process successfully a mjs file with CommonJS mode' , ( ) => {
36+ test . each ( [
37+ {
38+ tsconfig : {
39+ sourceMap : false ,
40+ } ,
41+ } ,
42+ {
43+ tsconfig : {
44+ target : 'es2016' ,
45+ } ,
46+ } ,
47+ {
48+ tsconfig : { } ,
49+ } ,
50+ ] ) ( 'should process successfully a mjs file to CommonJS codes' , ( { tsconfig } ) => {
2651 const result = tr . process (
2752 `
2853 const pi = parseFloat(3.124);
2954
30- export default pi;
31- ` ,
32- 'foo.mjs' ,
33- {
34- config : {
35- cwd : process . cwd ( ) ,
36- extensionsToTreatAsEsm : [ ] ,
37- testMatch : [ ] ,
38- testRegex : [ ] ,
39- } ,
40- } as any , // eslint-disable-line @typescript-eslint/no-explicit-any
41- ) ;
42-
43- expect ( typeof result ) . toBe ( 'object' ) ;
44- // @ts -expect-error `code` is a property of `TransformSource`
45- expect ( result . code ) . toMatchInlineSnapshot ( `
46- "\\"use strict\\";
47- Object.defineProperty(exports, \\"__esModule\\", { value: true });
48- const pi = parseFloat(3.124);
49- exports.default = pi;
50- //# sourceMappingURL=foo.mjs.js.map"
51- ` ) ;
52- // @ts -expect-error `map` is a property of `TransformSource`
53- expect ( result . map ) . toBeDefined ( ) ;
54- } ) ;
55-
56- test ( 'should process successfully a mjs file with ESM mode' , ( ) => {
57- const result = tr . process (
58- `
59- const pi = parseFloat(3.124);
60-
61- export default pi;
55+ export { pi };
6256 ` ,
6357 'foo.mjs' ,
6458 {
@@ -69,50 +63,20 @@ describe('NgJestTransformer', () => {
6963 testRegex : [ ] ,
7064 globals : {
7165 'ts-jest' : {
72- useESM : true ,
66+ tsconfig ,
7367 } ,
7468 } ,
7569 } ,
76- supportsStaticESM : true ,
7770 } as any , // eslint-disable-line @typescript-eslint/no-explicit-any
7871 ) ;
7972
80- expect ( typeof result ) . toBe ( 'object' ) ;
73+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74+ expect ( ( transformSync as unknown as jest . MockInstance < unknown , any > ) . mock . calls [ 0 ] ) . toMatchSnapshot ( ) ;
75+ // @ts -expect-error `code` is a property of `TransformSource`
76+ expect ( result . code ) . toBeDefined ( ) ;
8177 // @ts -expect-error `code` is a property of `TransformSource`
82- expect ( result . code ) . toMatchInlineSnapshot ( `
83- "const pi = parseFloat(3.124);
84- export default pi;
85- //# sourceMappingURL=foo.mjs.js.map"
86- ` ) ;
87- // @ts -expect-error `map` is a property of `TransformSource`
8878 expect ( result . map ) . toBeDefined ( ) ;
89- } ) ;
90-
91- test ( 'should throw syntax error for mjs file with checkJs true' , ( ) => {
92- expect ( ( ) =>
93- tr . process (
94- `
95- const pi == parseFloat(3.124);
96-
97- export default pi;
98- ` ,
99- 'foo.mjs' ,
100- {
101- config : {
102- cwd : process . cwd ( ) ,
103- extensionsToTreatAsEsm : [ ] ,
104- testMatch : [ ] ,
105- testRegex : [ ] ,
106- globals : {
107- 'ts-jest' : {
108- tsconfig : {
109- checkJs : true ,
110- } ,
111- } ,
112- } ,
113- } ,
114- } as any , // eslint-disable-line @typescript-eslint/no-explicit-any
115- ) ,
116- ) . toThrowError ( ) ;
79+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
80+ ( transformSync as unknown as jest . MockInstance < unknown , any > ) . mockClear ( ) ;
11781 } ) ;
11882} ) ;
0 commit comments