diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.js b/packages/babel-helper-transform-fixture-test-runner/src/index.js index f3b827464e..d5d3b6b847 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -33,7 +33,9 @@ testContext.global = testContext; runModuleInTestContext("@babel/polyfill", __filename); // Populate the "babelHelpers" global with Babel's helper utilities. -runCodeInTestContext(buildExternalHelpers()); +runCodeInTestContext(buildExternalHelpers(), { + filename: path.join(__dirname, "babel-helpers-in-memory.js"), +}); /** * A basic implementation of CommonJS so we can execute `@babel/polyfill` inside our test context. @@ -75,13 +77,10 @@ function runModuleInTestContext(id: string, relativeFilename: string) { * * Exposed for unit tests, not for use as an API. */ -export function runCodeInTestContext( - code: string, - opts: { filename?: string } = {}, -) { - const filename = opts.filename || null; - const dirname = filename ? path.dirname(filename) : null; - const req = filename ? id => runModuleInTestContext(id, filename) : null; +export function runCodeInTestContext(code: string, opts: { filename: string }) { + const filename = opts.filename; + const dirname = path.dirname(filename); + const req = id => runModuleInTestContext(id, filename); const module = { id: filename, diff --git a/packages/babel-helper-transform-fixture-test-runner/test/index.js b/packages/babel-helper-transform-fixture-test-runner/test/index.js index 05621f4500..2ec3d6bb2c 100644 --- a/packages/babel-helper-transform-fixture-test-runner/test/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/test/index.js @@ -4,20 +4,35 @@ describe("helper-transform-fixture-test-runner", function() { it("should not execute code in Node's global context", function() { try { global.foo = "outer"; - runCodeInTestContext(` - expect(global.foo).toBeUndefined(); - global.foo = "inner"; - `); + runCodeInTestContext( + ` + expect(global.foo).toBeUndefined(); + global.foo = "inner"; + `, + { + filename: `${__filename}.fake1`, + }, + ); expect(global.foo).toBe("outer"); - runCodeInTestContext(` - expect(global.foo).toBe("inner"); - `); + runCodeInTestContext( + ` + expect(global.foo).toBe("inner"); + `, + { + filename: `${__filename}.fake2`, + }, + ); } finally { delete global.foo; - runCodeInTestContext(` - delete global.foo; - `); + runCodeInTestContext( + ` + delete global.foo; + `, + { + filename: `${__filename}.fake3`, + }, + ); } }); });