Skip to content

Commit f943ef6

Browse files
authored
refactor(compiler): don't overwrite initial compiler options (#728)
Prevent potential side effects by not overwriting the original compiler options which are resolved from tsconfig
1 parent e7930ec commit f943ef6

File tree

3 files changed

+28
-39
lines changed

3 files changed

+28
-39
lines changed

src/__tests__/ng-jest-compiler.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ describe('NgJestCompiler', () => {
3333
'ts-jest': {
3434
...baseJestCfg.globals['ts-jest'],
3535
useESM,
36+
tsconfig: {
37+
esModuleInterop: false,
38+
allowSyntheticDefaultImports: false,
39+
},
3640
},
3741
},
3842
});
@@ -55,11 +59,17 @@ describe('NgJestCompiler', () => {
5559
.compilerOptions as ts.CompilerOptions;
5660
if (useESM) {
5761
expect(module).not.toEqual(ts.ModuleKind.CommonJS);
62+
// verify if allowSyntheticDefaultImports is hardcoded to true
5863
expect(allowSyntheticDefaultImports).toEqual(true);
64+
// verify if esModuleInterop is hardcoded to true
5965
expect(esModuleInterop).toEqual(true);
6066
} else {
6167
expect(module).toEqual(ts.ModuleKind.CommonJS);
6268
}
69+
// @ts-expect-error _initialCompilerOptions is a private property
70+
expect(compiler._initialCompilerOptions.esModuleInterop).not.toEqual(true);
71+
// @ts-expect-error _initialCompilerOptions is a private property
72+
expect(compiler._initialCompilerOptions.allowSyntheticDefaultImports).not.toEqual(true);
6373
});
6474
});
6575

@@ -121,6 +131,10 @@ describe('NgJestCompiler', () => {
121131
'ts-jest': {
122132
...jestCfgStub.globals['ts-jest'],
123133
useESM: true,
134+
tsconfig: {
135+
esModuleInterop: false,
136+
allowSyntheticDefaultImports: false,
137+
},
124138
},
125139
},
126140
});
@@ -131,6 +145,14 @@ describe('NgJestCompiler', () => {
131145

132146
// Source map is different based on file location which can fail on CI, so we only compare snapshot for js
133147
expect(emittedResult.substring(0, emittedResult.indexOf(SOURCE_MAPPING_PREFIX))).toMatchSnapshot();
148+
// @ts-expect-error _compilerOptions is a private property
149+
expect(compiler._compilerOptions.esModuleInterop).toEqual(true);
150+
// @ts-expect-error _compilerOptions is a private property
151+
expect(compiler._compilerOptions.allowSyntheticDefaultImports).toEqual(true);
152+
// @ts-expect-error _initialCompilerOptions is a private property
153+
expect(compiler._initialCompilerOptions.esModuleInterop).not.toEqual(true);
154+
// @ts-expect-error _initialCompilerOptions is a private property
155+
expect(compiler._initialCompilerOptions.allowSyntheticDefaultImports).not.toEqual(true);
134156
});
135157

136158
test.each([hasErrorFileName, undefined])('should throw diagnostics error for new file which is', (fileName) => {

src/compiler/ng-jest-compiler.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class NgJestCompiler implements CompilerInstance {
2424
private _compilerHost: CompilerHost | undefined;
2525
private _tsHost: NgJestCompilerHost | undefined;
2626
private _rootNames: string[] = [];
27+
private readonly _initialCompilerOptions: CompilerOptions;
2728
private readonly _logger: Logger;
2829
private readonly _ts: TTypeScript;
2930
private readonly isAppPath = (fileName: string) =>
@@ -32,6 +33,7 @@ export class NgJestCompiler implements CompilerInstance {
3233
constructor(readonly ngJestConfig: NgJestConfig, readonly jestCacheFS: Map<string, string>) {
3334
this._logger = this.ngJestConfig.logger;
3435
this._ts = this.ngJestConfig.compilerModule;
36+
this._initialCompilerOptions = { ...this.ngJestConfig.parsedTsConfig.options };
3537
this._setupOptions(this.ngJestConfig);
3638

3739
this._logger.debug('created NgJestCompiler');
@@ -46,9 +48,9 @@ export class NgJestCompiler implements CompilerInstance {
4648

4749
getCompiledOutput(fileName: string, fileContent: string, supportsStaticESM: boolean): string {
4850
const customTransformers = this.ngJestConfig.customTransformers;
49-
let moduleKind = this._compilerOptions.module;
50-
let esModuleInterop = this._compilerOptions.esModuleInterop;
51-
let allowSyntheticDefaultImports = this._compilerOptions.allowSyntheticDefaultImports;
51+
let moduleKind = this._initialCompilerOptions.module;
52+
let esModuleInterop = this._initialCompilerOptions.esModuleInterop;
53+
let allowSyntheticDefaultImports = this._initialCompilerOptions.allowSyntheticDefaultImports;
5254
if (supportsStaticESM && this.ngJestConfig.useESM) {
5355
moduleKind =
5456
!moduleKind ||
@@ -139,7 +141,7 @@ export class NgJestCompiler implements CompilerInstance {
139141
private _setupOptions({ parsedTsConfig }: NgJestConfig): void {
140142
this._logger.debug({ parsedTsConfig }, '_setupOptions: initializing compiler config');
141143

142-
this._compilerOptions = { ...parsedTsConfig.options };
144+
this._compilerOptions = { ...this._initialCompilerOptions };
143145
this._rootNames = parsedTsConfig.rootNames.filter((rootName) => !this.ngJestConfig.isTestFile(rootName));
144146
if (this._compilerOptions.strictMetadataEmit) {
145147
this._logger.warn(

yarn.lock

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,14 +1873,6 @@
18731873
"@typescript-eslint/typescript-estree" "4.13.0"
18741874
debug "^4.1.1"
18751875

1876-
"@typescript-eslint/[email protected]":
1877-
version "4.12.0"
1878-
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.12.0.tgz#beeb8beca895a07b10c593185a5612f1085ef279"
1879-
integrity sha512-QVf9oCSVLte/8jvOsxmgBdOaoe2J0wtEmBr13Yz0rkBNkl5D8bfnf6G4Vhox9qqMIoG7QQoVwd2eG9DM/ge4Qg==
1880-
dependencies:
1881-
"@typescript-eslint/types" "4.12.0"
1882-
"@typescript-eslint/visitor-keys" "4.12.0"
1883-
18841876
"@typescript-eslint/[email protected]":
18851877
version "4.13.0"
18861878
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.13.0.tgz#5b45912a9aa26b29603d8fa28f5e09088b947141"
@@ -1897,11 +1889,6 @@
18971889
"@typescript-eslint/types" "4.3.0"
18981890
"@typescript-eslint/visitor-keys" "4.3.0"
18991891

1900-
"@typescript-eslint/[email protected]":
1901-
version "4.12.0"
1902-
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.12.0.tgz#fb891fe7ccc9ea8b2bbd2780e36da45d0dc055e5"
1903-
integrity sha512-N2RhGeheVLGtyy+CxRmxdsniB7sMSCfsnbh8K/+RUIXYYq3Ub5+sukRCjVE80QerrUBvuEvs4fDhz5AW/pcL6g==
1904-
19051892
"@typescript-eslint/[email protected]":
19061893
version "4.13.0"
19071894
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.13.0.tgz#6a7c6015a59a08fbd70daa8c83dfff86250502f8"
@@ -1912,20 +1899,6 @@
19121899
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.3.0.tgz#1f0b2d5e140543e2614f06d48fb3ae95193c6ddf"
19131900
integrity sha512-Cx9TpRvlRjOppGsU6Y6KcJnUDOelja2NNCX6AZwtVHRzaJkdytJWMuYiqi8mS35MRNA3cJSwDzXePfmhU6TANw==
19141901

1915-
"@typescript-eslint/[email protected]":
1916-
version "4.12.0"
1917-
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.12.0.tgz#3963418c850f564bdab3882ae23795d115d6d32e"
1918-
integrity sha512-gZkFcmmp/CnzqD2RKMich2/FjBTsYopjiwJCroxqHZIY11IIoN0l5lKqcgoAPKHt33H2mAkSfvzj8i44Jm7F4w==
1919-
dependencies:
1920-
"@typescript-eslint/types" "4.12.0"
1921-
"@typescript-eslint/visitor-keys" "4.12.0"
1922-
debug "^4.1.1"
1923-
globby "^11.0.1"
1924-
is-glob "^4.0.1"
1925-
lodash "^4.17.15"
1926-
semver "^7.3.2"
1927-
tsutils "^3.17.1"
1928-
19291902
"@typescript-eslint/[email protected]":
19301903
version "4.13.0"
19311904
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.13.0.tgz#cf6e2207c7d760f5dfd8d18051428fadfc37b45e"
@@ -1954,14 +1927,6 @@
19541927
semver "^7.3.2"
19551928
tsutils "^3.17.1"
19561929

1957-
"@typescript-eslint/[email protected]":
1958-
version "4.12.0"
1959-
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.12.0.tgz#a470a79be6958075fa91c725371a83baf428a67a"
1960-
integrity sha512-hVpsLARbDh4B9TKYz5cLbcdMIOAoBYgFPCSP9FFS/liSF+b33gVNq8JHY3QGhHNVz85hObvL7BEYLlgx553WCw==
1961-
dependencies:
1962-
"@typescript-eslint/types" "4.12.0"
1963-
eslint-visitor-keys "^2.0.0"
1964-
19651930
"@typescript-eslint/[email protected]":
19661931
version "4.13.0"
19671932
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.13.0.tgz#9acb1772d3b3183182b6540d3734143dce9476fe"

0 commit comments

Comments
 (0)