avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Report does not contain a reference to the location of the test when using macros or helpers from another file #2722

Open MatthiasKunnen opened 3 years ago

MatthiasKunnen commented 3 years ago

When using a macro, the failure report does not include a line number that could be used to navigate to the test.

Example of the report:

  Number must be 5

  Difference:

  - 4
  + 5

  › macro (typescript/helper/macro.ts:4:7)

Reproduction: https://github.com/MatthiasKunnen/ava-line-number-reproduction. AVA version: 3.15.0. AVA configuration: See repo.

Proposal
Capture the line number when test is invoked and either add it to the stack trace or add it above the diff, e.g. typescript/spec/something.spec.ts:7

Notes Interestingly, the report is a bit different when the macro is inside vs outside the spec file. This can be seen on the root commit in the reproduction repository. Example diff with the + side representing a macro outside the spec file:

Number must be 5

-typescript/spec/something.spec.ts:4

-3: const macro: Macro<[number]> = (t, number) => {
-4:     t.is(number, 5);                           
-5: }

 Difference:

 ﹣ 4
 ﹢ 5

-› macro (typescript/spec/something.spec.ts:4:7)
+› macro (typescript/helper/macro.ts:4:7)
novemberborn commented 3 years ago

I believe this is a known issue, or at least I'm not surprised by it, but I can't immediately find an existing issue for this either.

I believe we try to find the line where the assertion was called. But that must be within the test file. It would be interesting to find the first line from the test file and provide context for that.

MatthiasKunnen commented 3 years ago

I'll have a look and see if I can add the filename and line number of the test to the stack trace. In which file would you like me to add tests regarding this?

novemberborn commented 3 years ago

In which file would you like me to add tests regarding this?

The laziest answer would be the reporter tests, but they're not easy to maintain.

The harder answer is a new self-hosted test in test/, using internal access to the raw result data. But you'll have to do some digging around for that (or leave it to me).

MatthiasKunnen commented 3 years ago

I'll take a stab at it ;)

novemberborn commented 3 years ago

As mentioned in https://github.com/avajs/ava/pull/2723, https://github.com/avajs/ava/pull/2785 adds a test.macro() to declare macros, maybe we can special case the behavior for those objects.