Mocha + TypeScript:不能在模块外使用 import 语句

Mar*_*mal 22 javascript mocha.js node.js typescript ecmascript-6

我正在观看此视频以了解如何向 Express 路由添加一些简单的测试,但在执行测试时出现各种错误。错误是:

import * as chai from 'chai';

^^^^^^

语法错误:不能在模块外使用导入语句

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

我使用 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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是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();
                });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

这是我的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"
  },
Run Code Online (Sandbox Code Playgroud)

所以……有什么建议吗?我应该更改通用 JS 模块吗?提前致谢

st_*_*nov 22

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

这对我有帮助:

tsconfig.json

替换了这个:

"module": "esnext", 
Run Code Online (Sandbox Code Playgroud)

有了这个:

"module": "commonjs",
Run Code Online (Sandbox Code Playgroud)

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


Mar*_*mal 15

感谢@types/chai-http – 无法使用 ES6 导入 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"]
  }
Run Code Online (Sandbox Code Playgroud)

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

"test": "env TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha --require ts-node/register 'src/test/**/*.ts'",
Run Code Online (Sandbox Code Playgroud)

最后我改变了测试文件,如:

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

// Assertions
chai.should();

chai.use(require('chai-http'));
Run Code Online (Sandbox Code Playgroud)

好吧,运行测试现在可以工作了。


小智 9

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

{
  "extension": ["ts"],
  "timeout": 5000,
  "exit": true,
  "require": "ts-node/register"
}
Run Code Online (Sandbox Code Playgroud)

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