【问题标题】:Mocha + TypeScript: Cannot use import statement outside a moduleMocha + TypeScript:不能在模块外使用导入语句
【发布时间】:2020-07-14 13:20:49
【问题描述】:

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

从'chai'导入*作为chai;

^^^^^^

SyntaxError: 不能在模块外使用 import 语句

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

我使用 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 模块吗?提前致谢

【问题讨论】:

  • 也许您应该使用 ts-mocha 而不是 mocha 来运行测试?

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


【解决方案1】:

遇到同样的问题,几乎放弃了使用 Mocha 和 TypeScript(在我们的例子中是 Angular 9)。

这对我有帮助:

tsconfig.json:

替换为:

"module": "esnext", 

用这个:

"module": "commonjs",

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

【讨论】:

  • 将我的文件与链接中的文件进行比较解决了我的问题。谢谢!
  • 我花了一天多的时间把头发拉出来。这解决了一切!!!
【解决方案2】:

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

我添加了第二个 TypeScript 配置文件 tsconfig.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'));

嗯,现在可以运行测试了。

【讨论】:

  • 为我工作,在我修正了您的错字后:tsconfig.testing.json,而不是 tscconfig.testing.json
  • 您还可以将第二个文件置于测试目录:test/tsconfig.json
【解决方案3】:

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

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

(另见https://github.com/mochajs/mocha-examples/tree/master/packages/typescript#es-modules

【讨论】:

    【解决方案4】:

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

    npm install --save-dev esm
    
    ./node_modules/mocha/bin/mocha -r esm -r ts-node/register "src/**/*Test.ts"
    

    【讨论】:

    【解决方案5】:

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

    该项目包含许多较小的模块,这些模块都是用 TypeScript 编写的,并在提交之前编译。我有(显然至少有几次,因为这个链接已经是紫色的)以某种方式设法提交我的模块而无需编译。当 mocha 尝试执行未提交到版本控制的依赖项的 .js 版本时,这会导致错误。

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

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-01
        • 2021-11-27
        • 2020-11-20
        • 2021-08-05
        • 2021-03-10
        • 1970-01-01
        • 2021-02-06
        • 2021-07-05
        相关资源
        最近更新 更多