【发布时间】:2017-10-11 16:18:12
【问题描述】:
这是我的模块和我要测试的功能:
import aVariable from 'aModule'
export function afunction() {
//do something with the imported aVariable
//calculates result
return result
});
我想为我的 mocha 单元测试模拟 aVariable。
import {afunction} from 'aModule.js'
describe('Tests', () => {
it('should return expected', () => {
expect(afunction()).to.equal(expected);
});
这可能吗?
更新:
使用 babel-rewire-plugin 我的 .babelrc:
{
"env": {
"dev": {
"presets": ["es2015"]
},
"test": {
"plugins": ["rewire"]
}
}
}
当我运行测试时:
meteor test --meteortesting:mocha
我收到此错误:
TypeError: _getServiceUrl(...).__Rewire__ is not a function
当我使用时:
BABEL_ENV=test meteor test --meteortesting:mocha
我明白了:
While processing files with ecmascript (for target web.browser):
/node_modules/rewire/lib/rewire.js:19:15: Filename must be a string
【问题讨论】:
-
你见过 babel 的 rewire 插件吗?它使您可以轻松地做这些事情。如果您不使用 babel,也可以使用 another rewire library。
-
我见过 rewire 但我不知道你可以用 i 模拟导入,我使用 Babel 作为转译器。
标签: javascript unit-testing mocking mocha.js