|
| 1 | +# vite-plugin e2e tests |
| 2 | + |
| 3 | +This directory contains e2e test that give more confidence that the plugin will work in real world scenarios outside the comfort of this monorepo. |
| 4 | + |
| 5 | +In general, these tests create test projects by copying a fixture from the `fixtures` directory into a temporary directory and then installing the local builds of the plugin along with its dependencies. |
| 6 | + |
| 7 | +## Running the tests |
| 8 | + |
| 9 | +Simply use turbo to run the tests from the root of the monorepo. |
| 10 | +This will also ensure that the required dependencies have all been built before running the tests. |
| 11 | + |
| 12 | +```sh |
| 13 | +pnpm test:e2e -F @cloudflare/vite-plugin |
| 14 | +``` |
| 15 | + |
| 16 | +## Developing e2e tests |
| 17 | + |
| 18 | +These tests use a mock npm registry where the built plugin has been published. |
| 19 | + |
| 20 | +The registry is booted up and loaded with the local build of the plugin and its local dependencies in the global-setup.ts file that runs once at the start of the e2e test run, and the server is killed and its caches removed at the end of the test run. |
| 21 | + |
| 22 | +The Vite `test` function is an extended with additional helpers to setup clean copies of fixtures outside of the monorepo so that they can be isolated from any other dependencies in the project. |
| 23 | + |
| 24 | +The simplest test looks like: |
| 25 | + |
| 26 | +```ts |
| 27 | +test("can serve a Worker request", async ({ expect, seed, viteDev }) => { |
| 28 | + const projectPath = await seed("basic"); |
| 29 | + runCommand(`pnpm install`, projectPath); |
| 30 | + |
| 31 | + const proc = await viteDev(projectPath); |
| 32 | + const url = await waitForReady(proc); |
| 33 | + expect(await fetchJson(url + "/api/")).toEqual({ name: "Cloudflare" }); |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +- The `seed()` helper makes a copy of the named fixture into a temporary directory. It returns the path to the directory containing the copy (`projectPath` above). This directory will be deleted at the end of the test. |
| 38 | +- The `runCommand()` helper simply executes a one-shot command and resolves when it has exited. You can use this to install the dependencies of the fixture from the mock npm registry, as in the example above. |
| 39 | +- The `viteDev()` helper boots up the `vite dev` command and returns an object that can be used to monitor its output. The process will be killed at the end of the test. |
| 40 | +- The `waitForReady()` helper will resolve when the `vite dev` process has output its ready message, from which it will parse the url that can be fetched in the test. |
| 41 | +- The `fetchJson()` helper makes an Undici fetch to the url parsing the response into JSON. It will retry every 250ms for up to 10 secs to minimize flakes. |
0 commit comments