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

Support ESM test files #5

Closed
novemberborn opened this issue Jan 27, 2020 · 9 comments · Fixed by #36
Closed

Support ESM test files #5

novemberborn opened this issue Jan 27, 2020 · 9 comments · Fixed by #36
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@novemberborn
Copy link
Member

Once avajs/ava#2345 has shipped it should be possible to configure AVA to load ts files as ES modules.

This means we need to import them, instead of using require.

We should also explore if there may be a way to configure this within the typescript configuration.

@novemberborn novemberborn added enhancement New feature or request help wanted Extra attention is needed labels Jan 27, 2020
@cinderblock
Copy link

Is this why I'm getting this error?

  C:\Users\Cameron\git\http-terminator\test\http-terminator\http.ts:3
  import createTests from '../helpers/createTests';
  ^^^^^^

  SyntaxError: Cannot use import statement outside a module

  × test\http-terminator\http.ts exited with a non-zero exit code: 1

  Uncaught exception in test\http-terminator\https.ts

@novemberborn
Copy link
Member Author

Yes, right now the files are loaded using require().

adam-lynch added a commit to adam-lynch/typescript that referenced this issue Apr 23, 2020
@adam-lynch adam-lynch mentioned this issue Apr 23, 2020
@novemberborn novemberborn pinned this issue Apr 25, 2020
@rien

This comment has been minimized.

@novemberborn

This comment has been minimized.

@rien

This comment has been minimized.

@sindresorhus
Copy link
Member

sindresorhus commented Jan 5, 2021

@novemberborn Just so we're on the same page, this issue is about supporting projects that use AVA with @ava/typescript where package.json has "type": "module" and the tsconfig has "module": "es2020", meaning the TS output is native ESM. Correct?

@novemberborn
Copy link
Member Author

@sindresorhus Yes.

@novemberborn
Copy link
Member Author

Having looked at the logic here and in AVA itself, I think the way to do this is first to remove this check:

typescript/index.js

Lines 162 to 166 in 27650db

for (const extension of extensionsToLoadAsModules) {
if (ref.endsWith(`.${extension}`)) {
throw new Error('@ava/typescript cannot yet load ESM files');
}
}

Then instead of immediately using the requireFn here:

return requireFn(rewritten);

We currently always rewrite to js file, so if js is in extensionsToLoadAsModules then just use import().

We then need to document that, since TypeScript compiles to JS files, you need to configure either your package.json#type or AVA itself to load JS files as modules. Configuring the ts extension will cause AVA to disregard @ava/typescript, as per these lines: https://github.com/avajs/ava/blob/a87460acf570a1d6080f35f0ecf2ccb1cac476b9/lib/worker/base.js#L161-L169

@novemberborn
Copy link
Member Author

I think the way to do this is first to remove this check:

typescript/index.js

Lines 162 to 166 in 27650db

for (const extension of extensionsToLoadAsModules) {
if (ref.endsWith(`.${extension}`)) {
throw new Error('@ava/typescript cannot yet load ESM files');
}
}

avajs/ava#2882 reorders it so that the provider has first dibs.

@novemberborn novemberborn self-assigned this Nov 1, 2021
novemberborn added a commit that referenced this issue Nov 1, 2021
novemberborn added a commit that referenced this issue Nov 1, 2021
novemberborn added a commit that referenced this issue Nov 1, 2021
novemberborn added a commit that referenced this issue Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants