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
二进制文件在命令行中运行测试。