> ## Documentation Index
> Fetch the complete documentation index at: https://e2e.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Services

> Start dependencies like an email API before your app runs and use them in tests.

A service is a dependency your app needs, like an email API, an OAuth
provider, or a database. When you run `e2e run` or `e2e explore`, each
service starts before the app commands and stops at the end of the run.
Services also start in
[`e2e mcp` and `openHost`](#services-in-e2e-mcp-and-openhost).

## Define a service

Call `defineService` with an object whose `start` property is an async
generator. The generator starts the dependency before `yield`, and the config
and tests receive the yielded object as the service's value. When the run
ends, everything after `yield` stops the dependency:

```ts title="services/email.ts" expandable

type Email = {
  to: string;
  subject: string;
};

export const email = defineService({
  async *start({ signal }) {
    const emails: Email[] = [];

    const server = createServer(async (request, response) => {
      emails.push(JSON.parse(await text(request)));
      response.end();
    });

    server.listen({ port: 0, host: '127.0.0.1', signal });

    await once(server, 'listening', { signal });

    const serverAddress = server.address();

    if (serverAddress === null || typeof serverAddress === 'string') {
      throw new Error('The email server has no TCP address.');
    }

    const received = (address: string) => emails.filter((message) => message.to === address);

    yield {
      url: `http://127.0.0.1:${serverAddress.port}`,
      createInbox: () => ({ address: `${randomUUID()}@example.com` }),
      received,
      waitForEmail: async (address: string, { subject }: { subject: RegExp }) => {
        for (let attempt = 0; attempt < 100; attempt++) {
          const match = received(address).find((message) => subject.test(message.subject));

          if (match !== undefined) {
            return match;
          }

          await setTimeout(100);
        }

        throw new Error(`no email to ${address} with a subject matching ${subject} arrived within 10 seconds`);
      },
    };

    server.close();

    await once(server, 'close');
  },
});
```

Pass `signal` to anything you start and stop in the service. The signal
aborts in these cases:

- Startup is cancelled (for example, with Ctrl+C), or the service doesn't
  yield within `startupTimeout` (60 seconds by default).
- When the services are no longer needed, the code after `yield` doesn't
  finish within `shutdownTimeout` (10 seconds by default).
- Ctrl+C is pressed a second time in `e2e run` or `e2e explore`, either during
  the tests or while services are stopping.

A service might need longer to start or stop (like one that pulls a Docker
image). Set `startupTimeout` or `shutdownTimeout` (the same names as on
[`app.command`](https://e2e.com/docs/reference/config#command)) in milliseconds on the object you
pass to `defineService`:

```ts
export const database = defineService({
  startupTimeout: 180_000, // default: 60000
  async *start({ signal }) {
    // ...
  },
});
```

## Pass the service to the app

Add the service under `services` in your config, and pass its address to the
app from an `env` function:

```ts title="e2e.config.ts"

export default defineConfig({
  projectId: 'dev.example.signup',
  services: { email },
  targets: [
    {
      name: 'web',
      engine: web(),
      app: {
        url: 'http://127.0.0.1:0',
        command: {
          executable: 'node',
          args: ['app/server.mjs'],
          env: async ({ services }) => ({
            PORT: '{port}',
            EMAIL_API_URL: services.email.url,
          }),
        },
      },
    },
  ],
});
```

Services start in key order before any app commands, and stop in reverse order
after the app commands finish.

The `env` function is called one time per run, after services start and before
any app commands start. For the other `env` rules, see
[`command`](https://e2e.com/docs/reference/config#command) in the config reference.

Use [`defineConfig`](https://e2e.com/docs/reference/config#defineconfig) instead of
`satisfies E2EConfig` here so `services` in `env` is typed from your config.

## Use the service in tests

Pass your config's type to `test.extend` to type the `services` fixture:

```ts title="tests/fixtures.ts"

export const test = base.extend<{}, {}, typeof config>({});
```

You can use `services` in tests, hooks, and fixtures of either scope:

```ts title="tests/signup.e2e.ts"

test('signing up sends one welcome email', async ({ app, screen, services }) => {
  const inbox = await services.email.createInbox();

  await app.open('/');
  await screen.getByRole('textbox', { name: 'Email' }).fill(inbox.address);
  await screen.getByRole('button', { name: 'Sign up' }).click();

  const email = await services.email.waitForEmail(inbox.address, { subject: /welcome/i });

  expect(email.subject).toContain('Welcome');
  expect(await services.email.received(inbox.address)).toHaveLength(1);
});
```

Each worker gets a copy of the yielded data. Any function you yield becomes
an async function in tests, so `services.email.createInbox()` returns a
promise.
When a test calls it, the process that started the services runs the real
function and sends back a copy of the result. That's the runner process in
`e2e run`, the server in `e2e mcp`, and the host's process in `openHost`.

In `e2e run` and `e2e explore`, all workers share the same service instance,
so one test can see emails sent by other tests. With `openHost`, each host
starts its own instance. If you reset a shared service between tests (for
example, clearing every email in `afterEach`), you also clear data for tests
running in other workers at the same time. Instead, have the service give
each test a unique ID and keep each test's data apart by that ID. In the test
above, `createInbox()` returns an inbox with a unique address, and the test
reads only the emails sent to it.

## What a service can yield

Yield a plain object of any shape. The data inside is copied with
[`v8.serialize`](https://nodejs.org/api/v8.html#v8serializevalue). Here's what
works inside it:

- Plain objects, arrays, maps, and sets can nest at any depth.
- Strings, numbers, booleans, bigints, `null`, `undefined`, dates, regular
  expressions, built-in errors (like `TypeError`), `ArrayBuffer`, and typed
  arrays (like `Buffer`) keep their type.
- Functions can appear at any depth in plain objects and arrays, but not
  inside a map or a set.

Any other value is rejected. If you have a custom class instance, pull the
data and functions tests need into a plain object.

With `defineService`, most rejected values are type errors (such as a `URL`, a
`Promise`, a function that takes a callback, or a function inside a `Map` or a
`Set`). A class instance with only data fields or an instance of your own
`Error` subclass still passes the type check (TypeScript compares shapes, not
classes), but the run fails with `INVALID_SERVICE_VALUE` when it starts.

Yielded functions run with `this` set to `undefined`. If a method depends on
`this`, wrap it in an arrow function (like `sent: () => inbox.sent()`). When
you call a service function in a test, the arguments and return value can be
any of the data types above, but they can't contain functions.

## When a service fails

| Code | When |
| --- | --- |
| [`SERVICE_START_FAILED`](https://e2e.com/docs/reference/errors#runner-codes) | `start` throws, finishes without yielding, isn't an async generator, or doesn't yield within its `startupTimeout`. Any services that already started are stopped, and the run fails. In `e2e mcp`, `open_session` fails instead. `openHost()` rejects with this error. |
| [`INVALID_SERVICE_VALUE`](https://e2e.com/docs/reference/errors#runner-codes) | The yielded value isn't a plain object, or it contains a value that can't be copied. |
| [`SERVICE_STOP_FAILED`](https://e2e.com/docs/reference/errors#runner-codes) | The code after `yield` throws, yields a second time, or doesn't finish within the service's `shutdownTimeout`. The run fails with a cleanup error. In `e2e mcp`, the last session's close summary includes it as a cleanup line. `openHost` returns it from `host.close()`. |

## Services in e2e mcp and openHost

`e2e mcp` starts a config file's services when the first session on it opens
and stops them when the last one closes. All sessions on that file share them.
If you edit the config while sessions on it are open, the running services
don't change. Your changes to the services apply after the last session on
that file closes and a new one opens.

[`openHost`](https://e2e.com/docs/reference/runner#openhost) starts the services when the host
opens. `host.close()` stops them after the app processes stop.

## Run a local API emulator

A service can run any package that emulates a third-party API. One option is
[emulate](https://emulate.dev), which runs local emulators for APIs like
GitHub, Resend, and Stripe. The example below uses its Resend emulator, so
install emulate as a dev dependency:

<CodeGroup>
```bash npm
npm install --save-dev emulate
```

```bash pnpm
pnpm add -D emulate
```

```bash bun
bun add -d emulate
```
</CodeGroup>

Start an emulator with `createEmulator`, and don't yield the whole emulator
object. If you do, any test can call its `close()` and stop the emulator for
every worker. Instead, yield only what tests need (like its `url`):

```ts title="services/resend.ts"

export const resend = defineService({
  async *start() {
    const emulator = await createEmulator({ service: 'resend', port: 0 });

    yield { url: emulator.url };

    await emulator.close();
  },
});
```

<Accordion title="Config and test for the Resend emulator">

```ts title="e2e.config.ts"

export default defineConfig({
  projectId: 'dev.example.signup',
  services: { resend },
  targets: [
    {
      name: 'web',
      engine: web(),
      app: {
        url: 'http://127.0.0.1:0',
        command: {
          executable: 'node',
          args: ['app/server.mjs'],
          env: ({ services }) => ({
            PORT: '{port}',
            RESEND_API_KEY: 're_test',
            RESEND_BASE_URL: services.resend.url,
          }),
        },
      },
    },
  ],
});
```

```ts title="tests/fixtures.ts"

export const test = base.extend<{}, {}, typeof config>({});
```

The test reads the sent emails from the emulator's Resend API:

```ts title="tests/signup.e2e.ts"

test('signing up sends a welcome email through Resend', async ({ app, screen, services }) => {
  const address = `ada+${randomUUID()}@example.com`;

  await app.open('/');
  await screen.getByRole('textbox', { name: 'Email' }).fill(address);
  await screen.getByRole('button', { name: 'Sign up' }).click();
  await expect(screen.getByRole('heading', { name: 'Check your inbox' })).toBeVisible();

  const response = await fetch(`${services.resend.url}/emails`);
  const { data } = await response.json();

  expect(data.filter((email: { to: string[] }) => email.to.includes(address))).toEqual([
    expect.objectContaining({ to: [address], subject: 'Welcome' }),
  ]);
});
```

</Accordion>

## Next

<CardGroup cols={2}>
  <Card title="Writing fixtures" icon="puzzle-piece" href="/fixtures">
    Set up test data before each test and clean it up after.
  </Card>
  <Card title="Config reference" icon="code" href="/reference/config#services">
    The `services` key and the `env` function.
  </Card>
</CardGroup>
