From 6b6b5efe575e37387a8db2b81783944170123d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 14 Oct 2019 16:05:25 -0400 Subject: [PATCH 1/2] test: add test case --- .../test/index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 2ec3d6bb2cce..b0f220380a4e 100644 --- a/packages/babel-helper-transform-fixture-test-runner/test/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/test/index.js @@ -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"); + }); }); From 80069c9fee64de3b8752d64c8a11e256b8665d25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 16 Oct 2019 13:39:46 -0400 Subject: [PATCH 2/2] fix: incorrect fixture callsite position --- .../babel-helper-transform-fixture-test-runner/src/index.js | 6 ++++-- .../test/index.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) 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 eed9dde11342..0ec7c043c16e 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -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; @@ -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); 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 b0f220380a4e..1d02bf80290f 100644 --- a/packages/babel-helper-transform-fixture-test-runner/test/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/test/index.js @@ -40,7 +40,7 @@ describe("helper-transform-fixture-test-runner", function() { filename: `${__filename}.fake4`, }; runCodeInTestContext( - `try { throw new Error()} catch (e) { + `try { throw new Error() } catch (e) { opts.stack = e.stack } `,