diff --git a/examples/example-app-v10/tsconfig-esm.spec.json b/examples/example-app-v10/tsconfig-esm.spec.json index 697868d2a9..d918086314 100644 --- a/examples/example-app-v10/tsconfig-esm.spec.json +++ b/examples/example-app-v10/tsconfig-esm.spec.json @@ -1,7 +1,6 @@ { "extends": "./tsconfig.spec.json", "compilerOptions": { - "module": "ESNext", - "allowJs": true + "module": "ESNext" } } diff --git a/examples/example-app-v11/tsconfig-esm.spec.json b/examples/example-app-v11/tsconfig-esm.spec.json index 697868d2a9..d918086314 100644 --- a/examples/example-app-v11/tsconfig-esm.spec.json +++ b/examples/example-app-v11/tsconfig-esm.spec.json @@ -1,7 +1,6 @@ { "extends": "./tsconfig.spec.json", "compilerOptions": { - "module": "ESNext", - "allowJs": true + "module": "ESNext" } } diff --git a/examples/example-app-v12-monorepo/projects/app1/tsconfig-esm.spec.json b/examples/example-app-v12-monorepo/projects/app1/tsconfig-esm.spec.json index 697868d2a9..d918086314 100644 --- a/examples/example-app-v12-monorepo/projects/app1/tsconfig-esm.spec.json +++ b/examples/example-app-v12-monorepo/projects/app1/tsconfig-esm.spec.json @@ -1,7 +1,6 @@ { "extends": "./tsconfig.spec.json", "compilerOptions": { - "module": "ESNext", - "allowJs": true + "module": "ESNext" } } diff --git a/examples/example-app-v12-monorepo/projects/app2/tsconfig-esm.spec.json b/examples/example-app-v12-monorepo/projects/app2/tsconfig-esm.spec.json index 697868d2a9..d918086314 100644 --- a/examples/example-app-v12-monorepo/projects/app2/tsconfig-esm.spec.json +++ b/examples/example-app-v12-monorepo/projects/app2/tsconfig-esm.spec.json @@ -1,7 +1,6 @@ { "extends": "./tsconfig.spec.json", "compilerOptions": { - "module": "ESNext", - "allowJs": true + "module": "ESNext" } } diff --git a/examples/example-app-v12/tsconfig-esm.spec.json b/examples/example-app-v12/tsconfig-esm.spec.json index 697868d2a9..d918086314 100644 --- a/examples/example-app-v12/tsconfig-esm.spec.json +++ b/examples/example-app-v12/tsconfig-esm.spec.json @@ -1,7 +1,6 @@ { "extends": "./tsconfig.spec.json", "compilerOptions": { - "module": "ESNext", - "allowJs": true + "module": "ESNext" } } diff --git a/examples/example-app-v9/tsconfig-esm.spec.json b/examples/example-app-v9/tsconfig-esm.spec.json index 697868d2a9..d918086314 100644 --- a/examples/example-app-v9/tsconfig-esm.spec.json +++ b/examples/example-app-v9/tsconfig-esm.spec.json @@ -1,7 +1,6 @@ { "extends": "./tsconfig.spec.json", "compilerOptions": { - "module": "ESNext", - "allowJs": true + "module": "ESNext" } } diff --git a/src/__tests__/ng-jest-config.spec.ts b/src/__tests__/ng-jest-config.spec.ts new file mode 100644 index 0000000000..123a7d75b3 --- /dev/null +++ b/src/__tests__/ng-jest-config.spec.ts @@ -0,0 +1,15 @@ +import { NgJestConfig } from '../config/ng-jest-config'; + +describe('NgJestConfig', () => { + test('should override some compiler options', () => { + const compilerOptions = new NgJestConfig(Object.create(null)).parsedTsConfig.options; + + expect(compilerOptions.enableIvy).toBe(true); + expect(compilerOptions.noEmitOnError).toBe(false); + expect(compilerOptions.suppressOutputPathCheck).toBe(true); + expect(compilerOptions.allowEmptyCodegenFiles).toBe(false); + expect(compilerOptions.annotationsAs).toBe('decorators'); + expect(compilerOptions.enableResourceInlining).toBe(false); + expect(compilerOptions.allowJs).toBe(true); + }); +}); diff --git a/src/__tests__/ng-jest-transformer.spec.ts b/src/__tests__/ng-jest-transformer.spec.ts index 6299ca260c..cea8e35c08 100644 --- a/src/__tests__/ng-jest-transformer.spec.ts +++ b/src/__tests__/ng-jest-transformer.spec.ts @@ -1,4 +1,5 @@ import { NgJestCompiler } from '../compiler/ng-jest-compiler'; +import { NgJestConfig } from '../config/ng-jest-config'; import { NgJestTransformer } from '../ng-jest-transformer'; describe('NgJestTransformer', () => { @@ -18,6 +19,7 @@ describe('NgJestTransformer', () => { // @ts-expect-error testing purpose expect(tr._compiler).toBeInstanceOf(NgJestCompiler); + expect(cs).toBeInstanceOf(NgJestConfig); }); }); }); diff --git a/src/config/ng-jest-config.ts b/src/config/ng-jest-config.ts new file mode 100644 index 0000000000..d3fbbf4d56 --- /dev/null +++ b/src/config/ng-jest-config.ts @@ -0,0 +1,22 @@ +import { ConfigSet } from 'ts-jest/dist/config/config-set'; +import type { RawCompilerOptions } from 'ts-jest/dist/raw-compiler-options'; +import type { ParsedCommandLine } from 'typescript'; + +export class NgJestConfig extends ConfigSet { + /** + * Override `ts-jest` behavior because we use `readConfiguration` which will read and resolve tsconfig. + */ + protected _resolveTsConfig(compilerOptions?: RawCompilerOptions, resolvedConfigFile?: string): ParsedCommandLine { + const result = super._resolveTsConfig(compilerOptions, resolvedConfigFile); + result.options.enableIvy = true; + result.options.noEmitOnError = false; + result.options.suppressOutputPathCheck = true; + result.options.allowEmptyCodegenFiles = false; + result.options.annotationsAs = 'decorators'; + result.options.enableResourceInlining = false; + // Since we define preset default also transform `js` so we need to set `allowJs` true + result.options.allowJs = true; + + return result as ParsedCommandLine; + } +} diff --git a/src/ng-jest-transformer.ts b/src/ng-jest-transformer.ts index 5c6184fcd1..8bd6abb53a 100644 --- a/src/ng-jest-transformer.ts +++ b/src/ng-jest-transformer.ts @@ -1,9 +1,15 @@ import { ConfigSet } from 'ts-jest/dist/config/config-set'; import { TsJestTransformer } from 'ts-jest/dist/ts-jest-transformer'; +import type { ProjectConfigTsJest } from 'ts-jest/dist/types'; import { NgJestCompiler } from './compiler/ng-jest-compiler'; +import { NgJestConfig } from './config/ng-jest-config'; export class NgJestTransformer extends TsJestTransformer { + protected _createConfigSet(config: ProjectConfigTsJest | undefined): ConfigSet { + return new NgJestConfig(config); + } + protected _createCompiler(configSet: ConfigSet, cacheFS: Map): void { this._compiler = new NgJestCompiler(configSet, cacheFS); } diff --git a/tsconfig.spec.json b/tsconfig.spec.json index a96a9daf6e..5b30143ad2 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -2,7 +2,6 @@ "extends": "./tsconfig.json", "compilerOptions": { "allowSyntheticDefaultImports": true, - "esModuleInterop": true, - "allowJs": true + "esModuleInterop": true } }