Bug fixing for Node 10 (#7931)

This commit is contained in:
Logan Smyth 2018-05-14 23:02:01 -07:00 committed by GitHub
parent f03adbadf4
commit 51db3e9a5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 18 deletions

View File

@ -33,7 +33,9 @@ testContext.global = testContext;
runModuleInTestContext("@babel/polyfill", __filename); runModuleInTestContext("@babel/polyfill", __filename);
// Populate the "babelHelpers" global with Babel's helper utilities. // 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. * 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. * Exposed for unit tests, not for use as an API.
*/ */
export function runCodeInTestContext( export function runCodeInTestContext(code: string, opts: { filename: string }) {
code: string, const filename = opts.filename;
opts: { filename?: string } = {}, const dirname = path.dirname(filename);
) { const req = id => runModuleInTestContext(id, filename);
const filename = opts.filename || null;
const dirname = filename ? path.dirname(filename) : null;
const req = filename ? id => runModuleInTestContext(id, filename) : null;
const module = { const module = {
id: filename, id: filename,

View File

@ -4,20 +4,35 @@ describe("helper-transform-fixture-test-runner", function() {
it("should not execute code in Node's global context", function() { it("should not execute code in Node's global context", function() {
try { try {
global.foo = "outer"; global.foo = "outer";
runCodeInTestContext(` runCodeInTestContext(
expect(global.foo).toBeUndefined(); `
global.foo = "inner"; expect(global.foo).toBeUndefined();
`); global.foo = "inner";
`,
{
filename: `${__filename}.fake1`,
},
);
expect(global.foo).toBe("outer"); expect(global.foo).toBe("outer");
runCodeInTestContext(` runCodeInTestContext(
expect(global.foo).toBe("inner"); `
`); expect(global.foo).toBe("inner");
`,
{
filename: `${__filename}.fake2`,
},
);
} finally { } finally {
delete global.foo; delete global.foo;
runCodeInTestContext(` runCodeInTestContext(
delete global.foo; `
`); delete global.foo;
`,
{
filename: `${__filename}.fake3`,
},
);
} }
}); });
}); });