alsatian
alsatian copied to clipboard
Add support for tests written with ES modules
Alsatian 3.2.1
Basic setup:
npm install --global alsatian@latest
mkdir ./dummy
cd ./dummy
npm init --yes
cat << EOF > example.spec.js
import { Expect, Test } from "alsatian";
export class ExampleTestFixture {
@Test()
public exampleTest() {
Expect(1 + 1).toBe(2);
}
}
EOF
alsatian ./example.spec.js
Error:
ERROR LOADING FILE: /home/alex/code/dummy/example.spec.js
/home/alex/code/dummy/example.spec.js:1
import { Expect, Test } from "alsatian";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1063:16)
at Module._compile (internal/modules/cjs/loader.js:1111:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at FileRequirer.require (/home/alex/.nvm/versions/node/v13.12.0/lib/node_modules/alsatian/dist/core/file-requirer.js:5:16)
at TestLoader.loadTestFixture (/home/alex/.nvm/versions/node/v13.12.0/lib/node_modules/alsatian/dist/core/test-loader.js:11:58)
at /home/alex/.nvm/versions/node/v13.12.0/lib/node_modules/alsatian/dist/core/test-set.js:46:80
This looks like you're using esnext
as your module type for typescript
but your version of node isn't set up for it. You can either enable esmodules for node or switch to commonjs
.
I think this support is in node 13 but you have to enable it. Details can be found in node's docshttps://nodejs.org/api/esm.html#esm_ecmascript_modules
Let me know if this works for you or if you need any further help? :)
Poking around a bit more, I guess the gist of my issue is that
- compiling with
"module": "commonjs"
and running the resulting JS using the Alsatian CLI works - trying the Alsatian CLI on the raw TS file does not
- trying the Alsatian CLI on JS output with
"module": "ESNext"
does not wok, either with or without"type": "module"
inpackage.json
Ah OK then, looks like we may not have full support for esm as things stand. I'm going to change this issues name to reflect this if that's ok?
Is this a blocker for you or can you use commonks for now?
Since it looks like you folks are using ts-node, it's not clear if they ever solidified support for ES modules.
Yes I think you're likely correct, may be worth seeing if there is at least a temporary work around with node 14 on the horizon and this becoming a more popular module system if we can't get support from ts-node.
No worries about the ticket! I have a workaround, I'm just bummed that the ecosystem support for modules is so spotty after a couple of years of availability.
Yep, although I guess because of a combination of it being behind an experimental flag for most of that time and dependencies often having an impact on using functionality / backwards compatibility we can have awkward transitions sometimes.
I've added this to our next milestone though so this will get looked at. If you have some time and inclination I would gratefully received aPR for it :)
Might take a look at what it would take!
An easy solution to this problem is to install the esm
package and require it on the command-line before doing anything else.
Nice tip @mwgamble :) I'll give this a test with some other changes for 4.0.0 and add this as a recommendation with some props to you! 👍