Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 使用“import”语句而不是“require”调用单个mocha测试`_Testing_Reactjs - Fatal编程技术网

Testing 使用“import”语句而不是“require”调用单个mocha测试`

Testing 使用“import”语句而不是“require”调用单个mocha测试`,testing,reactjs,Testing,Reactjs,我有一个简单的测试,我想在前端测试一个服务及其方法。在后端,我使用require()获取模块,但前端使用webpack和import 我的测试: const testee = require('../network-template.service'); describe('getTemplates', function () { it('shall return templates from server', function (done) { console.log

我有一个简单的测试,我想在前端测试一个服务及其方法。在后端,我使用
require()
获取模块,但前端使用webpack
import

我的测试:

const testee = require('../network-template.service');

describe('getTemplates', function () {
    it('shall return templates from server', function (done) {
        console.log(testee);
        done();
    });
});
我的测试班:

import fetch from 'isomorphic-fetch';

const ENDPOINT = 'http://localhost:3000/api/network-templates';

class NetworkTemplateService {
    getTemplates(){
        return fetch(ENDPOINT, {
            method: 'GET',
            headers: {
                'Accept' : 'application/json'
            },
            body: JSON.stringify(ports)
        })
            .then(response => ({response}))
            .catch(error => ({error}));
    }

有没有一种简单快速的方法来运行这个测试?我喜欢在后端我不编译任何东西,并且可以在没有任何设置的情况下立即运行所有测试。

也许可以将这一行添加到您的
包.json中,并使用babel预编译它

..."scripts": {
        "test":"mocha --compilers js:babel-core/register ./test/example_test.js"
}...

也许可以将这一行添加到您的
包.json中,并用babel预编译它

..."scripts": {
        "test":"mocha --compilers js:babel-core/register ./test/example_test.js"
}...
Babel有一个
require()
钩子,可用于动态传输代码,因此在运行测试之前不必进行任何设置

要安装:

npm i babel-register --save-dev
要使用它,您可以在
mocha.opts
文件中添加此行:

--compilers js:babel-register
或者,您可以将其添加为命令行标志。如果您的
包.json中有
测试
脚本

"scripts": {
    "test" "./node_modules/.bin/mocha --compilers js:babel-register"
}
Babel有一个
require()
钩子,可用于动态传输代码,因此在运行测试之前不必进行任何设置

要安装:

npm i babel-register --save-dev
要使用它,您可以在
mocha.opts
文件中添加此行:

--compilers js:babel-register
或者,您可以将其添加为命令行标志。如果您的
包.json中有
测试
脚本

"scripts": {
    "test" "./node_modules/.bin/mocha --compilers js:babel-register"
}

如果你想在前端使用导入和测试你的组件,我认为你必须预编译它,这样你才能进行测试。如果你想在前端使用导入和测试你的组件,我认为你必须预编译它,这样你才能进行测试。thx,它工作得很好。我正在使用Jetbrains PhpStorm。出于这个目的,我必须在全球范围内安装
babel register
,而不是
mocha.opts
我在
运行/调试配置中使用了
额外的mocha选项。我使用IntelliJ进行JavaScript开发,我还喜欢通过IDE运行测试。根据我的经验,
mocha.opts
文件是受人尊重的,因此您可以按照自己的意愿进行操作。我倾向于使用
mocha.opts
的原因是,我还喜欢不时使用
mocha
二进制文件在命令行中运行测试。thx,它工作得很好。我正在使用Jetbrains PhpStorm。出于这个目的,我必须在全球范围内安装
babel register
,而不是
mocha.opts
我在
运行/调试配置中使用了
额外的mocha选项。我使用IntelliJ进行JavaScript开发,我还喜欢通过IDE运行测试。根据我的经验,
mocha.opts
文件是受人尊重的,因此您可以按照自己的意愿进行操作。我倾向于使用
mocha.opts
的原因是,我还喜欢不时使用
mocha
二进制文件在命令行中运行测试。