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

Add support for tests written with ES modules #684

Open
alexsmaliy opened this issue Apr 8, 2020 · 10 comments
Open

Add support for tests written with ES modules #684

alexsmaliy opened this issue Apr 8, 2020 · 10 comments
Milestone

Comments

@alexsmaliy
Copy link

alexsmaliy commented Apr 8, 2020

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 alexsmaliy changed the title Wiki stating example no longer works. Wiki starting example no longer works. Apr 8, 2020
@jamesadarich
Copy link
Member

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

@alexsmaliy
Copy link
Author

alexsmaliy commented Apr 8, 2020

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

@jamesadarich
Copy link
Member

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 jamesadarich changed the title Wiki starting example no longer works. Add support for tests written with ES modules Apr 8, 2020
@jamesadarich jamesadarich added this to the 4.0.0 milestone Apr 8, 2020
@alexsmaliy
Copy link
Author

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

@jamesadarich
Copy link
Member

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.

@alexsmaliy
Copy link
Author

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.

@jamesadarich
Copy link
Member

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

@alexsmaliy
Copy link
Author

Might take a look at what it would take!

@jamesadarich jamesadarich self-assigned this Aug 9, 2020
@jamesadarich jamesadarich removed their assignment Aug 30, 2020
@jamesadarich jamesadarich modified the milestones: 4.0.0, 5.0.0 Aug 30, 2020
@mwgamble
Copy link

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

@jamesadarich
Copy link
Member

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

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

No branches or pull requests

3 participants