Generate mocks in the Mocks view opens a wizard: pick the sources to mock — a source a mock already serves says which — set each mock's key, its port — assigned by Studio or fixed — its records per collection, whether it starts with Studio, which endpoints it answers — every one, or those ticked, the rest going to the source's own server — and whether its data is generated or recorded, then review. Studio shows the exact change to courier.mocks.ts as a diff, with what the diff does not show — that it creates the file, which running mocks it restarts, that a port is taken or assigned — and writes it on one click. It creates courier.mocks.ts when there is none (courier.mocks.mts beside a courier.config.mts), formatted as the project formats its code — with its Biome, ESLint and Prettier, as the files of an import are — and changes an existing file only by inserting text, in the file's own style: the new entries in the object defineMocks takes, and the imports they need. It imports the config by its default export — or, for a config of named exports alone, as a namespace (import * as courierConfig) — and imports a courier.config.ts the way the project resolves modules: as ./courier.config.js when the tsconfig, or a project it lists under references, resolves them as Node does (node16 or nodenext) in a "type": "module" package, or when the config's or the mocks file's own relative imports end in .js or .mjs, and as ./courier.config otherwise. A key another mock has, in any letter case, a port that is Studio's own or that another mock declares, or a source the config does not declare is marked on its field instead. A port Studio assigned to another mock may be taken: the review warns that the other mock moves to another port. Generate mocks is turned off, and says why, while Studio's first load of courier.config runs, while the config does not load, and while it is missing. During a later reload it stays on: the wizard waits for the reload before it plans the change, and answers STUDIO_BUSY when the reload outlasts that wait.
Studio then loads the edited files and runs them only if they are the edit it meant: the mocks file loads, each new mock is a defineMockServer() definition serving the config's source, no mock declared before is gone, and the config's sources are unchanged. Otherwise it puts the file back, or removes the file it created, keeps running what it ran, and shows why, with the lines to add by hand once the cause is fixed. If the file was saved in the meantime, Studio keeps that save, and lists the lines it added for you to remove; so it does when it cannot put the file back. A save between the review and the click is caught too: Studio shows the change again, planned against the file as it stands.
When Studio will not edit the file itself, it shows the lines to add by hand — or the whole file to create — with the reason. For the first five reasons below, the wizard plans the change again by itself when the project changes, or on Check again.
| Code | Studio says |
|---|---|
STUDIO_STARTING | Studio is still loading courier.config; try again in a moment. |
STUDIO_BUSY | Studio is still loading courier.config; try again in a moment. On the click, once it has waited 5 s longer than one load may take: Studio is still loading courier.config after 25 s, so it did not change anything. Try again once it has loaded. |
CONFIG_LOAD_FAILING | courier.config does not load right now (error). Fix that first; Studio adds mocks only beside a config it can load. |
MOCKS_LOAD_FAILING | courier.mocks.ts does not load right now (error). Fix that first. |
MOCKS_CHANGED_SINCE_LOAD | courier.mocks.ts changed since Studio loaded it. Studio plans the edit once it has reloaded. |
NO_CONFIG | There is no courier.config in the project. Declare your sources there first. |
CONFIG_SHADOWED | courier.config.js sits beside courier.config.ts, so the import of "./courier.config" in courier.mocks.ts would load courier.config.js, not courier.config.ts, which Studio loads. Delete or move courier.config.js — most likely compiled output — then ask again. |
MOCKS_JAVASCRIPT | courier.mocks.js is JavaScript. Studio writes ES imports named as TypeScript resolves them, which a CommonJS file cannot take and Node does not always resolve, so it edits TypeScript mocks files only. |
MOCKS_IS_LINK | courier.mocks.ts is a symbolic link. Studio saves an edit by replacing the file, which would replace the link with a copy, so Studio edits only a regular file. |
MOCKS_NOT_REGULAR | courier.mocks.ts is a directory, so Studio cannot edit it as a mocks file. |
MOCKS_HARD_LINKED | courier.mocks.ts has 2 hard links. Studio saves an edit by replacing the file, which would part it from its other names, so it leaves the file to you. |
MOCKS_NOT_OWNED | courier.mocks.ts belongs to user 501, but Studio runs as user 502. An edited file would belong to user 502, so Studio leaves it to its owner. |
MOCKS_NOT_WRITABLE | Studio may not write courier.mocks.ts (EACCES). Studio does not edit a file that is read-only to it. |
MOCKS_TOO_LARGE | courier.mocks.ts is n bytes, more than the 1048576 bytes (1 MiB) Studio edits. |
MOCKS_GENERATED | Line n of courier.mocks.ts marks the file as generated, so an edit would be lost when it is generated again. Change what generates it instead. (@generated or DO NOT EDIT in its first five lines.) |
NOT_UTF8 | courier.mocks.ts is not valid UTF-8 text. Studio edits only text it can read in full, so it leaves this file as it is. |
PARSE_ERROR | courier.mocks.ts does not parse: problem |
MOCKS_COMMONJS | courier.mocks.ts exports with module.exports (line n); Studio edits only a mocks file that is an ES module. |
MOCKS_NOT_LITERAL | courier.mocks.ts does not export an object literal Studio can add to. Or it names what the file exports instead: the result of another call than defineMocks, more than one default export, a type, a variable not declared with const, or a getter, setter or method among the mocks. |
MOCKS_KEYS_HIDDEN | courier.mocks.ts (line n) spreads another object into the mocks, so Studio cannot see every key. Or a computed key, __proto__, or keys declared outside the object Studio would add to. |
IMPORT_NAME_TAKEN | courier.mocks.ts already declares defineMockServer on line n, so Studio cannot import defineMockServer from @noverax/courier-mock under that name. |
MOCK_PACKAGE_UNLOADABLE | Studio could not load @noverax/courier-mock in this project: reason. Install it with npm install --save-dev @noverax/courier-mock hono @hono/node-server, then ask again. (The command follows the project's lockfile.) |
WRITE_FAILED | Studio could not write courier.mocks.ts (error). |
INTERNAL_PLAN_REJECTED | Studio could not verify its edit of courier.mocks.ts (problem), so it changed nothing. |
NOT_LOCAL_PEER | Studio edits project files only for a browser on this machine. Add the mocks to your mocks file by hand, or open Studio on the machine it runs on. |