R
roblox-ts•3mo ago
duck

importing instance for jest mock

I wanted to do something like
jest.mock<typeof import("common/client/constants/local-player")>(
import("common/client/constants/local-player").await(),
() => {
const mockPlayer = {
Name: "TestPlayer",
UserId: 123456,
} as Player;

return {
LocalPlayer: mockPlayer,
};
},
);
jest.mock<typeof import("common/client/constants/local-player")>(
import("common/client/constants/local-player").await(),
() => {
const mockPlayer = {
Name: "TestPlayer",
UserId: 123456,
} as Player;

return {
LocalPlayer: mockPlayer,
};
},
);
to mock my modules to return the correct value. The import syntax returns a promise and a unknown type which isn't ideal. Is there a better way other than directly indexing from the data model to mock the module script?
Solution:
Gist
jest roblox-ts testing utils
jest roblox-ts testing utils. GitHub Gist: instantly share code, notes, and snippets.
Jump to solution
17 Replies
duck
duckOP•3mo ago
bump
lisachandra
lisachandra•3mo ago
i don't think there's any better way to do it ig you could mock the services itself
const servicesModule = getModuleByTree(...$getModuleTree("@rbxts/services"));
jest.mock<typeof import("@rbxts/services")>(servicesModule, () => {
const originalServices: typeof import("@rbxts/services") =
jest.requireActual(servicesModule);

mockServices ??= mockOnRuntime(jest, createMockInstance(originalServices));
return mockServices;
});
const servicesModule = getModuleByTree(...$getModuleTree("@rbxts/services"));
jest.mock<typeof import("@rbxts/services")>(servicesModule, () => {
const originalServices: typeof import("@rbxts/services") =
jest.requireActual(servicesModule);

mockServices ??= mockOnRuntime(jest, createMockInstance(originalServices));
return mockServices;
});
duck
duckOP•3mo ago
I tried using $getModuleTree
lisachandra
lisachandra•3mo ago
yea thats how u do it
duck
duckOP•3mo ago
does that work for file specific modules like in my case, LocalPlayer
lisachandra
lisachandra•3mo ago
yea those functions are from my testing utils
duck
duckOP•3mo ago
can you send me it šŸ™
lisachandra
lisachandra•3mo ago
sure lol wait
duck
duckOP•3mo ago
thx
Solution
lisachandra
lisachandra•3mo ago
Gist
jest roblox-ts testing utils
jest roblox-ts testing utils. GitHub Gist: instantly share code, notes, and snippets.
lisachandra
lisachandra•3mo ago
haven't tested the usage stuff yet but something like that should work oh yeah i also mock the ts runtime just incase of any weird errors although im not really sure if it does anything or not
const tsRuntimeModule = getModuleByTree(...$getModuleTree("include/RuntimeLib"));
const tsRuntime = require(tsRuntimeModule) as typeof import("include/RuntimeLib");
jest.mock<typeof import("include/RuntimeLib")>(tsRuntimeModule, () => tsRuntime);
const tsRuntimeModule = getModuleByTree(...$getModuleTree("include/RuntimeLib"));
const tsRuntime = require(tsRuntimeModule) as typeof import("include/RuntimeLib");
jest.mock<typeof import("include/RuntimeLib")>(tsRuntimeModule, () => tsRuntime);
duck
duckOP•3mo ago
hmmm
lisachandra
lisachandra•3mo ago
could be useful if u wanna mock the internals like generators n stuff
duck
duckOP•3mo ago
thanks jest is so good
lisachandra
lisachandra•3mo ago
it really is
duck
duckOP•3mo ago
mocking alr i'll mark this as solved
lisachandra
lisachandra•3mo ago
actually you might need the runtime files aswell lmk when you need it cus i gtg rn

Did you find this page helpful?