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

Feature: expose accurate test case location #3561

Closed
robertrossmann opened this issue Nov 9, 2018 · 4 comments
Closed

Feature: expose accurate test case location #3561

robertrossmann opened this issue Nov 9, 2018 · 4 comments
Labels
duplicate been there, done that, got the t-shirt... status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: feature enhancement proposal

Comments

@robertrossmann
Copy link
Contributor

robertrossmann commented Nov 9, 2018

Feature description

I would like to have access to precise location in which a test case has been defined (that is, filename, line, and column).

Possible use cases

  • I would like to create a kind of editor integration where I can accumulate all executed test cases and then be able to have the user click on each test case to take them exactly to the place where that test has been defined
  • To show this location information in case of test failures. Sometimes I noticed that when an error occurs (especially one thrown from setImmediate() hooks) the stack trace contains absolutely no information as to which test exactly has failed and I have to resort to string lookup in the code (using the test's title)

To clarify further with an example, I need to know where it() or test() have been called:

describe('suite', () => {
  // need to get this file's full name and the following line's line and row numbers:
  // /Users/me/project/src/api.test.js:4:3
  it('works', () => true)
})

PoC

I have successfully extracted the precise location of the test case's definition (even with applied source mapping ⚠️) using this crude one-liner inserted at line 27 in this file:

mocha/lib/test.js

Lines 16 to 27 in c6f61e6

function Test(title, fn) {
if (!isString(title)) {
throw new Error(
'Test `title` should be a "string" but "' +
typeof title +
'" was given instead.'
);
}
Runnable.call(this, title, fn);
this.pending = !fn;
this.type = 'test';
}

// lib/test.js:27
this.location = new Error().stack.split('\n').slice(0, 3).pop().trim()

Logging this.location produces the following output:

screen shot 2018-11-09 at 19 24 24

Obviously this needs work, but as you can see even this one-liner got me pretty close to the end result.

Implementation of this feature as I would imagine and need it to actually use it for my goals would entail

  • gathering test case location information for all registered tests
  • exposing this information to reporters

Would you accept pull requests implementing this feature? Are there requirements as to how this must be implemented? Am I allowed to add dependencies (ie. for parsing the stack traces into usable format)?

Thank you for considering! 🙏

@plroebuck
Copy link
Contributor

So you kinda want to reimplement what "vscode" does?
See Issue #3383 screenshots.

@robertrossmann
Copy link
Contributor Author

Well, kind of but not really. The issue you linked only mentions stack traces in case of failed test. I would prefer to know exactly where all test cases are defined so I can feed them to the editor and create clickable interface that takes me directly to the test’s definition.

I cannot do that right now because the row/column location info is not available, only the filename, and even that is not source-mapped in case it was compiled.

@plroebuck
Copy link
Contributor

Well, it won't ever be standard processing by any means, as no one else is going to want to take the speed hit of parsing self-created stacktraces simply to get row/col info for every test.

But feel free to see if you can make your version of "mochatags" work.
Ctags with Vim

@plroebuck plroebuck added the type: feature enhancement proposal label Nov 14, 2018
@JoshuaKGoldberg
Copy link
Member

This can be done somewhat by a custom reporter. Having it be done as a part of Mocha is covered by #1457. So this is somewhat a dup issue. I think. Someone please yell at me with an explanation and examples if I'm wrong. 🙂

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg added duplicate been there, done that, got the t-shirt... status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior labels Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate been there, done that, got the t-shirt... status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

3 participants