问题我一直在使用Jest和Enzyme使用很棒的StyledComponents为我的React组件构建编写测试图书馆。但是,由于我实现了主题化,所以我的所有测试都失败了。让我举一个例子。这是我的LooksBrowser的代码组件(我删除了所有导入和prop-types以使其更具可读性):constLooksBrowserWrapper=styled.div`position:relative;padding:0056.25%;`;constCurrentSlideWrapper=styled.div`position:absolute;top:0;left:0;z-index:2;`
考虑这种常见情况:vara={b:{c:function(){}}}我想监视c,虽然它很容易:spyOn(a.b,'c');但是它创建了一个spy但它不起作用。没有显示任何错误,调试时我可以看到有一个spy。如何监视嵌套方法?更新输出为:Object[objectObject]没有方法'tohaveBeenCalledWith' 最佳答案 我猜错字是问题所在;正如您概述的那样,监视嵌套函数效果很好。注意大小写:Jasmine函数是toHaveBeenCalled()。由于您编写了tohaveBeenCalled()错误消息是有意义的
我正在使用jest进行测试。我正在使用react和redux,我有这个Action:functiongetData(id,notify){return(dispatch,...)=>{dispatch(anotherFunction());Promise.all(['resource1','resource2','resource3']).then(([response1,response2,response3])=>{//...handleresponses}).catch(error=>{dispatch(handleError(error));}};}我一直在Jest文档中寻找如
我希望运行者在第一次失败后停止,而不是运行所有测试。 最佳答案 这是一个hack,但您可以通过在第一次测试之前插入此脚本来做到这一点;//aftereverytesthasrunafterEach(function(){//checkifanyhavefailedif(this.results_.failedCount>0){//ifso,changethefunctionwhichshouldmovetothenexttestjasmine.Queue.prototype.next_=function(){//toinsteads
我在一些Jasmine测试规范中有一个自定义匹配器,形式如下:this.addMatchers({checkContains:function(elem){varfound=false;$.each(this.actual,function(actualItem){//Checkiftheseobjectscontainthesameproperties.found=found||actualItem.thing==elem;});returnfound;}});当然,actualItem.thing==elem实际上并没有比较对象内容——我必须使用Objectcomparisonin
我正在为angularjs工厂编写一些测试,但有些期望不起作用,我真的不知道为什么。这是我的工厂(其中的一部分)。'使用严格';angular.module('myAppMod').factory('Person',function(BaseModel){returnBaseModel.extend({getfullname(){varname=[];if(this.first_name){name.push(this.first_name);}if(this.person_extra&&this.person_extra.middle_name){name.push(this.per
我有一个带有默认导出和命名导出的ES6模块:/**/src/dependency.js**/exportfunctionutilityFunction(){returnfalse;}exportdefaultfunctionmainFunction(){return'foo';}它被第二个ES6模块使用:/**/src/myModule.js**/importmainFunction,{utilityFunction}from'./dependency';//EDIT:Fixedsyntaxerrorincodesample//exportdefaultmyModule(){expor
在与揭示性模块模式短暂接触后,我开始意识到单元测试模块的挫折。但是,我无法确定这是否是我测试模块的方法,或者是否有某种形式的解决方法。考虑以下代码:varmyWonderfulModule=(function(){functionpublicMethodA(condition){if(condition==='b'){publicMethodB();}}functionpublicMethodB(){//...}return{methodA:publicMethodA,methodB:publicMethodB}}());如果我想测试(使用Jasmine)从publicMethodA到
我是Javascript的新手,刚开始学习AngularJS,但我的大部分测试用例都与我发现的一些很好的示例一起使用。不幸的是,我似乎找不到任何可以帮助我测试当前案例的东西。我正在使用一个模拟服务测试一个Controller,该服务的方法返回一个promise。我希望模拟服务返回一个错误,以便在Controller方法中执行“.catch”block。我可以通过几种方式判断它没有被正确调用:我正在使用istanbul用于代码覆盖,它告诉我我没有覆盖“catch”'.catch'block中的代码没有被执行,据我通过调试得知被测Controller,具体需要测试$scope.login中
当使用jest进行测试时,我看到属性innerText未定义,而在测试中它具有正确的值。it('gettextfromdiv',()=>{constdiv=document.createElement('DIV')div.innerHTML='abc'console.log('innerText',div.innerText)//undefinedconsole.log('textContent',div.textContent)//'abc'//expect(getTextFromDiv(div).length).toMatchSnapshot()})但是当使用不在Jest测试中的相