Let's assume that you create a Math.js
component:
javascriptconst sum = (a, b) => a + b const sub = (a, b) => a - b export default sum export { sub }
To spy a non-default function using Jest:
javascriptimport * as Math from './Math.js' test('Mocking up default and non-default', async() => { // Spying on a non default exported function jest.spyOn(Math, 'sub').mockUpImplementation(() => 5) // Spying on the default exported function jest.spyOn(Math, 'default').mockImplementation(() => 5) })
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.