Using Spies and Mocks on complex objects with Jest

I’m fairly new to testing and writing tests for a currently uncovered javaScript codebase using Jest. The code covers some niche use cases, as its conditionally injected and executed by the browser during page load. Anyway, I’m having issues mocking up custom objects. Here’s the function in question: As far as I understand, I need to mock both window.location.pathname to return a string, and I need to mock window.TOOL.cookie.setCookie() as a mock function. Here’s my attempt at the test: The test fails, saying that window.TOOL.cookie.setCookie was called 0 times. I’ve dug into the process, and found that window.location.pathname is executing

Change default timeout for mocha

If we have a unit test file my-spec.js and running with mocha: The default timeout will be 2000 ms. It can be overwritten for partial test with a command line parameter: Is it possible to change the default timeout globally for all tests? i.e. the default timeout value will be different from 2000 ms when you call: Answer By default Mocha will read a file named test/mocha.opts that can contain command line arguments. So you could create such a file that contains: Whenever you run Mocha at the command line, it will read this file and set a timeout of

Global variables in Karma test runner

I have a global variable defined in my main template, which I use to store information bits from the back end, such as the environment context path. I can’t move that variable inside a service. How can I expose that variable to Karma when I run the unit tests? Answer You either declare that global variable within your test file: or add a Javascript file where it’s defined to your karma.conf.js file: