> ## 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.

# Emulate third-party APIs with emulate

> Run a local emulator for APIs like GitHub, Stripe, or Resend as an e2e service, and point your app at it instead of the real API.

[emulate](https://emulate.dev) runs local emulators for third-party APIs like
GitHub, Stripe, Google, Slack, and Resend. Each emulator keeps state like the
real API and needs no API keys or network access. Start one as a
[service](https://e2e.com/docs/services) before your app starts, then point your app at it instead
of the real API.

## Installation

To install emulate, run:

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

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

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

## Start the emulator

Start an emulator with `createEmulator` in a service, 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();
  },
});
```

## Configure e2e

Add the service under `services` in your config and pass the emulator's URL
to your app through an `env` function. Because the app sends emails to the
emulator, any API key works:

```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,
          }),
        },
      },
    },
  ],
});
```

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>({});
```

## Write the test

Sign up with a unique address, then read 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' }),
  ]);
});
```

## Next

<CardGroup cols={2}>
  <Card title="Services" icon="server" href="/services">
    Start your app's dependencies before it runs, and use them in tests.
  </Card>
  <Card title="Config reference" icon="code" href="/reference/config#services">
    The `services` key and the `env` function.
  </Card>
</CardGroup>
