导入*为(茉莉花spyOn)不可写

Pet*_*r I 6 jasmine webpack babeljs

升级到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');
    });
});
Run Code Online (Sandbox Code Playgroud)

Helper.js

function test() {
    return 'a'
}

export {test}
Run Code Online (Sandbox Code Playgroud)

错误

'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>
Run Code Online (Sandbox Code Playgroud)

小智 5

来源:是否可以将webpack 4模块配置为允许Jasmine监视其成员?

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

您的设置将如下所示

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

  • 如果我尝试他的解决方案,我会得到“myFunc 未声明可配置”...知道为什么吗? (10认同)
  • 我现在遇到错误:“未声明可配置” (5认同)
  • 不再起作用了。 (2认同)