Skip to content

Commit

Permalink
Incorrect trace position in fixture runner (#10566)
Browse files Browse the repository at this point in the history
* test: add test case

* fix: incorrect fixture callsite position
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Dec 4, 2019
1 parent bb6cc61 commit e94da0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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");
});
});

0 comments on commit e94da0d

Please sign in to comment.