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

CLI running tests filtered by line does not work anymore #3049

Closed
sculpt0r opened this issue Jun 5, 2022 · 11 comments
Closed

CLI running tests filtered by line does not work anymore #3049

sculpt0r opened this issue Jun 5, 2022 · 11 comments

Comments

@sculpt0r
Copy link
Contributor

sculpt0r commented Jun 5, 2022

According to docs: https://github.com/avajs/ava/blob/main/docs/05-command-line.md#running-tests-at-specific-line-numbers it is possible to run a specific test by line number.

Please open the StackBlitz linked in the documentation. It should work fine. Then, change the AVA version to 4.3.0 in package.json. Instal with npm i and try to run the test again with npm run test. Now there is an error in the StackBlitz terminal:
image

Also, in a different project with TS, when I tried to run a test with a line filter, I received only some generic information:
image

It would be good to provide some more output to the console - because right now I don't know the reasons why the file can't be parsed? Without the line filter - tests in the file work fine 🤔

@sculpt0r sculpt0r changed the title CLI running tests filtered by line doeasn work anymore CLI running tests filtered by line does not work anymore Jun 5, 2022
@novemberborn
Copy link
Member

The example works locally. Looking at the stack trace that is printed, it contains HTTPS URLs. AVA is not expecting that.

Probably current here is coming back as undefined which I would say is unexpected and due to the StackBlitz environment:

const current = callSite.getFileName();
if (file.startsWith('file://')) {
return current.startsWith('file://') ? file === current : file === pathToFileURL(current).toString();
}
return current.startsWith('file://') ? pathToFileURL(file).toString() === current : file === current;

Are you running into this locally or really are you asking about your TypeScript project? If you modify this line it should contain more details:

this.write(colors.information(`${figures.warning} Could not parse ${this.relativeFile(event.testFile)} for line number selection`));

@sculpt0r
Copy link
Contributor Author

sculpt0r commented Jun 6, 2022

TBH I try to write a CodeLens for VSC extension that run test case from given line. For now, I tried this command with two TS projects I have, but maybe I should try with plain JS project 🤔 After unsuccessful runs I moved to the documentation sample - which works in 3.15.0 - but when I switched in a given sample to 4.3.0 it starts to fail. I'll try it as a local project - thanks :)

BTW, I've run this in debug mode and find out in event message: "SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)". So it looks like my problems with TS 🤔

@novemberborn
Copy link
Member

I've run this in debug mode and find out in event message: "SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)". So it looks like my problems with TS 🤔

We parse the source here:

const ast = acorn.parse(fs.readFileSync(file, 'utf8'), {
ecmaVersion: 11,
locations: true,
sourceType: 'module',
});

I wouldn't expect that specific error coming out of that code…

The implementation is meant to handle JS files. The way this works for TypeScript (with @ava/typescript) is that Node.js applies source maps, so we can translate map the CLI argument to the test declarations in the resulting JavaScript.

I don't know how any dynamic compilation with ts-node impacts this.

@sculpt0r
Copy link
Contributor Author

sculpt0r commented Jun 6, 2022

The exception comes right from the code you highlighted. But only for the TS project. I just verified it on new clean JS project and filter with line numer works fine 👍 Thank you for the help :)

I think I just open a new discussion, as you mentioned here.

Not sure - but this report could be closed or do you think that we need some outcome from it: like removing the stackblitz sample from the docs or just mention there that it works 'online' only for the version < 4?

@novemberborn
Copy link
Member

The exception comes right from the code you highlighted. But only for the TS project. I just verified it on new clean JS project and filter with line numer works fine 👍 Thank you for the help :)

That does sound like a bug. That's locally / in CI right, not with StackBlitz? Do you have a reproduction you could share?

Not sure - but this report could be closed or do you think that we need some outcome from it: like removing the stackblitz sample from the docs or just mention there that it works 'online' only for the version < 4?

There's a PR for updating the examples so I've mentioned it there: #3017 (comment)

@sculpt0r
Copy link
Contributor Author

sculpt0r commented Jun 7, 2022

That does sound like a bug. That's locally / in CI right, not with StackBlitz? Do you have a reproduction you could share?

That is right: it is locally. I try to prepare a sample project and if I won't be able - I try to send an invitation to the private repo - this one I can share :) Just give me some time 🙏

@sculpt0r
Copy link
Contributor Author

sculpt0r commented Jun 8, 2022

So I have two sample projects:

  • ts.zip
    This one I believe is the easiest version of TS + AVA. The results of running ava with filter:

image

  • ts-no-tsnode.zip
    This one have no ts-node neither AVA config. And the same command produce different results:
    image

Also I send you an invitations to the organisation and the repository inside it. The repo is like a monorepo. Could you npm i in the main directory and then in the terminal go to the inner directory src/backend - then npm i again?

One of the test is placed in /engine/src/backend/system/controller-selector.test.ts - so when I runs it like:

npx ava --verbose /Users/sculpt0r/projects/engine/src/backend/system/controller-selector.test.ts:47

I receive:
image

Please notice I changed the ava/lib/worker/line-numbers.js
so it contains additionall try...catch and dummy log:
image

The exception detail indicates to: loc: Position { line: 11, column: 21 }, which contains:

class FakeAuthorizer implements IAuthorizer {

The above class is declared in test file, because it is used as a mock of real dependency to solve auth in the app.

Right now I'm not sure if my troubles comes from invalid TS + AVA setup? If you need additional logs or sth - just let me know.

@novemberborn
Copy link
Member

I haven't had a chance yet to look into this.

@sculpt0r
Copy link
Contributor Author

sculpt0r commented Jul 5, 2022

This feature works fine for JS project, but not for TS.

I'm able to run test file with AVA, but AVA is unable to find any test if the same file is filtered by line number.

To makes thing easier I added the line filter to ava test runner. Then I tried it on TS project with & without precompilation with the same results

@novemberborn
Copy link
Member

@sculpt0r there's a bug in how the source map is applied for your ts.zip reproduction.

For the larger project you shared last month, the problem is that the parser chokes on the implements keyword. Ideally we identify a compatible parser or acorn replacement. We then need to figure out how to select that parser (either automatic, and added to the dependencies, or side-loading like @ava/typescript).

Even with @ava/typescript in pre-compiled mode we still attempt to parse the TypeScript. This is silly and we should parse the JS instead and then rely on the source maps.

I'll open some follow-up issues as well as a PR to fix the bug and report the parser error.

@novemberborn
Copy link
Member

Bugfix: #3061
Error reporting: #3062
Follow-up issues: #3064 & #3065

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants