R
roblox-ts•2mo ago
duck

How to mock a library function in jest

I tried doing .spyOn to spy on the function but it didn't work. I don't think .mock works because it requires a module script, and roblox-ts strings will mess up its mockings
13 Replies
duck
duckOP•2mo ago
in my use case, I tried mocking matter's useDeltaTime function to return a deseried delta time value, but I can't find out how to do that
iSentinel
iSentinel•2mo ago
You need to dynamically import I did something like:
export function mockPlayerService(players?: Array<Player>): {
onPlayerAdded: Signal<(player: Player) => void>;
onPlayerRemoved: Signal<(player: Player) => void>;
} {
const onPlayerAdded = new Signal<(player: Player) => void>();
const onPlayerRemoved = new Signal<(player: Player) => void>();

jest.mock(getServices(), () => {
return {
Players: {
GetPlayerByUserId(userId: number): Player | undefined {
return players?.find(player => player.UserId === userId);
},
GetPlayers: () => players ?? [],
PlayerAdded: onPlayerAdded,
PlayerRemoving: onPlayerRemoved,
},
RunService: {
IsServer: () => game.GetService("RunService").IsServer(),
IsStudio: () => game.GetService("RunService").IsStudio(),
},
};
});

return { onPlayerAdded, onPlayerRemoved };
}
export function mockPlayerService(players?: Array<Player>): {
onPlayerAdded: Signal<(player: Player) => void>;
onPlayerRemoved: Signal<(player: Player) => void>;
} {
const onPlayerAdded = new Signal<(player: Player) => void>();
const onPlayerRemoved = new Signal<(player: Player) => void>();

jest.mock(getServices(), () => {
return {
Players: {
GetPlayerByUserId(userId: number): Player | undefined {
return players?.find(player => player.UserId === userId);
},
GetPlayers: () => players ?? [],
PlayerAdded: onPlayerAdded,
PlayerRemoving: onPlayerRemoved,
},
RunService: {
IsServer: () => game.GetService("RunService").IsServer(),
IsStudio: () => game.GetService("RunService").IsStudio(),
},
};
});

return { onPlayerAdded, onPlayerRemoved };
}
and then my code looked like:
describe("a test", () => {
let someFileThatImportsPlayerService: Example;

beforeEach(() => {
const { Example } = import("path/to/some-file-that-imports-player-service").expect();
someFileThatImportsPlayerService = Example;
});
describe("a test", () => {
let someFileThatImportsPlayerService: Example;

beforeEach(() => {
const { Example } = import("path/to/some-file-that-imports-player-service").expect();
someFileThatImportsPlayerService = Example;
});
you need to ensure you mock it before you import it
duck
duckOP•2mo ago
what is getServices?
lisachandra
lisachandra•2mo ago
just mock the module that exports getdeltatime no?
const useDeltaTimeMock = jest.fn()
beforeAll(() => {
const useDeltaTimeModule = getModuleByTree(...$getModuleTree("@rbxts/matter/lib/hooks/useDeltaTime"))
jest.mock<typeof import("@rbxts/matter/lib/hooks/useDeltaTime")>(useDeltaTimeModule, () => {
return useDeltaTimeMock;
})
})
const useDeltaTimeMock = jest.fn()
beforeAll(() => {
const useDeltaTimeModule = getModuleByTree(...$getModuleTree("@rbxts/matter/lib/hooks/useDeltaTime"))
jest.mock<typeof import("@rbxts/matter/lib/hooks/useDeltaTime")>(useDeltaTimeModule, () => {
return useDeltaTimeMock;
})
})
make sure the mock is setup before importing anything else that might import matter aswell
iSentinel
iSentinel•2mo ago
it was just
export function getServices(): ModuleScript {
return ReplicatedStorage.rbxts_include
.FindFirstChild("node_modules")
?.FindFirstChild("@rbxts")
?.FindFirstChild("services") as ModuleScript;
}
export function getServices(): ModuleScript {
return ReplicatedStorage.rbxts_include
.FindFirstChild("node_modules")
?.FindFirstChild("@rbxts")
?.FindFirstChild("services") as ModuleScript;
}
duck
duckOP•2mo ago
const func = jest.fn();

const useDeltaTimeMock = jest.mock<
typeof import("@rbxts/planck-matter-hooks/out/hooks/useDeltaTime")
>(
getModuleByTree(...$getModuleTree("@rbxts/planck-matter-hooks/out/hooks/useDeltaTime")),
() => ({ useDeltaTime: func() }),
);

func.mockReturnValue(0);

print(useDeltaTime());
const func = jest.fn();

const useDeltaTimeMock = jest.mock<
typeof import("@rbxts/planck-matter-hooks/out/hooks/useDeltaTime")
>(
getModuleByTree(...$getModuleTree("@rbxts/planck-matter-hooks/out/hooks/useDeltaTime")),
() => ({ useDeltaTime: func() }),
);

func.mockReturnValue(0);

print(useDeltaTime());
I tried doing something like this. But it didn't work do I have to wrap this in another module and export the mocked version of useDeltaTime
lisachandra
lisachandra•2mo ago
it should work if u do this part correctly whats your code exactly?
duck
duckOP•2mo ago
lisachandra
lisachandra•2mo ago
move these imports below the usedt mock
No description
lisachandra
lisachandra•2mo ago
except jest globals
duck
duckOP•2mo ago
eslint will get mad at me 😅
iSentinel
iSentinel•2mo ago
Might need to turn it off for spec files or do something else, the import order matters here, which is why I did the dynamic import
duck
duckOP•2mo ago
ngl this feels very hacky I might not do it

Did you find this page helpful?