TypeError during Jest’s spyOn: Cannot set property getRequest of # which has only a getter

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

Spying on a constructor using Jasmine

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

What is the difference between jest.fn() and jest.spyOn() methods in jest?

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

How to spy on a default exported function with Jest?

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

Mockito – @Spy vs @Mock

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)