I am currently working on a function that calls the url
module.
index.svelte
import {url} from '@roxi/routify';
someFunction(() => {
let x = $url('/books') // this line needs to be mocked
console.log('x: ' + x);
});
Is there a way I can mock this specific line of code?
$url("/books")
$ prefix
Additional Details:
url()
is an object containing the named function subscribe(listener)
subscribe(listener)
invokes the function derived(...)
, which in turn returns a Readable
interface
The Readable
interface has its own subscribe(...)
function
Attempts made so far:
#1 Tried using mockReturnValue
for subscribe
within url
import {readable} from "svelte/store";
import {mocked} from "jest-mock";
import {render} from "@testing-library/svelte";
import * as routify from "@roxi/routify";
import books_index from "./index.svelte";
jest.mock("@roxi/routify", jest.fn(() => {
return {
__esModule: true,
url: {
subscribe: jest.fn().mockReturnValue(readable())
}
}
}));
const mockedRoutify = mocked(routify);
it("needs to work", () => {
const results = render(books_index);
expect(results);
});
This resulted in:
ReferenceError: Cannot access 'store_1' before initialization
#2 Attempted another approach with mockReturnValue
for subscribe
within url
jest.mock("@roxi/routify", jest.fn(() => {
return {
__esModule: true,
url: {
subscribe: jest.fn().mockReturnValue(() => readable())
}
}
}));
This led to:
TypeError: $url is not a function
Other notes to consider:
Attempted a different strategy within the test, resulting in
routify_1.url.mockReturnValue is not a function
Tried both with and without
__esModules: true
.Played around with the placement of imports and mock declarations.
Another test involving a custom service is functioning correctly, indicating that my
jest.config.ts
andtsconfig.json
are set up properly.
Request for Assistance:
What crucial element am I overlooking in this process?