karma-jasmine

How to test code inside $(window).on(“load”, function() {}); in Jasmine

余生颓废 提交于 2020-01-15 04:20:29
问题 I have a javascript below, which appends a DIV on page load and hides it after 3 sec. var testObj = { initialize: function() { var that = this; $(window).on("load", function() { (function ($) { //Append Div $('body').append("<div>TEST</div>"); })(jQuery); that.hideAppendedDiv(); }); }, hideAppendedDiv: function() { //Hide appended Div after 3s setTimeout(function(){ $("div").hide(); }, 3000); } }; //call Initialize method testObj.initialize(); How to write Jasmine test cases for the methods

How to mock window.screen.width in Angular Unit Test with Jasmine

血红的双手。 提交于 2020-01-14 13:51:09
问题 I have a BreakpointService, which tells me - depending on the screen width - in which SidebarMode (closed - minified - open) I should display my Sidebar. This is the main part of the service: constructor(private breakpointObserver: BreakpointObserver) { this.closed$ = this.breakpointObserver.observe(['(min-width: 1024px)']).pipe( filter((state: BreakpointState) => !state.matches), mapTo(SidebarMode.Closed) ); this.opened$ = this.breakpointObserver.observe(['(min-width: 1366px)']).pipe( filter

karma-typescript: import JS file with Async keyword

不羁岁月 提交于 2020-01-14 07:48:11
问题 I'm using karma-typescript, with this karma config file : karmaTypescriptConfig: { compilerOptions: { target: "es5", lib: ["dom", "es2015", "es2017"] }, bundlerOptions: { transforms: [require("karma-typescript-es6-transform")()] } }, In my spec files, I have this code : import {} from './local/lib.js' In my lib.js, I have this code : async function() {} When executing my tests with npm test , I have this error : ERROR [source-reader.karma-typescript] Error parsing code: Unexpected token (X:Y)

AngularJS: how to test directive which gives focus to element?

半世苍凉 提交于 2020-01-13 13:52:12
问题 Note: the suggested link is an answer to a question about a service, and doesn't give a clear explanation on how to solve this issue I'm trying to setup a karma test for my simple (and working) AngularJS autofocus directive: app.directive('autofocus', function ($timeout) { return { replace: false, link: function (scope, element, attr) { scope.$watch(attr.autofocus, function (value) { if (value) { $timeout(function () { element[0].focus(); console.log('focus called'); }); } } ); } }; }); This

Do I need to unsubscribe from subscriptions in my unit tests?

纵饮孤独 提交于 2020-01-13 08:24:39
问题 If I have a test such as the following: it('should return some observable', async(() => { mockBackend.connections.subscribe((mockConnection: MockConnection) => { const responseOptions = new ResponseOptions({ body: JSON.stringify(/* some response */) }); mockConnection.mockRespond(new Response(responseOptions)); }); service.getSomeObservable() .subscribe(result => { expect(result).toBe(/* expected response */); }); })); Do I need to unsubscribe from the subscription in an afterEach or afterAll

karma start - passing parameters

梦想的初衷 提交于 2020-01-11 05:10:49
问题 Is there a way to pass a parameter thru the Karma command line and then read that somewhere in your tests? For instance, this is what want: karma start -branding="clientX" And then somewhere in my specs I would need to access this variable (I need the "clientX" value). Is this possible at all? 回答1: It is possible to transmit parameters to test cases. It can be a bit tricky. What you can to do is check for __karma__.config.args in you test suite: it("get karma args", function () { console.log(

Unit Testing/mocking Window properties in Angular2 (TypeScript)

两盒软妹~` 提交于 2020-01-10 22:05:29
问题 I'm building some unit tests for a service in Angular2. Within my Service I have the following code: var hash: string; hash = this.window.location.hash; However when I run a test which contains this code, it will fail. It'd be great to utilise all the features of Window, but as I'm using PhantomJs, I don't think this is possible (I have also tried Chrome which yields the same results). In AngularJs, I would have resorted to mocking $Window (or at least the properties in question), but as

Expect not toThrow function with arguments - Jasmine

此生再无相见时 提交于 2020-01-10 12:12:29
问题 I have function that gets 3 arguments. I want to check that this function not throwing an error. I did something like this: expect(myFunc).not.toThrow(); The problem is myFunc need to get arguments. How can I send the arguments? P.S I tried to pass it argument but I got error that myFunc(arg1, arg2, arg3) is not a function. 回答1: toThrow matcher requires function to be passed as argument to expect so you can simply wrap your function call in anonymous function: expect(function() { myFunc(arg1,

Expect not toThrow function with arguments - Jasmine

烂漫一生 提交于 2020-01-10 12:11:22
问题 I have function that gets 3 arguments. I want to check that this function not throwing an error. I did something like this: expect(myFunc).not.toThrow(); The problem is myFunc need to get arguments. How can I send the arguments? P.S I tried to pass it argument but I got error that myFunc(arg1, arg2, arg3) is not a function. 回答1: toThrow matcher requires function to be passed as argument to expect so you can simply wrap your function call in anonymous function: expect(function() { myFunc(arg1,

Gitlab CI running Angular Jasmine tests crashing without error

我的梦境 提交于 2020-01-06 06:32:58
问题 Having just setup Gitlab CI to run the Jasmine tests in my Angular project, I found the pipeline would bomb out with very little indication of the cause. All my tests would pass when run locally using ng test or locally in headless Chrome (having followed docs and another guide to set that up). However, the pipeline would get to around 13/90 tests and then disconnect. Here's a sample of the debug output (with logLevel: config.LOG_DEBUG in karma.conf.js ): HeadlessChrome 74.0.3723 (Linux 0.0.0