Mocha + TypeScript:无法在模块外部使用import语句 [英] Mocha + TypeScript: Cannot use import statement outside a module

查看:789
本文介绍了Mocha + TypeScript:无法在模块外部使用import语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看此视频,以了解如何添加一些视频我的Express路线进行了简单的测试,但是执行测试时却遇到各种错误.错误是:

I was watching this video in order to learn how to add some simple tests to my Express routes but I am getting all kind of errors while executing a test. The error is:

从"chai"中将*导入为chai;

import * as chai from 'chai';

^^^^^^^

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

SyntaxError: Cannot use import statement outside a module

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

I have read some similar Stack Overflow questions and GitHub issues but I didn't find a solution for my own application. Finally I found Mocha documentation on GitHub regarding ES modules but it didn't work:

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

I created the app using TypeScript and CommonJS module to transpile, so I added "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha src/test/*.ts" to the package.json scripts but I am getting the same error every time. I am using ts-node as a server.

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

Anyway, this is my tsconfig.json file:

{
    "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吗?预先感谢

So... any suggestions? Should I change Common JS Module? Thanks in advance

推荐答案

感谢 @ types/chai-http –无法使用ES6导入 GitHub问题的答案.

I was able to test thanks to the @types/chai-http – Can't use ES6 import GitHub issue's answer.

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

I added a second TypeScript configuration file tscconfig.testing.json with the following information:

{
    "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'",

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

Finally I changed the test file like:

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

// Assertions
chai.should();

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

好吧,现在运行测试就可以了.

Well, running the tests works now.

这篇关于Mocha + TypeScript:无法在模块外部使用import语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆