How to change return value of jasmine spy?
Try this instead. The API changed for Jasmine 2.0: authService.currentUser.and.returnValue(‘bob’); Documentation: https://jasmine.github.io/tutorials/spying_on_properties
Try this instead. The API changed for Jasmine 2.0: authService.currentUser.and.returnValue(‘bob’); Documentation: https://jasmine.github.io/tutorials/spying_on_properties
This one was interesting. Issue Babel generates properties with only get defined for re-exported functions. utils/serverRequests/index.ts re-exports functions from other modules so an error is thrown when jest.spyOn is used to spy on the re-exported functions. Details Given this code re-exporting everything from lib: export * from ‘./lib’; …Babel produces this: ‘use strict’; Object.defineProperty(exports, “__esModule”, … Read more
flipCounter is just another function, even if it also happens to construct an object. Hence you can do: var cSpy = spyOn(window, ‘flipCounter’); to obtain a spy on it, and do all sorts of inspections on it or say: var cSpy = spyOn(window, ‘flipCounter’).andCallThrough(); var counter = flipCounter(‘foo’, options); expect(cSpy).wasCalled(); However, this seems overkill. It … Read more
My simple understanding of these two functions in react/frontend projects is the following: jest.fn() You want to mock a function and really don’t care about the original implementation of that function (it will be overridden by jest.fn()) Often you just mock the return value This is very helpful if you want to remove dependencies to … Read more
I ended up ditching the default export: // UniqueIdGenerator.js export const uniqueIdGenerator = () => Math.random().toString(36).substring(2, 8); And then I could use and spy it like this: import * as UniqueIdGenerator from ‘./UniqueIdGenerator’; // … const spy = jest.spyOn(UniqueIdGenerator, ‘uniqueIdGenerator’); Some recommend wrapping them in a const object, and exporting that. I suppose you can … Read more
Attention: I am going to oversimplify and maybe even slightly falsify in the upcoming paragraphs. For more detailed info see Martin Fowler’s website. A mock is a dummy class replacing a real one, returning something like null or 0 for each method call. You use a mock if you need a dummy instance of a … Read more
Technically speaking both “mocks” and “spies” are a special kind of “test doubles”. Mockito is unfortunately making the distinction weird. A mock in mockito is a normal mock in other mocking frameworks (allows you to stub invocations; that is, return specific values out of method calls). A spy in mockito is a partial mock in … Read more