Mocha + TypeScript:无法在模块外部使用import语句

时间:2020-04-02 14:21:33

标签: javascript node.js typescript ecmascript-6 mocha

我在观看this video是为了学习如何在Express路由中添加一些简单的测试,但是在执行测试时遇到各种错误。错误是:

从“ chai”导入*作为chai;

^^^^^^^

SyntaxError:无法在模块外部使用import语句

我已经阅读了一些类似的Stack Overflow问题和GitHub问题,但是我没有找到适合自己的应用程序的解决方案。最后,我在GitHub上找到了关于ES模块的Mocha documentation,但没有用:

我使用TypeScript和CommonJS模块进行编译创建了应用程序,因此我在"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha src/test/*.ts"脚本中添加了package.json,但是每次都遇到相同的错误。我使用ts-node作为服务器。

无论如何,这是我的tsconfig.json文件:

{
    "compilerOptions": {
        "sourceMap": true,
        "target": "es6",
        "module": "commonjs",
        "outDir": "./dist",
        "rootDir": "./src"
    },
    "exclude": [
        "node_modules"
    ]
}

这是src/test/mi-route.ts文件:

import * as chai from 'chai';
import * as chaiHttp from 'chai-http';
import server from '../app';

// Assertions
chai.should();

chai.use(chaiHttp);

describe('API Users', () => {
    // Test Route GET
    describe('GET /api/admin/users', () => {
        it('Should return all the users', done => {
            chai.request(server)
                .get('/api/admin/users')
                .end((err, response) => {
                    response.should.have.status(200);
                    response.body.should.be.a('object');
                    done();
                });
        });
    });
});

这是我的package.json脚本:

"scripts": {
    "dev": "ts-node-dev src/app.ts",
    "start": "node dist/app.js",
    "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha src/test/*.ts",
    "build": "tsc -p"
  },

那么...有什么建议吗?我应该更改Common JS Module吗?预先感谢

6 个答案:

答案 0 :(得分:15)

遇到同样的问题,几乎放弃了将Mocha与TypeScript一起使用(在我们的示例中为Angular 9)。

这对我有帮助:

tsconfig.json中:

替换为:

"module": "esnext", 

与此:

"module": "commonjs",

我还在这里找到了一个带有TypeScript的Mocha的工作示例,并使用了tsconfig文件与我的进行了比较: https://medium.com/@RupaniChirag/writing-unit-tests-in-typescript-d4719b8a0a40

答案 1 :(得分:10)

感谢@types/chai-http – Can't use ES6 import GitHub问题的回答,我得以进行测试。

我添加了第二个TypeScript配置文件tscconfig.testing.json,其中包含以下信息:

{
    "compilerOptions": {
      "module": "commonjs",
      "target": "es2015",
      "lib": ["es2017"],
      "declaration": false,
      "noImplicitAny": false,
      "removeComments": true,
      "inlineSourceMap": true,
      "moduleResolution": "node"
    },
    "include": ["scripts/**/*.ts", "src/**/*.ts", "node_modules/lodash-es/**/*.js"]
  }

然后,我将package.json脚本更改为:

"test": "env TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha --require ts-node/register 'src/test/**/*.ts'",

最后,我将测试文件更改为:

import * as chai from 'chai';
import 'chai-http';
import server from '../app';

// Assertions
chai.should();

chai.use(require('chai-http'));

好吧,现在运行测试即可。

答案 2 :(得分:2)

也有这个问题,发现不用切换到commonJS,只需要开启ESM:

npm install --save-dev esm

./node_modules/mocha/bin/mocha -r esm -r ts-node/register "src/**/*Test.ts"

答案 3 :(得分:1)

确保您的项目中有 .mocharc.json

{
  "extension": ["ts"],
  "timeout": 5000,
  "exit": true,
  "require": "ts-node/register"
}

(另见URL after translate to any page

答案 4 :(得分:0)

我最近在我正在进行的一个项目中遇到了几次这个问题。

该项目包含许多较小的模块,这些模块都是用 TypeScript 编写的,并在提交之前进行编译。我已经(显然至少有几次,因为这个链接已经是紫色的)不知何故设法在没有编译的情况下提交了我的模块。当 mocha 尝试执行未提交给版本控制的依赖项的 .js 版本时,这会导致错误。

构建依赖项、提交和更新包解决了我的问题。希望这可以帮助与我处于类似情况的其他人!

答案 5 :(得分:0)

此错误的另一个可能原因:

您的文件类型是 .tsx 而不是 .ts?将其更改为 .ts 将修复错误或添加对 .tsx 文件的适当支持。

相关问题