Mocking and modifying original function using native node mocking

30 Views Asked by At

The latest versions of node have a built in test runner and mocking capabilities. I see the function documentation for mock.method() reads that the default argument is the original method.

implementation | An optional function used as the mock implementation for object[methodName]. Default: The original method specified by object[methodName].

Is there a way to access that original method to then modify it?

I tried creating a new instance of the method but this seems to trigger an infinite loop since I assume the mocked method is being triggered.

import * as Pino from "pino"

const logs: string[] = []
const dest = { write: (data: string) => logs.push(data) }

// Function in original src code just uses `Pino.pino(transport)`
mock.method(Pino, 'pino', (transport: any) => Pino.pino(transport, dest))
0

There are 0 best solutions below