gpt4 book ai didi

webpack - 导入*为( Jasmine spyOn)不可写

转载 作者:行者123 更新时间:2023-12-04 14:54:02 31 4
gpt4 key购买 nike

升级到babel 7.1.5后,当我使用import * as时,我的测试失败。

test.spec.js

import * as Helper from "../../../../src/renderer/modules/Helper";

describe('Testing', () => {
it('Should import correctly', () => {
console.log(Helper.test()) // a
spyOn(Helper, 'test').and.returnValue('b');
});
});


Helper.js

function test() {
return 'a'
}

export {test}


错误

'Upgrade.spec.js (7:8)', 'a'

Error: <spyOn> : test is not declared writable or has no setter
Usage: spyOn(<object>, <methodName>)
at <Jasmine>
at UserContext.it (webpack:///./test/unit/specs/renderer/Upgrade.spec.js?:7:5)
at <Jasmine>

最佳答案

来源:Can webpack 4 modules be configured as to allow Jasmine to spy on their members?

有一个spyOnProperty,通过将accessType参数设置为'get'可以将属性视为只读。

您的设置将如下所示

import * as mod from 'my/module';
//...
const funcSpy = jasmine.createSpy('myFunc').and.returnValue('myMockReturnValue');
spyOnProperty(mod, 'myFunc', 'get').and.returnValue(funcSpy);

关于webpack - 导入*为( Jasmine spyOn)不可写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53296840/

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