首页 > 解决方案 > 有没有办法使用 Jasmine 在 NodeJs 上对 es6 模块进行单元测试?

问题描述

我一直在研究这个几天,并没有发现任何有用的东西。

具体来说,我想要的是将我的源代码转换为 es6 以外的东西并对其进行测试。

// Class1.js

// The use of 'export' makes this an es6 module.
export class Class1 {
   getValue() {
      return 42;
   }
}


// Class1.spec.js

// Jasmine imports its spec files using CommonJS 'require'

// Therefore this import will not work under NodeJs and will fail with the error that
// 'import' cannot be used in a CommonJS module.
import { Class1 } from './Class1';

describe('Class1', function() {
   it('getValue returns 42'), function() {
      let c1 = new Class1();
      expect(c1.getValue()).toBe(42);
   });
});

标签: javascriptnode.jsjasmine

解决方案


推荐阅读