Skip to content

Commit

Permalink
Tweaks to unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mears-2 committed Aug 6, 2024
1 parent 43d47b0 commit d45d437
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/unit/mocks/mockoon.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"responses": [
{
"uuid": "eb55d0af-aa59-4d0f-8cea-58ba9c68fa55",
"body": "{\"status\":\"success\",\"errors\":null,\"data\":{\"daedalus\":\"externally mocked daedalus model version\",\"daedalus.api\":\"externally mocked R API version\"}}",
"body": "{\"status\":\"success\",\"errors\":null,\"data\":{\"daedalus\":\"1.2.3.4.5.6.7.8\",\"daedalus.api\":\"8.7.6.5.4.3.2.1\"}}",
"latency": 0,
"statusCode": 200,
"label": "",
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/server/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ The @nuxt/test-utils/e2e module just mentioned, which has Vitest run the test se

## Mockoon

To run that separate service, [MSW](https://mswjs.io/) would be a common choice, but it doesn't seem to be compatible with Nuxt (at least server-side): [here](https://github.com/nuxt/test-utils/issues/775) is an example of a GitHub issue for this. So instead we are using [Mockoon](https://mockoon.com/), whose canned responses are stored in /tests/unit/mocks/mockoon.json. To configure the responses, you can either directly edit that file, or use the convenient UI of the Mockoon desktop app.
To run that separate service, [MSW](https://mswjs.io/) would be a common choice, but it doesn't seem to be compatible with Nuxt (at least server-side): [here](https://github.com/nuxt/test-utils/issues/775) is an example of a GitHub issue for this. So instead we are using [Mockoon](https://mockoon.com/), whose canned responses are stored in /tests/unit/mocks/mockoon.json. To configure the responses, you can either directly edit that file (before re-starting the mock server), or use the convenient UI of the Mockoon desktop app.

To save time writing mock responses, Mockoon is able to record responses from another service, if you follow the steps in [this GitHub comment](https://github.com/mockoon/mockoon/issues/21#issuecomment-1900049410). These recorded responses can then be played back in tests, and edited.

This mocking service has to be up and running for unit tests to pass, and it does not interfere with uses of `registerEndpoint` (which, anyway, works by registering mock endpoints from within the web app, so can't interfere with things served on external ports). By contrast, when we run our end-to-end suite (with Playwright), we want to test that the whole collection of services works properly in a realistic simulation, and for that we must use the real R API service. Thus, we include a check in the end-to-end tests to verify that the mock R API server is not running. We do this by sending a request to a smoke endpoint `/mock-smoke` that should never be implemented by the genuine R API. The same smoke endpoint should be used by tests that do rely on the mocking service, to verify that they are being run against the mocked API.

We should be able to switch away from Mockoon to another solution if desired, by [exporting](https://mockoon.com/docs/latest/openapi/import-export-openapi-format/) the canned responses to OpenAPI format.

### To run the mock server

#### From CLI:

npx mockoon-cli start --data ./tests/unit/mocks/mockoon.json

#### From desktop app:

Make sure the correct API config is selected in the left sidebar (not the demo config). Then click the green "play" button.
9 changes: 4 additions & 5 deletions tests/unit/server/api/versions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const nodeFetch = fetch; // Normal 'fetch' from Node

beforeAll(async () => {
// Verify that the user of the test suite has started the mock server
// by checking that the server is listening on localhost:8001
// by checking that the server is listening on localhost:8001/mock-smoke

let response;
try {
Expand All @@ -28,15 +28,14 @@ beforeAll(async () => {
});

describe("api/versions", async () => {
// Run the setup function to start the Nuxt server
await setup();
await setup(); // Start the Nuxt server

it("returns the expected version data", async () => {
const response = await nuxtTestUtilsFetch("/api/versions");
const json = await response.json();

expect(json.daedalusModel).toBe("externally mocked daedalus model version");
expect(json.daedalusApi).toBe("externally mocked R API version");
expect(json.daedalusModel).toBe("1.2.3.4.5.6.7.8");
expect(json.daedalusApi).toBe("8.7.6.5.4.3.2.1");
expect(json.daedalusWebApp).toMatch(/(\d+\.)?(\d+\.)?(\*|\d+)/);
});
});

0 comments on commit d45d437

Please sign in to comment.