Skip to content

Commit

Permalink
refactor(compiler): don't overwrite initial compiler options (#728)
Browse files Browse the repository at this point in the history
Prevent potential side effects by not overwriting the original compiler options which are resolved from tsconfig
  • Loading branch information
ahnpnl committed Jan 13, 2021
1 parent e7930ec commit f943ef6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 39 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/ng-jest-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('NgJestCompiler', () => {
'ts-jest': {
...baseJestCfg.globals['ts-jest'],
useESM,
tsconfig: {
esModuleInterop: false,
allowSyntheticDefaultImports: false,
},
},
},
});
Expand All @@ -55,11 +59,17 @@ describe('NgJestCompiler', () => {
.compilerOptions as ts.CompilerOptions;
if (useESM) {
expect(module).not.toEqual(ts.ModuleKind.CommonJS);
// verify if allowSyntheticDefaultImports is hardcoded to true
expect(allowSyntheticDefaultImports).toEqual(true);
// verify if esModuleInterop is hardcoded to true
expect(esModuleInterop).toEqual(true);
} else {
expect(module).toEqual(ts.ModuleKind.CommonJS);
}
// @ts-expect-error _initialCompilerOptions is a private property
expect(compiler._initialCompilerOptions.esModuleInterop).not.toEqual(true);
// @ts-expect-error _initialCompilerOptions is a private property
expect(compiler._initialCompilerOptions.allowSyntheticDefaultImports).not.toEqual(true);
});
});

Expand Down Expand Up @@ -121,6 +131,10 @@ describe('NgJestCompiler', () => {
'ts-jest': {
...jestCfgStub.globals['ts-jest'],
useESM: true,
tsconfig: {
esModuleInterop: false,
allowSyntheticDefaultImports: false,
},
},
},
});
Expand All @@ -131,6 +145,14 @@ describe('NgJestCompiler', () => {

// Source map is different based on file location which can fail on CI, so we only compare snapshot for js
expect(emittedResult.substring(0, emittedResult.indexOf(SOURCE_MAPPING_PREFIX))).toMatchSnapshot();
// @ts-expect-error _compilerOptions is a private property
expect(compiler._compilerOptions.esModuleInterop).toEqual(true);
// @ts-expect-error _compilerOptions is a private property
expect(compiler._compilerOptions.allowSyntheticDefaultImports).toEqual(true);
// @ts-expect-error _initialCompilerOptions is a private property
expect(compiler._initialCompilerOptions.esModuleInterop).not.toEqual(true);
// @ts-expect-error _initialCompilerOptions is a private property
expect(compiler._initialCompilerOptions.allowSyntheticDefaultImports).not.toEqual(true);
});

test.each([hasErrorFileName, undefined])('should throw diagnostics error for new file which is', (fileName) => {
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/ng-jest-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class NgJestCompiler implements CompilerInstance {
private _compilerHost: CompilerHost | undefined;
private _tsHost: NgJestCompilerHost | undefined;
private _rootNames: string[] = [];
private readonly _initialCompilerOptions: CompilerOptions;
private readonly _logger: Logger;
private readonly _ts: TTypeScript;
private readonly isAppPath = (fileName: string) =>
Expand All @@ -32,6 +33,7 @@ export class NgJestCompiler implements CompilerInstance {
constructor(readonly ngJestConfig: NgJestConfig, readonly jestCacheFS: Map<string, string>) {
this._logger = this.ngJestConfig.logger;
this._ts = this.ngJestConfig.compilerModule;
this._initialCompilerOptions = { ...this.ngJestConfig.parsedTsConfig.options };
this._setupOptions(this.ngJestConfig);

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

getCompiledOutput(fileName: string, fileContent: string, supportsStaticESM: boolean): string {
const customTransformers = this.ngJestConfig.customTransformers;
let moduleKind = this._compilerOptions.module;
let esModuleInterop = this._compilerOptions.esModuleInterop;
let allowSyntheticDefaultImports = this._compilerOptions.allowSyntheticDefaultImports;
let moduleKind = this._initialCompilerOptions.module;
let esModuleInterop = this._initialCompilerOptions.esModuleInterop;
let allowSyntheticDefaultImports = this._initialCompilerOptions.allowSyntheticDefaultImports;
if (supportsStaticESM && this.ngJestConfig.useESM) {
moduleKind =
!moduleKind ||
Expand Down Expand Up @@ -139,7 +141,7 @@ export class NgJestCompiler implements CompilerInstance {
private _setupOptions({ parsedTsConfig }: NgJestConfig): void {
this._logger.debug({ parsedTsConfig }, '_setupOptions: initializing compiler config');

this._compilerOptions = { ...parsedTsConfig.options };
this._compilerOptions = { ...this._initialCompilerOptions };
this._rootNames = parsedTsConfig.rootNames.filter((rootName) => !this.ngJestConfig.isTestFile(rootName));
if (this._compilerOptions.strictMetadataEmit) {
this._logger.warn(
Expand Down
35 changes: 0 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1873,14 +1873,6 @@
"@typescript-eslint/typescript-estree" "4.13.0"
debug "^4.1.1"

"@typescript-eslint/scope-manager@4.12.0":
version "4.12.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.12.0.tgz#beeb8beca895a07b10c593185a5612f1085ef279"
integrity sha512-QVf9oCSVLte/8jvOsxmgBdOaoe2J0wtEmBr13Yz0rkBNkl5D8bfnf6G4Vhox9qqMIoG7QQoVwd2eG9DM/ge4Qg==
dependencies:
"@typescript-eslint/types" "4.12.0"
"@typescript-eslint/visitor-keys" "4.12.0"

"@typescript-eslint/scope-manager@4.13.0":
version "4.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.13.0.tgz#5b45912a9aa26b29603d8fa28f5e09088b947141"
Expand All @@ -1897,11 +1889,6 @@
"@typescript-eslint/types" "4.3.0"
"@typescript-eslint/visitor-keys" "4.3.0"

"@typescript-eslint/types@4.12.0":
version "4.12.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.12.0.tgz#fb891fe7ccc9ea8b2bbd2780e36da45d0dc055e5"
integrity sha512-N2RhGeheVLGtyy+CxRmxdsniB7sMSCfsnbh8K/+RUIXYYq3Ub5+sukRCjVE80QerrUBvuEvs4fDhz5AW/pcL6g==

"@typescript-eslint/types@4.13.0":
version "4.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.13.0.tgz#6a7c6015a59a08fbd70daa8c83dfff86250502f8"
Expand All @@ -1912,20 +1899,6 @@
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.3.0.tgz#1f0b2d5e140543e2614f06d48fb3ae95193c6ddf"
integrity sha512-Cx9TpRvlRjOppGsU6Y6KcJnUDOelja2NNCX6AZwtVHRzaJkdytJWMuYiqi8mS35MRNA3cJSwDzXePfmhU6TANw==

"@typescript-eslint/typescript-estree@4.12.0":
version "4.12.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.12.0.tgz#3963418c850f564bdab3882ae23795d115d6d32e"
integrity sha512-gZkFcmmp/CnzqD2RKMich2/FjBTsYopjiwJCroxqHZIY11IIoN0l5lKqcgoAPKHt33H2mAkSfvzj8i44Jm7F4w==
dependencies:
"@typescript-eslint/types" "4.12.0"
"@typescript-eslint/visitor-keys" "4.12.0"
debug "^4.1.1"
globby "^11.0.1"
is-glob "^4.0.1"
lodash "^4.17.15"
semver "^7.3.2"
tsutils "^3.17.1"

"@typescript-eslint/typescript-estree@4.13.0":
version "4.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.13.0.tgz#cf6e2207c7d760f5dfd8d18051428fadfc37b45e"
Expand Down Expand Up @@ -1954,14 +1927,6 @@
semver "^7.3.2"
tsutils "^3.17.1"

"@typescript-eslint/visitor-keys@4.12.0":
version "4.12.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.12.0.tgz#a470a79be6958075fa91c725371a83baf428a67a"
integrity sha512-hVpsLARbDh4B9TKYz5cLbcdMIOAoBYgFPCSP9FFS/liSF+b33gVNq8JHY3QGhHNVz85hObvL7BEYLlgx553WCw==
dependencies:
"@typescript-eslint/types" "4.12.0"
eslint-visitor-keys "^2.0.0"

"@typescript-eslint/visitor-keys@4.13.0":
version "4.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.13.0.tgz#9acb1772d3b3183182b6540d3734143dce9476fe"
Expand Down

0 comments on commit f943ef6

Please sign in to comment.