gpt4 book ai didi

javascript - Mocha 模拟导入变量

转载 作者:行者123 更新时间:2023-11-28 10:44:13 25 4
gpt4 key购买 nike

这是我的模块和我想测试的功能:

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);
});

这可能吗?

更新:

在我的 .babelrc 中使用 babel-rewire-plugin:

{
"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-plugin-rewire 节点模块,您可以在测试中执行以下操作:

describe('Tests', () => {
it('should return expected', () => {
afunction.__Rewire__( 'aVariable', 'myStubVariableValue' );
expect(afunction()).to.equal(expected);
afunction.__ResetDependency__( 'aVariable' );
});

关于javascript - Mocha 模拟导入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46693075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com