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:
Tag: unit-testing
How to test snapshots with Jest and new React lazy 16.6 API
I have to components imported with the new React lazy API (16.6). In my tests, I’m doing the snapshots of this component. It’s a very straightforward test: In the logs, the test is failing with this error: Do I have to wrap in every single test suite with <Suspense>…? If I do that, I only see in the snapshot the
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
How can I test for object keys and values equality using Jest?
I have a mapModule where I import components and export them: How can I test that mapModule has the correct exported keys, values and that they are not null or undefined? Answer In version 23.3.0 of jest, expects a string. Use: result is passing test
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
How to change the behaviour of a mocked import?
I am quite confused with mocking in Jest an how to unit test the implementations. The thing is i want to mock different expected behaviours. Is there any way to achieve this? as imports can be only on the top of the file and to be able to mock something it must be declared before the import. I have also
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
How to test properly React Dropzone onDrop method
I’m testing React Dropzone and I need to check the onDrop function. This function has two parameters (acceptedFiles and rejectedFiles). I’m mocking the files like this: Then in my test, I do that: This is my onDrop function: The expected result would be that handleOnDrop returns acceptedFiles but returns rejectedFiles and I don’t know why. Mime type it’s ok and
Testing code that uses an IntersectionObserver
I have a JavaScript component in my application that handles infinite scroll pagination, and i’m trying to rewrite it to use the IntersectionObserver, as described here, however I’m having issues in testing it. Is there a way to drive the behavior of the observer in a QUnit test, i.e. to trigger the observer callback with some entries described in my