Any way to modify Jasmine spies based on arguments?

In Jasmine versions 3.0 and above you can use withArgs describe(‘my fn’, function() { it(‘gets user name and ID’, function() { spyOn(externalApi, ‘get’) .withArgs(‘abc’).and.returnValue(‘Jane’) .withArgs(‘123’).and.returnValue(98765); }); }); For Jasmine versions earlier than 3.0 callFake is the right way to go, but you can simplify it using an object to hold the return values describe(‘my fn’, … Read more

Jasmine.js comparing arrays

Just did the test and it works with toEqual please find my test: http://jsfiddle.net/7q9N7/3/ describe(‘toEqual’, function() { it(‘passes if arrays are equal’, function() { var arr = [1, 2, 3]; expect(arr).toEqual([1, 2, 3]); }); }); Just for information: toBe() versus toEqual(): toEqual() checks equivalence. toBe(), on the other hand, makes sure that they’re the exact … Read more

How to access and test an internal (non-exports) function in a node.js module?

The rewire module is definitely the answer. Here’s my code for accessing an unexported function and testing it using Mocha. application.js: function logMongoError(){ console.error(‘MongoDB Connection Error. Please make sure that MongoDB is running.’); } test.js: var rewire = require(‘rewire’); var chai = require(‘chai’); var should = chai.should(); var app = rewire(‘../application/application.js’); var logError = app.__get__(‘logMongoError’); … Read more

How to execute only one test spec with angular-cli

Each of your .spec.ts file have all its tests grouped in describe block like this: describe(‘SomeComponent’, () => {…} You can easily run just this single block, by prefixing the describe function name with f: fdescribe(‘SomeComponent’, () => {…} If you have such function, no other describe blocks will run. Btw. you can do similar … Read more

toBe(true) vs toBeTruthy() vs toBeTrue()

What I do when I wonder something like the question asked here is go to the source. toBe() expect().toBe() is defined as: function toBe() { return { compare: function(actual, expected) { return { pass: actual === expected }; } }; } It performs its test with === which means that when used as expect(foo).toBe(true), it … Read more

How to write unit testing for Angular / TypeScript for private methods with Jasmine

I’m with you, even though it’s a good goal to “only unit test the public API” there are times when it doesn’t seem that simple and you feel you are choosing between compromising either the API or the unit-tests. You know this already, since that’s exactly what you’re asking to do, so I won’t get … Read more

How can I write a test which expects an ‘Error’ to be thrown in Jasmine?

Try using an anonymous function instead: expect( function(){ parser.parse(raw); } ).toThrow(new Error(“Parsing is not possible”)); you should be passing a function into the expect(…) call. Your incorrect code: // incorrect: expect(parser.parse(raw)).toThrow(new Error(“Parsing is not possible”)); is trying to actually call parser.parse(raw) in an attempt to pass the result into expect(…),

tech