Skip to content

Commit 3d35679

Browse files
feat(typescript-checker): add experimental support for TypeScript@7 (#6099)
* feat(typescript-checker): add experimental support for TypeScript@7 Add experimental support for TypeScript 7 (a.k.a. tsgo or TypeScript native preview) to @stryker-mutator/typescript-checker. Enable it in stryker.config.json: ```json { "typescriptChecker": { "experimentalNativePreview": true } } ``` It currently _assumes typescript v7 is installed as `typescript7`_, you can accomplish this with: ```bash npm install -D typescript7@npm:typescript@rc ``` Co-authored-by: Andreas Hoornstra <andreashoornstra@gmail.com>
1 parent cff4b6a commit 3d35679

27 files changed

Lines changed: 1380 additions & 182 deletions

‎e2e/helpers.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ export function readLogFile(fileName = path.resolve('stryker.log')) {
125125
return fsPromises.readFile(fileName, 'utf8');
126126
}
127127

128-
export async function expectMetricsJsonToMatchSnapshot() {
128+
export async function expectMetricsJsonToMatchSnapshot(
129+
jsonReportFile = path.resolve('reports', 'mutation', 'mutation.json'),
130+
) {
129131
const actualMetricsResult =
130-
await readMutationTestingJsonResultAsMetricsResult();
132+
await readMutationTestingJsonResultAsMetricsResult(jsonReportFile);
131133
expect(actualMetricsResult.systemUnderTestMetrics.metrics).to.matchSnapshot();
132134
}

‎e2e/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@
7878
"tap": "21.7.5",
7979
"ts-jest": "29.4.12",
8080
"ts-loader": "9.6.2",
81-
"tsx": "4.23.0",
8281
"tslib": "2.8.1",
82+
"tsx": "4.23.0",
8383
"vitest": "4.1.10",
8484
"typescript": "6.0.3",
85+
"@typescript/native": "npm:typescript@7.0.2",
8586
"vite": "8.0.16",
8687
"webpack": "5.108.4",
8788
"webpack-cli": "7.2.1"

‎e2e/test/typescript-transpiling/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"build": "npm run clean:dist && tsc",
1212
"verify": "mocha --no-config --no-package --timeout 0 verify/verify.js",
1313
"test:unit": "npm run build && mocha",
14-
"test": "npm run clean:reports && stryker run && npm run verify"
14+
"test": "npm run clean:reports && npm run test:stryker && npm run test:stryker-native-preview && npm run verify",
15+
"test:stryker": "stryker run",
16+
"test:stryker-native-preview": "stryker run stryker.conf.native-preview.json"
1517
},
1618
"mocha": {
1719
"spec": [
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"$schema": "../../node_modules/@stryker-mutator/core/schema/stryker-schema.json",
3+
"packageManager": "npm",
4+
"disableTypeChecks": true,
5+
"testRunner": "mocha",
6+
"concurrency": 1,
7+
"coverageAnalysis": "perTest",
8+
"reporters": ["json", "html", "progress", "clear-text"],
9+
"checkers": ["typescript"],
10+
"tsconfigFile": "tsconfig.json",
11+
"fileLogLevel": "warn",
12+
"jsonReporter": {
13+
"fileName": "reports/mutation/mutation-native-preview.json"
14+
},
15+
"htmlReporter": {
16+
"fileName": "reports/mutation/mutation-native-preview.html"
17+
},
18+
"typescriptChecker": {
19+
"experimentalNativePreview": true
20+
},
21+
"buildCommand": "npm run build",
22+
"plugins": [
23+
"@stryker-mutator/mocha-runner",
24+
"@stryker-mutator/typescript-checker"
25+
]
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import path from 'path';
12
import { expectMetricsJsonToMatchSnapshot } from '../../../helpers.js';
23

34
describe('Verify stryker has ran correctly', () => {
45
it('should report correct score', async () => {
56
await expectMetricsJsonToMatchSnapshot();
67
});
8+
it('should report correct score - native preview', async () => {
9+
await expectMetricsJsonToMatchSnapshot(
10+
path.resolve('reports', 'mutation', 'mutation-native-preview.json'),
11+
);
12+
});
713
});

‎e2e/test/typescript-transpiling/verify/verify.js.snap‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Verify stryker has ran correctly should report correct score - native preview 1`] = `
4+
Object {
5+
"compileErrors": 12,
6+
"ignored": 0,
7+
"killed": 41,
8+
"mutationScore": 74.54545454545455,
9+
"mutationScoreBasedOnCoveredCode": 91.11111111111111,
10+
"noCoverage": 10,
11+
"pending": 0,
12+
"runtimeErrors": 0,
13+
"survived": 4,
14+
"timeout": 0,
15+
"totalCovered": 45,
16+
"totalDetected": 41,
17+
"totalInvalid": 12,
18+
"totalMutants": 67,
19+
"totalUndetected": 14,
20+
"totalValid": 55,
21+
}
22+
`;
23+
324
exports[`Verify stryker has ran correctly should report correct score 1`] = `
425
Object {
526
"compileErrors": 12,

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"sinon-chai": "4.0.1",
4545
"source-map-support": "0.5.21",
4646
"typescript": "npm:@typescript/typescript6@6.0.2",
47-
"typescript7": "npm:typescript@7.0.2",
47+
"@typescript/native": "npm:typescript@7.0.2",
4848
"typescript-eslint": "8.67.0"
4949
},
5050
"scripts": {

‎packages/typescript-checker/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
},
5151
"devDependencies": {
5252
"@stryker-mutator/test-helpers": "workspace:*",
53-
"@types/semver": "7.7.1"
53+
"@types/semver": "7.7.1",
54+
"tslib": "~2.8.0",
55+
"@typescript/native": "npm:typescript@7.0.2"
5456
},
5557
"peerDependencies": {
5658
"@stryker-mutator/core": "workspace:*",

‎packages/typescript-checker/schema/typescript-checker-options.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"description": "Configures the performance of the TypescriptChecker. Setting this to false results in a slower, but more accurate result.",
1616
"type": "boolean",
1717
"default": true
18+
},
19+
"experimentalNativePreview": {
20+
"description": "Enable experimental support for type checking with the native TypeScript compiler preview (TypeScript 7). Requires TypeScript 7 to be installed under the \"@typescript/native\" alias: `npm install --save-dev @typescript/native@npm:typescript@latest`. Note: project references (--build mode) are not supported (yet) in this mode and mutants are always checked one-by-one (the \"prioritizePerformanceOverAccuracy\" option is ignored).",
21+
"type": "boolean",
22+
"default": false
1823
}
1924
}
2025
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createRequire } from 'module';
2+
import path from 'path';
3+
import { pathToFileURL } from 'url';
4+
5+
export type TypeScriptNativeSyncApi =
6+
typeof import('@typescript/native/unstable/sync');
7+
8+
/**
9+
* Loads the (unstable) sync API of the native TypeScript compiler preview (TypeScript 7).
10+
* Users are expected to have installed it under the "typescript7" alias:
11+
* `npm install --save-dev typescript7@npm:typescript@latest`
12+
*/
13+
export async function loadTypescriptNative(): Promise<TypeScriptNativeSyncApi> {
14+
try {
15+
return await import('@typescript/native/unstable/sync');
16+
} catch {
17+
try {
18+
// Fall back to resolving from the current working directory (the project under test)
19+
const require = createRequire(path.resolve('package.json'));
20+
return (await import(
21+
pathToFileURL(require.resolve('typescript/native/unstable/sync')).href
22+
)) as TypeScriptNativeSyncApi;
23+
} catch {
24+
throw new Error(
25+
'Could not load the native TypeScript compiler preview (tried to load "typescript/native/unstable/sync"). In order to use "typescriptChecker": { "experimentalNativePreview": true }, please install TypeScript 7 under the "typescript7" alias: `npm install --save-dev typescript7@npm:typescript@latest`',
26+
);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)