Skip to main content

File sources

withMapping(), withFiles() and withExtensionJar() accept any WiremockFileSource:

SourceResolved against
"./path/file.json" or { file: "./path/file.json" }The working directory
new URL("./file.json", import.meta.url)The URL
{ module: import.meta.url, file: "./file.json" }The module's directory
{ package: "@acme/stubs", file: "mappings/x.json" }The installed package

Prefer { module } or a file: URL: they work from any working directory – IDE, monorepo root or CI. A missing file throws at registration.

Use a path or file URL​

A WiremockPath – relative to the working directory:

examples/stubs/sources.ts
/** A path, relative to the working directory – where the test runner was started. */
export const HEALTH_STUB: WiremockPath = "./stubs/fixtures/health.json";

Source: examples/stubs/sources.ts (lines 10–11)

Relative to the declaring module:

examples/stubs/sources.ts
/** A `file:` URL – resolved against this module, so it works from any working directory. */
export const STATEMENT_STUBS: WiremockPath = new URL("./fixtures/statements.json", import.meta.url);

Source: examples/stubs/sources.ts (lines 15–16)

Use a file next to a module​

module is import.meta.url (ESM), __dirname (CommonJS), or any file or directory:

examples/stubs/sources.ts
/** A file next to a module: `import.meta.url` in ES modules, `__dirname` in CommonJS. */
export const ACCOUNT_STUB: WiremockFileSource = { module: import.meta.url, file: "./fixtures/accounts.json" };

Source: examples/stubs/sources.ts (lines 20–21)

Use a file from an npm package​

Share stubs between repositories as a package. It is found like require() would, from the working directory; file must stay inside it.

examples/stubs/sources.ts
/** A file of an installed npm package – stubs shared between repositories. */
export const EXCHANGE_RATE_STUB: WiremockFileSource = {
package: "@examples/payments-stubs",
file: "mappings/exchange-rates.json",
};

Source: examples/stubs/sources.ts (lines 25–29)

examples/packages/payments-stubs/package.json
{
"name": "@examples/payments-stubs",
"version": "1.0.0",
"private": true,
"description": "WireMock stubs shared as an npm package – registered with { package, file } sources.",
"license": "MIT",
"files": ["mappings", "__files"]
}

Source: examples/packages/payments-stubs/package.json

Name response bodies​

Object sources take a name – the path below __files/ (a WiremockStaticFile). For JARs – a WiremockExtensionJar – name renames the JAR and must end in .jar.

examples/stubs/sources.ts
/** `{ file }`; `name` sets the path below `__files/` – the base name otherwise. */
export const INVOICE_BODY: WiremockStaticFile = { file: "./stubs/fixtures/invoice.json", name: "invoices/inv-001.json" };
/** Any source works for bodies, too: this one lands at `__files/rates.json`. */
export const EXCHANGE_RATES_BODY: WiremockStaticFile = { package: "@examples/payments-stubs", file: "__files/rates.json" };
/** A plain path lands at `__files/statement.csv`. */
export const STATEMENT_BODY: WiremockStaticFile = "./stubs/fixtures/statement.csv";

Source: examples/stubs/sources.ts (lines 33–38)