![](/img/trans.png)
[英]Typescript with mocha - Cannot use import statement outside a module
[英]Mocha + TypeScript: Cannot use import statement outside a module
我正在观看此视频,以了解如何在我的 Express 路由中添加一些简单的测试,但是在执行测试时我遇到了各种错误。 错误是:
从'chai'导入*作为chai;
^^^^^^
SyntaxError: 不能在模块外使用 import 语句
我已经阅读了一些类似的 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"
]
}
这是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 模块吗? 提前致谢
有同样的问题,几乎放弃使用 Mocha 和 TypeScript(在我们的例子中是 Angular 9)。
这对我有帮助:
在tsconfig.json
:
替换了这个:
"module": "esnext",
有了这个:
"module": "commonjs",
同样在这里,我找到了一个带有 TypeScript 的 Mocha 工作示例,并使用那里的tsconfig
文件与我的进行比较: https : tsconfig
感谢@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"]
}
然后我将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'));
好吧,运行测试现在可以工作了。
也有这个问题,发现不用切换到commonJS,只需要开启ESM:
npm install --save-dev esm
./node_modules/mocha/bin/mocha -r esm -r ts-node/register "src/**/*Test.ts"
确保您的项目中有.mocharc.json
:
{
"extension": ["ts"],
"timeout": 5000,
"exit": true,
"require": "ts-node/register"
}
(另见https://github.com/mochajs/mocha-examples/tree/master/packages/typescript#es-modules )
从我在过去 2 小时内学到的:
{
"type": "module"
}
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
}
}
{
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
]
}
也遇到这个问题,是因为我安装的 mocha 版本只需要 mocharc.json。 不支持任何其他格式,我的 mocha 配置文件是 .mocharc 没有结束 withs .json
我最近在我正在从事的项目中遇到了几次这个问题。
该项目包含许多较小的模块,这些模块都是用 TypeScript 编写的,并在提交之前进行编译。 我已经(显然至少有几次,因为这个链接已经是紫色的)不知何故设法在没有编译的情况下提交了我的模块。 当 mocha 尝试执行未提交给版本控制的依赖项的.js
版本时,这会导致错误。
构建依赖项、提交和更新包解决了我的问题。 希望这可以帮助其他与我处于类似位置的人!
此错误的另一个可能原因:
你的文件类型是.tsx
而不是.ts
? 将其更改为.ts
将修复错误或添加对.tsx
文件的适当支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.