导入*为(茉莉花spyOn)不可写 [英] import * as (jasmine spyOn) not writable

查看:74
本文介绍了导入*为(茉莉花spyOn)不可写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After upgrading to babel 7.1.5 my tests fail when i'm using import * as.

test.spec.js

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

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>

推荐答案

来源:有一个spyOnProperty,它可以通过将accessType参数设置为'get'来将属性视为只读.

There's spyOnProperty which allows treating a property as read-only by setting the accessType argument to 'get'.

您的设置将如下所示

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

这篇关于导入*为(茉莉花spyOn)不可写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆