Skip to content
Advertisement

Tag: unit-testing

How to return based on actual arguments being sent?

Let’s say we have a function with two arguments. It’s being called many times with different arguments on every call. So, its impossible to stub it with withArgs option. I want to write a stub function which will check the actual arguments being passed (args1, args2) and return a static response with a switch case. Something on the following lines:

How to spy on a recursive function in JavaScript

Note: I’ve seen variations of this question asked in different ways and in reference to different testing tools. I thought it would useful to have the issue and solution clearly described. My tests are written using Sinon spies for readability and will run using Jest or Jasmine (and need only minor changes to run using Mocha and Chai), but the

Error is thrown but Jest’s `toThrow()` does not capture the error

Here is my error codes: As you can see the error did indeed occurred: Function FunctionThatDoesNotExistsInString does not exists in string.. However it is not captured as a pass in Jest. Here is my codes: Answer expect(fn).toThrow() expects a function fn that, when called, throws an exception. However you are calling CheckFunctionExistenceByStr immediatelly, which causes the function to throw before

How to show passed test in Jasmine?

When running jasmine it only presents dot(.) for successful tests, and only verbose if the test fails. My running command is: jasmine-node test.spec.js The result: How to make jasmine display this test result like jasmine should show this text? Answer Use the –verbose flag: Note: jasmine-node doesn’t seem to be actively maintained. The jasmine CLI supports tests run from the

What is the alternative for “toNotEqual” in Jasmine?

I am trying to write Unit Test in Jasmine and in my code, I am comparing two objects for inequality. I am using following code to do it: But getting following error: TypeError: expect(…).toNotEqual is not a function Can anyone please suggest how to resolve this? Answer It could have been more useful if you specified the Jasmine Version you

Advertisement