This example uses Zimic with Jest in a server-side environment. Node.js is used in this example, but other runtimes should be similar, such as Deno and Bun.
The application is a simple Fastify server, fetching repositories from the GitHub API. Any other Node.js framework could be used as well, such as express and Nest.js.
- Server:
src/app.ts
An example test suite uses Jest to test the application. Zimic is used to mock the GitHub API and simulate a test case where the repository is found and another where it is not.
- GitHub interceptor:
tests/interceptors/github.ts
- Test suite:
tests/example.test.ts
- Test setup file:
tests/setup.ts
- Jest configuration:
jest.config.js
Important
The flag --experimental-vm-modules
, present in the command test
in the package.json
, is
required by Jest because Zimic uses dynamic imports internally.
-
Clone this example:
mkdir zimic cd zimic git init git remote add origin git@github.com:zimicjs/zimic.git git sparse-checkout init git sparse-checkout set examples/with-jest-node git pull origin canary # or a specific branch or tag cd examples/with-jest-node
If you'd like to clone the example of a previous version, replace
canary
with the desired branch or tag, such as@zimic/interceptor@0
and@zimic/fetch@0.1.0
. -
Install the dependencies:
pnpm install
-
Run the tests:
pnpm run test