可以将 webpack 4 模块配置为允许 Jasmine 监视其成员吗? [英] Can webpack 4 modules be configured as to allow Jasmine to spy on their members?

查看:37
本文介绍了可以将 webpack 4 模块配置为允许 Jasmine 监视其成员吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法使用 webpack 4 运行我的测试 jasmine 测试套件.升级 webpack 后,我几乎每次测试都会收到以下错误:

I've been unable to get my test jasmine test suite running with webpack 4. After upgrading webpack, I get the following error for almost every test:

Error: <spyOn> : getField is not declared writable or has no setter 

这是由于我们用来为简单函数创建间谍的常见模式是:

This is due to a common pattern we use to create spys for simple functions is:

import * as mod from 'my/module';
//...
const funcSpy = spyOn(mod, 'myFunc');

我玩过 module.rules[].type 但似乎没有一个选项能奏效.

I've played around with module.rules[].type but none of the options seem to do the trick.

这个 webpack GH issue 表明 ECMA 模块是不可写的,这是有道理的用于网络,但真的没有测试的解决方法吗?

This webpack GH issue indicates ECMA modules are meant to not be writable which makes sense for the web but is there really no workaround for testing?

相关包版本:

"jasmine-core": "2.6.4",
"typescript": "2.5.3",
"webpack": "4.1.1",
"webpack-cli": "^2.0.12",
"karma": "^0.13.22",
"karma-jasmine": "^1.1.0",
"karma-webpack": "^2.0.13",

推荐答案

spyOnProperty 允许通过将 accessType 参数设置为 来将属性视为只读>'获取'.

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

这篇关于可以将 webpack 4 模块配置为允许 Jasmine 监视其成员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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