How can I import ES6 class modules into Jasmine for testing?
Asked Answered
U

2

8

I want to unit test some ES6 classes that are stored as modules. However, when I try to run my tests, import triggers an error message: Cannot use import statement outside a module which means I can't test my modules.

All the Jasmine examples use the old ES5 syntax with classes defined as functions using the modules.export and then imported using the require function. I've found websites such as this where they seem to use the ES6 and import { module } from 'path' syntax, but I have no idea why this works and why mine doesn't? Is it because I'm not testing using a headless browser? It is because Jasmine doesn't support this functionality natively and I need some other package? Is it because I need to use jasmine-node in some specific way to get it to work? Should I not be using ES6 classes at all?

As its probably obvious I'm a beginner to node and javascript but I wouldn't have thought that just testing the functionality of a class would be this difficult so if anyone could give me some guidance on how to accomplish testing a class module in Jasmine I would be really grateful.

For example I have a module.

// src/myClass.mjs
export class MyClass {
  constructor() { }
}

I have a simple Jasmine unit test.

// spec/myClassSpec.js
import { MyClass } from '../src/myClass.mjs'
describe('my class', function() {
  var myClassInstance
  beforeEach(function() {
    myClassInstance = new MyClass()
  })

  it('is an instance of MyClass', function() {
    expect(myClassInstance).toBeInstanceOf(MyClass)
  })
})
Ummersen answered 24/5, 2020 at 12:17 Comment(4)
Check this answer: https://mcmap.net/q/1426356/-run-javascript-es6-code-in-jasmineFelon
Hey OPKD did the previous answer work for you? I'm in exactly the same position. New to Jasmine, cannot get past the 'Cannot use import statement outside a module' error.Alfi
It didn't work for me. But I think this node --experimental-vm-modules <test_runner_file> works. Basically replace <test_runner_file> with the path to the Jasmine file in node_modules that runs the test and it should work, your package.json also needs to have "type": "module" set.Ummersen
For me node --experimental... did not work. I got ERR_MODULE_NOT_FOUND. Same error when running the git repo in this answer: https://mcmap.net/q/1426356/-run-javascript-es6-code-in-jasmineSilvey
B
2

I may be late to answer however using "import" statement in Nodejs is not as simple as it looks. There are few main differences between require and import (like being async) thus one can not be simply converted into another. **

  • A simple solution is to use rollup

**. Once you have completed your module your can use rollup to bundle this module as a commonjs file. That file can then be used for testing.

To install rollup

npm install rollup --save-dev

After rollup in installed you need to add following commands to the script of "package.json" file

"dist": "rollup -c"

You need to have a rollup.config.js file in the root folder of your application. The content of rollup.config.js are:

import { rollup } from "rollup";

export default {
input: './index.js',
output: {
  file: 'dist/index.js',
  format: 'cjs' //cjs stands for common js file
}

};

Bondsman answered 6/5, 2021 at 11:30 Comment(0)
S
0

Upgrade Node to version 13 or higher. Enable ECMAScript modules in Node by setting "type" field to "module" in package.json:

{
  "type": "module",
  ...
}
Sampan answered 7/4 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.