-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(instrumenter): Use
globalThis
when available (#4169)
Use `globalThis` to reference the global object when available for mutant schemata workings. Only falling back to `(new Function("return this"))` when it is `globalThis` is not available. Fixes `EvalError: Code generation from strings disallowed for this context` errors when using the [--disallow-code-generation-from-strings cli option.](https://nodejs.org/api/cli.html#--disallow-code-generation-from-strings).
- Loading branch information
Showing
25 changed files
with
3,865 additions
and
2,883 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
e2e/test/disallow-code-generation-from-strings/.mocharc.jsonc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"spec": ["src/**/*.spec.js"] | ||
} |
15 changes: 15 additions & 0 deletions
15
e2e/test/disallow-code-generation-from-strings/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "disallow-code-generation-from-strings", | ||
"version": "1.0.0", | ||
"description": "A e2e test for running node with --disallow-code-generation-from-strings. See https://github.com/stryker-mutator/stryker-js/issues/4035", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test:unit": "cross-env NODE_OPTIONS='--disallow-code-generation-from-strings' mocha", | ||
"test": "stryker run", | ||
"posttest": "mocha --no-config --no-timeout verify/verify.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function add(a, b) { | ||
return a + b; | ||
} |
8 changes: 8 additions & 0 deletions
8
e2e/test/disallow-code-generation-from-strings/src/math.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { expect } from 'chai'; | ||
import { add } from './math.js'; | ||
|
||
describe(add.name, () => { | ||
it('should add 40, 2 to be 42', () => { | ||
expect(add(40, 2)).eq(42); | ||
}) | ||
}); |
8 changes: 8 additions & 0 deletions
8
e2e/test/disallow-code-generation-from-strings/stryker.conf.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"$schema": "../../node_modules/@stryker-mutator/core/schema/stryker-schema.json", | ||
"concurrency": 1, | ||
"reporters": ["json", "clear-text"], | ||
"testRunner": "command", | ||
"commandRunner": { "command": "npm run test:unit" }, | ||
"plugins": ["@stryker-mutator/mocha-runner"] | ||
} |
7 changes: 7 additions & 0 deletions
7
e2e/test/disallow-code-generation-from-strings/verify/verify.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { expectMetricsJsonToMatchSnapshot } from '../../../helpers.js'; | ||
|
||
describe('After running stryker --disallow-code-generation-from-strings', () => { | ||
it('should report the mutation score correctly', async () => { | ||
await expectMetricsJsonToMatchSnapshot(); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
e2e/test/disallow-code-generation-from-strings/verify/verify.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`After running stryker --disallow-code-generation-from-strings should report the mutation score correctly 1`] = ` | ||
Object { | ||
"compileErrors": 0, | ||
"ignored": 0, | ||
"killed": 2, | ||
"mutationScore": 100, | ||
"mutationScoreBasedOnCoveredCode": 100, | ||
"noCoverage": 0, | ||
"pending": 0, | ||
"runtimeErrors": 0, | ||
"survived": 0, | ||
"timeout": 0, | ||
"totalCovered": 2, | ||
"totalDetected": 2, | ||
"totalInvalid": 0, | ||
"totalMutants": 2, | ||
"totalUndetected": 0, | ||
"totalValid": 2, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.