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

Magic assert doesn't seem to work with "type": "module" #2719

Closed
Filyus opened this issue Mar 14, 2021 · 7 comments · Fixed by #2741
Closed

Magic assert doesn't seem to work with "type": "module" #2719

Filyus opened this issue Mar 14, 2021 · 7 comments · Fixed by #2741
Milestone

Comments

@Filyus
Copy link

Filyus commented Mar 14, 2021

Just add "type": "module" to package.json and run the failing test in ES-module, you don't see any code in the logs. I don't want to install something like Babel.
ava 3.15.0
Node 12.20.0

test.js:

import test from 'ava';
test("test magic assert", t => {
  t.is(1, 2); 
});

log:

  test magic assert

  Difference:

  - 1
  + 2

  » file:///test.js:3:5

  ─

  1 test failed

Related: #2293

@Filyus Filyus changed the title No code in log with "type": "module" Magic assert doesn't seem to work with "type": "module" Mar 14, 2021
@novemberborn
Copy link
Member

It strikes me that the path is file:///test.js which implies the test file was at your file system's root. That seems odd. Assuming the file does not actually live there, this is probably the error.

I haven't had a chance yet to try and reproduce this but it's on my radar.

@novemberborn novemberborn added this to the 4.0 milestone Mar 28, 2021
@novemberborn
Copy link
Member

I suspect the problem is here:

// Normalize the test file so it matches `callSite.file`.
const relFile = path.relative(process.cwd(), testFile);
const normalizedFile = process.platform === 'win32' ? slash(relFile) : relFile;
for (const line of stack.split('\n')) {
const callSite = stackUtils.parseLine(line);
if (callSite && callSite.file === normalizedFile) {
return {
isDependency: false,
isWithinProject: true,
file: path.resolve(process.cwd(), callSite.file),

I haven't tested it directly, but this came up in my work for #2741.

@novemberborn novemberborn linked a pull request May 17, 2021 that will close this issue
2 tasks
@ghost
Copy link

ghost commented Aug 5, 2021

@novemberborn not sure if it is fixed by #2741. I encountered the same issue today and tried with the master branch. The issue is still reproducible. Same setup as @Filyus, "type": "module" in package.json and everything as es modules.
image

@ghost
Copy link

ghost commented Aug 5, 2021

hmm maybe it's because of the source maps, the files are compiled from typescript and the source maps point to .ts files.

image

@ghost
Copy link

ghost commented Aug 5, 2021

well, forgive my ignorance @novemberborn. Just found out that I would need additional configuration to work with precompiled typescript.

@ghost
Copy link

ghost commented Aug 5, 2021

unfortunately, es modules do not work with the @ava/typescript yet.

I was curious though. My setup has IDE watchers that auto compile the typescript to javascript in the same folder on change. This was a nice setup for me because I can use these auto transpiled codebase directly as the artifact without needing a bundle (at least in dev). I expected the same to work for avajs such that I just reference the transpiled test files and the source maps help with the stack traces.

I was playing with the codebase and seems it works if the check to match stack trace with the test file does not check the extension strictly. I think this would work nicely even if the js is transpiled to another folder, given that source maps are generated during transpile. Find the change I made to try this in screenshot below, I basically changed line 40 to line 41 in serialize-error.js. It loads the files properly and generates the magic asserts with correct trace based on the sourcemap.

image

Do you think this could be something that can be supported, maybe under a configuration flag or so? @novemberborn I think all of the typescript projects that do not bundle should work with this approach without needing @ava/typescript, just if the transpiled files go into another folder, it would be more than just checking the filename without extension.

@novemberborn
Copy link
Member

unfortunately, es modules do not work with the @ava/typescript yet.

If you're game @jiby-gh, it shouldn't be too hard to implement, see avajs/typescript#5 (comment).

Do you think this could be something that can be supported, maybe under a configuration flag or so?

I don't quite follow, sorry. What is your suggestion?

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

Successfully merging a pull request may close this issue.

2 participants