Skip to content

Commit 64229a8

Browse files
committed
fix(config): set tsconfig target to ES2015 when target > ES2016 (#1118)
Closes #1058
1 parent 34f6497 commit 64229a8

File tree

8 files changed

+1229
-1238
lines changed

8 files changed

+1229
-1238
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { HarnessLoader } from '@angular/cdk/testing';
2+
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
3+
import { Component } from '@angular/core';
4+
import { ComponentFixture, TestBed } from '@angular/core/testing';
5+
import { MatButtonModule } from '@angular/material/button';
6+
import { MatButtonHarness } from '@angular/material/button/testing';
7+
import { MATERIAL_SANITY_CHECKS } from '@angular/material/core';
8+
9+
@Component({
10+
selector: 'snd-button-page',
11+
template: `
12+
<p>button-page works!</p>
13+
<p>
14+
<button mat-button (click)="click()">Click</button>
15+
</p>
16+
`,
17+
styles: [],
18+
})
19+
class ButtonPageComponent {
20+
r = 0;
21+
22+
click(): void {
23+
this.r = 1;
24+
}
25+
}
26+
27+
describe('ButtonPageComponent', () => {
28+
let component: ButtonPageComponent;
29+
let fixture: ComponentFixture<ButtonPageComponent>;
30+
let loader: HarnessLoader;
31+
32+
beforeEach(async () => {
33+
await TestBed.configureTestingModule({
34+
imports: [MatButtonModule],
35+
declarations: [ButtonPageComponent],
36+
providers: [
37+
{
38+
provide: MATERIAL_SANITY_CHECKS,
39+
useValue: false,
40+
},
41+
],
42+
}).compileComponents();
43+
fixture = TestBed.createComponent(ButtonPageComponent);
44+
loader = TestbedHarnessEnvironment.loader(fixture);
45+
});
46+
47+
beforeEach(() => {
48+
component = fixture.componentInstance;
49+
fixture.detectChanges();
50+
});
51+
52+
it('should create', () => {
53+
expect(component).toBeTruthy();
54+
});
55+
56+
it('should click', async () => {
57+
const button = await loader.getHarness(MatButtonHarness);
58+
await button.click();
59+
expect(component.r).toBe(1);
60+
});
61+
});

e2e/async/jest-esm.config.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
const jestCfg = require('./jest.config');
2+
13
/** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */
24
module.exports = {
3-
displayName: 'async',
4-
preset: '<rootDir>/../../node_modules/ts-jest/presets/default-esm',
5+
...jestCfg,
56
globals: {
67
'ts-jest': {
78
useESM: true,
8-
tsconfig: '<rootDir>/../tsconfig-esm.json',
9+
tsconfig: {
10+
...require('../tsconfig-esm.json').compilerOptions,
11+
/**
12+
* Set at ES2018 to test Angular doesn't work with ES2017+
13+
* see https://github.com/angular/components/issues/21632#issuecomment-764975917
14+
*/
15+
target: 'ES2018',
16+
},
917
},
1018
},
11-
setupFilesAfterEnv: ['<rootDir>/../../setup-jest.js'],
12-
transform: { '^.+\\.(ts|js|html)$': '<rootDir>/../../build/index.js' },
19+
moduleNameMapper: {
20+
tslib: 'tslib/tslib.es6.js',
21+
},
22+
transformIgnorePatterns: ['node_modules/(?!tslib)'],
1323
};

e2e/async/jest.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ module.exports = {
33
displayName: 'async',
44
globals: {
55
'ts-jest': {
6-
tsconfig: '<rootDir>/../tsconfig.json',
6+
tsconfig: {
7+
...require('../tsconfig.json').compilerOptions,
8+
/**
9+
* Set at ES2018 to test Angular doesn't work with ES2017+
10+
* see https://github.com/angular/components/issues/21632#issuecomment-764975917
11+
*/
12+
target: 'ES2018',
13+
},
714
},
815
},
16+
testEnvironment: 'jsdom',
917
setupFilesAfterEnv: ['<rootDir>/../../setup-jest.js'],
1018
transform: { '^.+\\.(ts|js|html)$': '<rootDir>/../../build/index.js' },
1119
};

package.json

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,43 @@
5353
"@angular/platform-browser-dynamic": ">=10.0.0"
5454
},
5555
"devDependencies": {
56-
"@angular/common": "^12.2.6",
57-
"@angular/compiler": "^12.2.6",
58-
"@angular/compiler-cli": "^12.2.6",
59-
"@angular/core": "^12.2.6",
60-
"@angular/platform-browser": "^12.2.6",
61-
"@angular/platform-browser-dynamic": "^12.2.6",
56+
"@angular/animations": "^12.2.11",
57+
"@angular/cdk": "^12.2.11",
58+
"@angular/common": "^12.2.11",
59+
"@angular/compiler": "^12.2.11",
60+
"@angular/compiler-cli": "^12.2.11",
61+
"@angular/core": "^12.2.11",
62+
"@angular/forms": "^12.2.11",
63+
"@angular/material": "^12.2.11",
64+
"@angular/platform-browser": "^12.2.11",
65+
"@angular/platform-browser-dynamic": "^12.2.11",
6266
"@babel/core": "^7.15.5",
6367
"@babel/preset-env": "^7.15.6",
6468
"@commitlint/cli": "^13.1.0",
6569
"@commitlint/config-angular": "^13.1.0",
66-
"@jest/globals": "^27.2.1",
67-
"@jest/transform": "^27.2.1",
68-
"@jest/types": "^27.1.1",
70+
"@jest/globals": "^27.3.0",
71+
"@jest/transform": "^27.3.0",
72+
"@jest/types": "^27.2.5",
6973
"@types/jest": "^27.0.2",
7074
"@types/jquery": "^3.5.6",
7175
"@types/node": "^16.9.4",
7276
"@typescript-eslint/eslint-plugin": "^4.31.2",
7377
"@typescript-eslint/parser": "^4.31.2",
74-
"babel-jest": "^27.2.1",
78+
"babel-jest": "^27.3.1",
7579
"conventional-changelog-cli": "^2.1.1",
7680
"cross-env": "^7.0.3",
7781
"eslint": "^7.32.0",
7882
"eslint-config-prettier": "^8.3.0",
79-
"eslint-plugin-import": "^2.24.2",
80-
"eslint-plugin-jest": "^24.4.0",
81-
"eslint-plugin-jsdoc": "^36.1.0",
83+
"eslint-plugin-import": "^2.25.2",
84+
"eslint-plugin-jest": "^25.2.2",
85+
"eslint-plugin-jsdoc": "^37.0.0",
8286
"eslint-plugin-prefer-arrow": "^1.2.3",
8387
"eslint-plugin-prettier": "^4.0.0",
8488
"execa": "^5.1.1",
8589
"fs-extra": "^10.0.0",
8690
"glob": "^7.1.7",
8791
"husky": "^7.0.2",
88-
"jest": "^27.2.1",
92+
"jest": "^27.3.1",
8993
"jest-snapshot-serializer-raw": "^1.2.0",
9094
"pinst": "^2.1.6",
9195
"prettier": "^2.4.1",

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ts from 'typescript';
2+
13
import { NgJestConfig } from '../config/ng-jest-config';
24

35
describe('NgJestConfig', () => {
@@ -11,5 +13,21 @@ describe('NgJestConfig', () => {
1113
expect(compilerOptions.annotationsAs).toBe('decorators');
1214
expect(compilerOptions.enableResourceInlining).toBe(false);
1315
expect(compilerOptions.allowJs).toBe(true);
16+
expect(compilerOptions.target).toBe(ts.ScriptTarget.ES2015);
17+
});
18+
19+
test('should set typescript target to ES2015 if user is using target higher than ES2016', () => {
20+
expect(
21+
new NgJestConfig({
22+
globals: {
23+
'ts-jest': {
24+
tsconfig: {
25+
target: 'ES2017',
26+
},
27+
},
28+
},
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
} as any).parsedTsConfig.options.target,
31+
).toBe(ts.ScriptTarget.ES2015);
1432
});
1533
});

src/config/ng-jest-config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class NgJestConfig extends ConfigSet {
77
* Override `ts-jest` behavior because we use `readConfiguration` which will read and resolve tsconfig.
88
*/
99
protected _resolveTsConfig(compilerOptions?: RawCompilerOptions, resolvedConfigFile?: string): ParsedCommandLine {
10-
const result = super._resolveTsConfig(compilerOptions, resolvedConfigFile);
10+
const result = super._resolveTsConfig(compilerOptions, resolvedConfigFile) as ParsedCommandLine;
1111
result.options.enableIvy = true;
1212
result.options.noEmitOnError = false;
1313
result.options.suppressOutputPathCheck = true;
@@ -16,7 +16,13 @@ export class NgJestConfig extends ConfigSet {
1616
result.options.enableResourceInlining = false;
1717
// Since we define preset default also transform `js` so we need to set `allowJs` true
1818
result.options.allowJs = true;
19+
const ts = this.compilerModule;
20+
const scriptTarget = result.options.target ?? ts.ScriptTarget?.ES2015;
21+
if (scriptTarget > ts.ScriptTarget?.ES2016) {
22+
// See https://github.com/angular/components/issues/21632#issuecomment-764975917
23+
result.options.target = ts.ScriptTarget?.ES2015;
24+
}
1925

20-
return result as ParsedCommandLine;
26+
return result;
2127
}
2228
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"skipLibCheck": true,
1212
"importHelpers": true,
1313
"esModuleInterop": true,
14-
"target": "es2015",
14+
"moduleResolution": "Node",
15+
"target": "ES2015",
1516
"module": "CommonJS",
1617
"lib": ["esnext", "dom"],
1718
"types": ["node", "jest"]

0 commit comments

Comments
 (0)