是否可以将webpack 4模块配置为允许Jasmine监视其成员? [英] Can webpack 4 modules be configured as to allow Jasmine to spy on their members?
问题描述
我无法让我的测试茉莉花测试套件与webpack 4一起运行.升级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问题表示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
参数设置为'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);
这篇关于是否可以将webpack 4模块配置为允许Jasmine监视其成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!