Nicolò Ribaudo d04842a700
Avoid using CJS globals in internal source files (#12963)
* Lint against CJS globals in modules

* Use `import.meta.url` instead of `__filename` in `src` files

* Prepare fixtures runner for `import.meta.url`

* Use `import.meta.url` instead of `__filename` in `test/index` files

* Remove `__dirname` from remaining test files

dirname

* Avoid using `module` in `src` files

* Avoid using `require` in `src` files

* Avoid using `require` in `test` files

* Update `@types/node`

* Compile dynamic import in `@babel/node`

* Fix windows

* Use `@babel/plugin-proposal-dynamic-import` from npm
2021-03-05 19:55:36 +01:00

55 lines
1.2 KiB
JavaScript

import { runCodeInTestContext } from "..";
import { fileURLToPath } from "url";
const filename = fileURLToPath(import.meta.url);
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";
`,
{
filename: `${filename}.fake1`,
},
);
expect(global.foo).toBe("outer");
runCodeInTestContext(
`
expect(global.foo).toBe("inner");
`,
{
filename: `${filename}.fake2`,
},
);
} finally {
delete global.foo;
runCodeInTestContext(
`
delete global.foo;
`,
{
filename: `${filename}.fake3`,
},
);
}
});
it("should print correct trace position when error is thrown in the first line", () => {
const opts = {
filename: `${filename}.fake4`,
};
runCodeInTestContext(
`try { throw new Error() } catch (e) {
opts.stack = e.stack
}
`,
opts,
);
expect(opts.stack).toContain(opts.filename + ":1:13");
});
});