Skip to content

Commit

Permalink
fix(core): disableTypeChecks true only forces ts-like file match (#4485)
Browse files Browse the repository at this point in the history
Change the meaning of `true` for `disableTypeChecks` from all files to `'**/*.{js,ts,jsx,tsx,html,vue,mjs,mts,cts,cjs}'`.
  • Loading branch information
randmon committed Oct 14, 2023
1 parent c3644dc commit 31f3411
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 7 deletions.
19 changes: 17 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,26 @@ _Note: Disable bail needs to be supported by the test runner plugin in order to

### `disableTypeChecks` [`boolean` | `string`]

Default: `"{test,src,lib}/**/*.{js,ts,jsx,tsx,html,vue}"`<br />

<details>

<summary>History</summary>

| Version | Changes |
| ------- | --------------------------- |
| 7.0 | Default changed from `"{test,src,lib}/**/*.{js,ts,jsx,tsx,html,vue}"` to `true` |

</details>

Default: `true`<br />
Command: _none_<br />
Config file: `"disableTypeChecks": false`

Set to 'true' to disable type checking, or 'false' to enable it. For more control, configure a pattern that matches the files of which type checking has to be disabled. This is needed because Stryker will create (typescript) type errors when inserting the mutants in your code. Stryker disables type checking by inserting `// @ts-nocheck` atop those files and removing other `// @ts-xxx` directives (so they won't interfere with `@ts-nocheck`). The default setting allows these directives to be stripped from all JavaScript and friend files in `lib`, `src` and `test` directories.
Set to `true` to disable type checking, or `false` to enable it. For more control, configure a pattern that matches the files of which type checking has to be disabled, for example: `"{test,src,lib}/**/*.{js,ts,jsx,tsx}"`. Setting it to `true` will disable type checking for all TypeScript-ish files (currently *.ts, *.js, *.tsx, *.jsx, *.mjs, *.mts, *.cts, *.cjs, *.html and *.vue files).

Disabling type checking is needed because Stryker will create (typescript) type errors when inserting the mutants in your code. Stryker disables type checking by inserting `// @ts-nocheck` atop those files and removing other `// @ts-xxx` directives (so they won't interfere with `@ts-nocheck`).



### `dryRunOnly` [`boolean`]

Expand Down
10 changes: 10 additions & 0 deletions e2e/test/use-binary-files/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const foo = 'bar';
import { readFileSync } from 'fs';

export default function () {
if (foo === 'bar') {
const buf = readFileSync('input.docx');
return buf;
}
return 'baz';
}
Binary file added e2e/test/use-binary-files/input.docx
Binary file not shown.
13 changes: 13 additions & 0 deletions e2e/test/use-binary-files/package-lock.json

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

16 changes: 16 additions & 0 deletions e2e/test/use-binary-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "use-binary-filesg",
"version": "0.0.0",
"private": true,
"type": "module",
"description": "A module to verify issue https://github.com/stryker-mutator/stryker-js/issues/4386",
"main": "index.js",
"scripts": {
"test:unit": "mocha",
"pretest": "rimraf \"reports\" \"stryker.log\"",
"test": "stryker run",
"posttest": "mocha --no-config --no-package --timeout 0 verify/verify.js"
},
"author": "",
"license": "ISC"
}
9 changes: 9 additions & 0 deletions e2e/test/use-binary-files/stryker.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "../../node_modules/@stryker-mutator/core/schema/stryker-schema.json",
"packageManager": "npm",
"testRunner": "mocha",
"concurrency": 1,
"reporters": ["json", "html", "progress", "clear-text"],
"fileLogLevel": "warn",
"plugins": ["@stryker-mutator/mocha-runner"]
}
12 changes: 12 additions & 0 deletions e2e/test/use-binary-files/test/use-binary-file.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import foo from '../foo.js';

describe('should do x', function () {
it('xxx', function () {
console.log('hello');
const result = foo();
console.log(result.length);
if (result.length !== 12077) {
throw new Error(`Error getting result : ${result.length}`);
}
});
});
3 changes: 3 additions & 0 deletions e2e/test/use-binary-files/verify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
7 changes: 7 additions & 0 deletions e2e/test/use-binary-files/verify/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expectMetricsJsonToMatchSnapshot } from '../../../helpers.js';

describe('Verify stryker has ran correctly', () => {
it('should report correct score', async () => {
await expectMetricsJsonToMatchSnapshot();
});
});
22 changes: 22 additions & 0 deletions e2e/test/use-binary-files/verify/verify.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Verify stryker has ran correctly should report correct score 1`] = `
Object {
"compileErrors": 0,
"ignored": 0,
"killed": 0,
"mutationScore": NaN,
"mutationScoreBasedOnCoveredCode": NaN,
"noCoverage": 0,
"pending": 0,
"runtimeErrors": 0,
"survived": 0,
"timeout": 0,
"totalCovered": 0,
"totalDetected": 0,
"totalInvalid": 0,
"totalMutants": 0,
"totalUndetected": 0,
"totalValid": 0,
}
`;
9 changes: 7 additions & 2 deletions packages/core/src/config/file-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import { normalizeFileName } from '@stryker-mutator/util';
export class FileMatcher {
private readonly pattern: boolean | string;

constructor(pattern: boolean | string) {
constructor(
pattern: boolean | string,
private readonly allowHiddenFiles = true,
) {
if (typeof pattern === 'string') {
this.pattern = normalizeFileName(path.resolve(pattern));
} else if (pattern) {
this.pattern = '**/*.{js,ts,jsx,tsx,html,vue,mjs,mts,cts,cjs}';
} else {
this.pattern = pattern;
}
}

public matches(fileName: string): boolean {
if (typeof this.pattern === 'string') {
return minimatch(normalizeFileName(path.resolve(fileName)), this.pattern);
return minimatch(normalizeFileName(path.resolve(fileName)), this.pattern, { dot: this.allowHiddenFiles });
} else {
return this.pattern;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/fs/project-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class ProjectReader {
},
];
}
const matcher = new FileMatcher(mutatePattern);
const matcher = new FileMatcher(mutatePattern, /* allowHiddenFiles */ false);
const inputFiles = new Map<string, FileDescription>();
for (const fileName of fileNames) {
if (matcher.matches(fileName)) {
Expand Down
21 changes: 19 additions & 2 deletions packages/core/test/unit/config/file-matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@ describe(FileMatcher.name, () => {
expect(sut.matches('src/foo.ts')).true;
});

it('should match if the pattern is set to `true`', () => {
['js', 'ts', 'jsx', 'tsx', 'html', 'vue', 'mjs', 'mts', 'cts', 'cjs'].forEach((ext) => {
it(`should match when the glob pattern matches with extension ${ext}`, () => {
const sut = new FileMatcher(true);
expect(sut.matches(`src/foo.${ext}`)).true;
});
});

it('should allow hidden files to be matched', () => {
const sut = new FileMatcher(true);
expect(sut.matches('src/foo.ts')).true;
expect(sut.matches('.hidden/foo.js')).true;
});

it('should not allow hidden files to be matched when allowHiddenFiles is false', () => {
const sut = new FileMatcher(true, /* allowHiddenFiles */ false);
expect(sut.matches('.hidden/foo.js')).false;
});

it('should not match binary files if the pattern is set to `true`', () => {
const sut = new FileMatcher(true);
expect(sut.matches('src/foo.docx')).false;
});

it('should not match if the pattern is set to `false`', () => {
Expand Down

0 comments on commit 31f3411

Please sign in to comment.