Skip to content

Commit

Permalink
fix(typescript-checker): support TS 4.4 (#3178)
Browse files Browse the repository at this point in the history
TypeScript 4.4 also watches package.json files in node_modules for changes. I guess this is done to be able to reload the types when a node_module gets updated.

However, the typescript-checker wasn't expecting those `watchFile` calls to come in. The checker was defensive programmed, meaning an error is thrown when something happens that it doesn't expect.

This ended up being a simple fix. Simply ignoring `watchFile` of files that are not loaded seems to do the trick.
  • Loading branch information
nicojs authored Sep 30, 2021
1 parent 95e6b69 commit 772e5bc
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
6 changes: 3 additions & 3 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"ts-jest": "~27.0.1",
"ts-loader": "~8.0.3",
"ts-node": "~7.0.0",
"typescript": "~4.3.2",
"typescript": "^4.4.3",
"webpack": "~4.41.2",
"webpack-cli": "~3.3.12"
},
Expand Down
4 changes: 2 additions & 2 deletions e2e/test/jest-with-ts/stryker.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
],
"coverageAnalysis": "off",
"reporters": [
"event-recorder",
"progress",
"html"
"html",
"json"
]
}
11 changes: 5 additions & 6 deletions e2e/test/jest-with-ts/verify/verify.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { expectMetrics } from '../../../helpers';
import { expectMetricsJson } from '../../../helpers';

describe('Verify stryker has ran correctly', () => {

it('should report correct score', async () => {
await expectMetrics({
await expectMetricsJson({
ignored: 0,
killed: 17,
mutationScore: 56.67,
mutationScore: 62.96,
noCoverage: 0,
survived: 13,
survived: 10,
timeout: 0,
compileErrors: 32
compileErrors: 35,
});
});
});
2 changes: 1 addition & 1 deletion packages/typescript-checker/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"type": "node",
"request": "launch",
"name": "Unit / Integration tests",
"name": "💙 Unit / Integration tests",
"program": "${workspaceRoot}/../../node_modules/mocha/bin/_mocha",
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
Expand Down
7 changes: 3 additions & 4 deletions packages/typescript-checker/src/fs/hybrid-file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ export class HybridFileSystem {

public watchFile(fileName: string, watcher: ts.FileWatcherCallback): void {
const file = this.getFile(fileName);
if (!file) {
throw new Error(`Cannot find file ${fileName} for watching`);
if (file) {
this.log.trace('Registering watcher for file "%s"', fileName);
file.watcher = watcher;
}
this.log.trace('Registering watcher for file "%s"', fileName);
file.watcher = watcher;
}

public getFile(fileName: string): ScriptFile | undefined {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sinon from 'sinon';
import ts from 'typescript';
import { expect } from 'chai';
import { testInjector } from '@stryker-mutator/test-helpers';
import { factory, testInjector } from '@stryker-mutator/test-helpers';

import { HybridFileSystem } from '../../../src/fs';

Expand Down Expand Up @@ -106,8 +106,14 @@ describe('fs', () => {
expect(helper.readFileStub).calledWith('test/foo/a.js');
});

it("should throw if file doesn't exist", () => {
expect(() => sut.watchFile('not-exists.js', sinon.stub())).throws('Cannot find file not-exists.js for watching');
it("should not throw if file isn't loaded", () => {
// Should ignore the file watch
const watchCallback = sinon.stub();
sut.watchFile('node_modules/chai/package.json', watchCallback);

// If it was successfully ignored, than `mutate` should throw
expect(() => sut.mutate(factory.mutant({ fileName: 'node_modules/chai/package.json' }))).throws();
expect(watchCallback).not.called;
});

it('should log that the file is watched', () => {
Expand Down

0 comments on commit 772e5bc

Please sign in to comment.