草庐IT

jest-jasmine

全部标签

javascript - AngularJS + Jasmine : Comparing objects

我刚开始为我的AngularJS应用程序编写测试,并在Jasmine中这样做。这里是相关的代码片段客户端Controller:'usestrict';adminConsoleApp.controller('ClientController',functionClientController($scope,Client){//Getlistofclients$scope.clients=Client.query(function(){//preselectfirstclientinarray$scope.selected.client=$scope.clients[0];});//nec

javascript - 如何使用 Jest 模拟 JavaScript 'window' 对象?

我需要测试一个在浏览器中打开新标签页的功能openStatementsReport(contactIds){window.open(`a_url_${contactIds}`);}我想模拟window的open函数,这样我就可以验证传递给open函数的URL是否正确。使用Jest,我不知道如何模拟window。我试图用模拟函数设置window.open,但这种方式不起作用。下面是测试用例:it('thecorrectURLiscalled',()=>{window.open=jest.fn();statementService.openStatementsReport(111);exp

javascript - 在 Jest 中模拟全局变量

在Jest中有什么方法可以模拟全局对象,例如navigator或Image*?我几乎放弃了这一点,并将其留给了一系列可模拟的实用方法。例如://Utils.jsexportisOnline(){returnnavigator.onLine;}测试这个小函数很简单,但很麻烦,而且根本不确定。我可以完成75%的路程,但这是我所能达到的极限://Utils.test.jsit('knowsifitisonline',()=>{const{isOnline}=require('path/to/Utils');expect(()=>isOnline()).not.toThrow();expect

javascript - 我如何使用 jasmine 监视 getter 属性?

我如何使用jasmine监视getter属性?varo={getfoo(){},};spyOn(o,'foo').and.returnValue('bar');//Doesn'twork.这也不起作用AFAICT:spyOn(Object.getOwnPropertyDescriptor(o,'foo'),'get').and.returnValue('bar'); 最佳答案 从Jasmine2.6开始,这可以通过spyOnProperty实现。.要监视foo属性的访问器,请执行以下操作:spyOnProperty(o,'foo')

javascript - karma : Uncaught ReferenceError: require is not defined 中的 Jasmine 测试

Karma无法识别JSFileSpec.js文件中的“require”语句。运行karma.conf.js:(function(){describe("DummyEmittercreation",function(){returnit("creation",function(){varDummyEmitter=require('Util.DummyEmitter');vardummy=newDummyEmitter('someName');returnexpect(dummy).toBeDefined();});});})();ReferenceError:require未定义

javascript - 使用 Jest 只运行一次测试

我只想用Jest运行一个测试。我使用it.only或describe.only,但它仍然运行大量测试。我认为它运行了self上次提交以来的所有测试,但它不应该在显式设置only标志的情况下出现这种行为,对吧?是什么导致了这种行为?我该如何运行单个测试? 最佳答案 Jestparallelizestestrunsanditdoesn'tknowupfrontwhichtestsitshouldrunandwhichitshouldn'trun.Thismeanswhenyouuse"fit",itwillonlyrunonetesti

javascript - 引用错误 : module is not defined - Karma/Jasmine configuration with Angular/Laravel app

我有一个现有的Angular/Laravel应用程序,其中Laravel充当仅提供JSON数据的Angular前端的API。加载Angular应用程序的页面index.php目前由Laravel提供服务。从那里开始,Angular接管了工作。我很难尝试开始使用Karma/Jasmine。当使用karmastart或karmastartkarma.conf.js从项目的根目录运行我的测试时,出现以下错误:ReferenceError:模块未定义完整输出:INFO[karma]:Karmav0.12.28serverstartedathttp://localhost:9876/INFO[l

javascript - Jasmine 2.0 async done() 和 angular-mocks inject() 在同一个测试中 it()

我通常的测试用例看起来像it("shouldsendgetrequest",inject(function(someServices){//sometest}));Jasmine2.0异步测试应该是这样的it("shouldsendgetrequest",function(done){someAsync.then(function(){done();});});如何在一个测试中同时使用done和inject? 最佳答案 这应该有效;当我更新到Jasmine2.0时遇到了同样的问题it("shouldsendgetrequest",f

javascript - 是否有 Jasmine 匹配器来比较对象的属性子集

我有一个对象可以根据我的测试行为进行扩展,但我想确保原始属性仍然存在。varexample={'foo':'bar','bar':'baz'}varresult=extendingPipeline(example)//{'foo':'bar','bar':'baz','extension':Function}expect(result).toEqual(example)//failsmiserably我想要一个在这种情况下会通过的匹配器,按照以下行:expect(result).toInclude(example)我知道我可以编写自定义匹配器,但在我看来,这是一个很常见的问题,应该已经

javascript - Angular karma Jasmine 错误 : Illegal state: Could not load the summary for directive

我正在开发githubrepository(使用angular7和angular-cli),我在master分支中对Karma和Jasmine进行了一些测试。现在我正在尝试添加延迟加载功能,问题是,之前通过的测试现在没有通过。这很有趣,因为只有延迟加载模块的测试失败了......这是代码和错误:import{async,TestBed}from'@angular/core/testing';import{APP_BASE_HREF}from'@angular/common';import{AppModule}from'../../app.module';import{HeroDetai