cucumber / cucumber-js

Cucumber for JavaScript
https://cucumber.io
MIT License
5.02k stars 1.09k forks source link

Typescript error line number not correct when importing @playwright/test #2307

Closed Konubinix closed 12 months ago

Konubinix commented 12 months ago

πŸ‘“ What did you see?

A file loaded using --require file.ts and containing an error leads to a correct stack track, until I start using @playwright/test. I that case, it looks like it shows the line number of the javascript file (but I'm not sure).

βœ… What did you expect to see?

I would expect that using @playwright/test does not change the behavior of the stack trace.

πŸ“¦ Which tool/library version are you using?

I use the official example https://github.com/cucumber/cucumber-js-examples/tree/main/examples/typescript-node .

I tried with the version specified in the package-lock (8.0.0-rc.2) and with an updtated version (9.3.0).

πŸ”¬ How could we reproduce it?

I forked the examples to show the problematic behavior.

  1. git clone https://github.com/Konubinix/cucumber-js-examples
  2. cd examples/typescript-node
  3. npm i
  4. npm test

You should see the expected error in somefile.ts at line 4

image

  1. uncomment the use of @playwright/test in somefile.ts. Its content should then become:
import { expect as pwExpect } from "@playwright/test";
let a = pwExpect;
class A {}
throw new Error();
  1. Run npm test again and see that the error is now at line 7

image

I expected the error to point to the line 4.

πŸ“š Any additional context?

Transpiling the file shows that the error is at line 10 in the javascript file, so it might be more complicated that a simple misuse of sourcemap.

image

Konubinix commented 12 months ago

I realized something more about this behavior.

All the files loaded after somefile.ts have the issue, but all the files loaded before don't.

To show this, comment the error in somefile.ts and create someotherfile.ts with an error in it.

You should have the following contents.

somefile.ts

import { expect } from "@playwright/test";
let a = expect;
class A {}
// throw new Error();

someotherfile.ts:

class B {}
throw new Error();

Now, with this content in cucumber.js

module.exports = {
  default: [
    "--require ./somefile2.ts",
    "--require ./somefile.ts",
    "--require-module ts-node/register",
    "--require features/**/*.ts",
    "--publish-quiet",
  ].join(" "),
};

npm test gives image

Which is the correct line number,

but try reversing the two requires, so that cucumber.js now looks like this

module.exports = {
  default: [
    "--require ./somefile.ts",
    "--require ./somefile2.ts",
    "--require-module ts-node/register",
    "--require features/**/*.ts",
    "--publish-quiet",
  ].join(" "),
};

And now, npm test gives image

It points to the line 4, while IΒ still expect the line 2.

Once again, it is in fact not the value in the transpiled javascript file, as you can see issuing the following command.

$ npm exec tsc somefile2.ts && cat -n somefile2.js | grep Error
     6  throw new Error();
Konubinix commented 12 months ago

After some digging, I realized that I can reproduce the issue with playwright alone, without cucumber. I guess this is not a cucumber issue after all, so I am closing this issue, in favor of microsoft/playwright#26346.

In any case, if someone can give me a hint about what to look into to fix this issue, I would be very welcome.