Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect trace position in fixture runner #10566

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -62,11 +62,12 @@ function runModuleInTestContext(id: string, relativeFilename: string) {
const req = id => runModuleInTestContext(id, filename);

const src = fs.readFileSync(filename, "utf8");
const code = `(function (exports, require, module, __filename, __dirname) {${src}\n});`;
const code = `(function (exports, require, module, __filename, __dirname) {\n${src}\n});`;

vm.runInContext(code, testContext, {
filename,
displayErrors: true,
lineOffset: -1,
}).call(module.exports, module.exports, req, module, filename, dirname);

return module.exports;
Expand Down Expand Up @@ -94,10 +95,11 @@ export function runCodeInTestContext(code: string, opts: { filename: string }) {
// Expose the test options as "opts", but otherwise run the test in a CommonJS-like environment.
// Note: This isn't doing .call(module.exports, ...) because some of our tests currently
// rely on 'this === global'.
const src = `(function(exports, require, module, __filename, __dirname, opts) {${code}\n});`;
const src = `(function(exports, require, module, __filename, __dirname, opts) {\n${code}\n});`;
return vm.runInContext(src, testContext, {
filename,
displayErrors: true,
lineOffset: -1,
})(module.exports, req, module, filename, dirname, opts);
} finally {
process.chdir(oldCwd);
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-helper-transform-fixture-test-runner/test/index.js
Expand Up @@ -35,4 +35,17 @@ describe("helper-transform-fixture-test-runner", function() {
);
}
});
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");
});
});