Skip to content

Commit

Permalink
fix(instrumenter): Use globalThis when available (#4169)
Browse files Browse the repository at this point in the history
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
nicojs authored May 6, 2023
1 parent e4a5bca commit 7d1e58e
Show file tree
Hide file tree
Showing 25 changed files with 3,865 additions and 2,883 deletions.
3 changes: 3 additions & 0 deletions e2e/test/disallow-code-generation-from-strings/.mocharc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"spec": ["src/**/*.spec.js"]
}
15 changes: 15 additions & 0 deletions e2e/test/disallow-code-generation-from-strings/package.json
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"
}
3 changes: 3 additions & 0 deletions e2e/test/disallow-code-generation-from-strings/src/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function add(a, b) {
return a + b;
}
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);
})
});
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"]
}
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();
});
});
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,
}
`;
3 changes: 2 additions & 1 deletion packages/instrumenter/src/util/syntax-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const { types, traverse, parse } = babel;
* Returns syntax for the header if JS/TS files
*/
export const instrumentationBabelHeader = deepFreeze(
// `globalThis` implementation is based on core-js's implementation. See https://github.com/stryker-mutator/stryker-js/issues/4035
parse(
`function ${STRYKER_NAMESPACE_HELPER}(){
var g = new Function("return this")();
var g = typeof globalThis === 'object' && globalThis && globalThis.Math === Math && globalThis || new Function("return this")();
var ns = g.${ID.NAMESPACE} || (g.${ID.NAMESPACE} = {});
if (ns.${ID.ACTIVE_MUTANT} === undefined && g.process && g.process.env && g.process.env.${ID.ACTIVE_MUTANT_ENV_VARIABLE}) {
ns.${ID.ACTIVE_MUTANT} = g.process.env.${ID.ACTIVE_MUTANT_ENV_VARIABLE};
Expand Down
Loading

0 comments on commit 7d1e58e

Please sign in to comment.