Why is testDouble not able to mock a 3rd party function?

237 Views Asked by At

I have a local module with a function that I want to mock using tetDouble for nodeJS

This is the function that I want to test:

import {supportsAPL} from "skills-lib"

export function example(thing: any): boolean {
    if (!supportsAPL(thing)) {
        throw new Error("numbers only!")
    }
    return true
}

And this is the actual test:

const td = require("testdouble")
require("testdouble-jest")(td, jest)

const supportsAPL = td.replace("skills-lib")
import {example} from "../lambda/custom/numbers-only"
// const example = require("../lambda/custom/numbers-only")

describe("ex", () => {
    it("ex2", async () => {
        td.when(supportsAPL("a string")).thenReturn(true) // tee-hee, this is silly
        const result = example("a string")
        expect(result).toMatch("true")
    })
})

The code runs fine but when I run the test I get:

supportsAPL is not a function
TypeError: supportsAPL is not a function

The only way I found to circumvent this issue is by creating a wrapper for the local-module and mock the wrapper instead.

Does anyone have a better way to handle this issue?

0

There are 0 best solutions below