Open shellscape opened 2 years ago
Are you trying to setup this for ESM or CJS?
If for ESM, then AVA needs different setup. Here's my config:
export default {
environmentVariables: {
SWC_NODE_PROJECT: "./tsconfig.ava.json", // This one is not necessary, just my default tsconfig.json ignores tests, so swc-node will not compile them. This file just adds tests to `include` section of the config.
SWCRC: "true"
},
extensions: {
ts: "module"
},
nodeArguments: [
"--no-warnings",
"--loader=@swc-node/register/esm" // This is the one loader you need instead of swc-node/register
],
files: [
"src/**/*.test.ts"
]
}
Worth to mention that this will not work on Node v20, because nodeArguments
are getting ignored for some reason. To fix this, you'll need to move them to package.json
"scripts": {
"test": "cross-env NODE_OPTIONS=\"--no-warnings --loader=@swc-node/register/esm\" ava --fail-fast"
},
Also, I'm not sure if .swcrc
would be necessary, probably not, but I have one in my project:
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2020"
}
}
Hope this will help.
Running Node v16@latest.
@swc-node/register
doesn't work withAva
out of the box. The instructions in the README aren't comprehensive enough to use straight away.config:
test:
result:
Now, I understand that error message has to do with ESM module support. OK. I'll tell it to use a tsconfig that should work around that:
tsconfig.base.json:
tsconfig.json (used by test):
updated config:
Same result. If I swap
ts-node/register
back in, everything works again. There's either a bit of information left out, or this straight up does not work with Ava. I'd be baffled if it was the latter because I can see tests working here in this repo and they use ava.