alsatian icon indicating copy to clipboard operation
alsatian copied to clipboard

Add support for tests written with ES modules

Open alexsmaliy opened this issue 4 years ago • 10 comments

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

alexsmaliy avatar Apr 08 '20 07:04 alexsmaliy

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? :)

jamesadarich avatar Apr 08 '20 12:04 jamesadarich

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" in package.json

alexsmaliy avatar Apr 08 '20 21:04 alexsmaliy

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?

jamesadarich avatar Apr 08 '20 22:04 jamesadarich

Since it looks like you folks are using ts-node, it's not clear if they ever solidified support for ES modules.

alexsmaliy avatar Apr 08 '20 22:04 alexsmaliy

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.

jamesadarich avatar Apr 08 '20 22:04 jamesadarich

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.

alexsmaliy avatar Apr 08 '20 22:04 alexsmaliy

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 :)

jamesadarich avatar Apr 08 '20 22:04 jamesadarich

Might take a look at what it would take!

alexsmaliy avatar Apr 08 '20 22:04 alexsmaliy

An easy solution to this problem is to install the esm package and require it on the command-line before doing anything else.

mwgamble avatar Oct 15 '20 05:10 mwgamble

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! 👍

jamesadarich avatar Oct 30 '20 00:10 jamesadarich