Spy on non-default function using Jest's spyOn()

Let's assume that you create a Math.js component:

javascript
const sum = (a, b) => a + b
const sub = (a, b) => a - b

export default sum
export { sub }

To spy a non-default function using Jest:

javascript
import * 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.