# Test SMS, email, OAuth and other external APIs with e2e services

> Services let you run mock servers, start databases etc. before the tests run, so you can avoid external dependencies for more reliable tests.

By Satyajit Sahoo. Published 2026-12-04. Canonical URL: https://e2e.com/blog/test-external-apis-with-e2e-services

External dependencies in apps can be hard to test in end-to-end tests. Often, SMS, email, OAuth, and payment APIs are either mocked inside the app or hit the real endpoints. But mocking them inside the app adds complexity to the actual code, and hitting real endpoints means our tests can fail because the external service went down.

The [services API](https://e2e.com/docs/services) in [e2e](https://e2e.com/) provides a cleaner way to handle it. With services, you can:

- Start dependencies before your app starts and stop them after it stops
- Expose data and helpers from the services to tests

A service can be anything: an HTTP server, a database, or a local API emulator such as [emulate.dev](https://emulate.dev), which provides emulators for Resend, GitHub, Stripe, Google sign-in, Twilio, Slack, AI providers, and more.

## What is a service

A service is an object defined with the [`defineService`](https://e2e.com/docs/services#define-a-service) helper. Its `start` method is an [async generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*):

```ts
import { defineService } from "e2e";

export const email = defineService({
  async *start({ signal }) {
    // Start your service

    yield {
      // Expose data and helpers to tests
    };

    // Stop your service
  },
});
```

In the `start` method, you can run your service and then `yield` an object with properties that you want to expose to tests.

## How to mock an email API in e2e tests

For example, an email service using the Resend emulator from [emulate.dev](https://emulate.dev) may look like:

```ts title="services/email.ts"
import { createEmulator } from "emulate";
import { defineService } from "e2e";

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

    yield { url: emulator.url };

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

Here, we create a Resend emulator with `createEmulator`, then `yield` its URL.

### Registering the service

Then, we can register the service in the config in the [`services`](https://e2e.com/docs/reference/config#services) key:

```ts title="e2e.config.ts"
import { defineConfig } from "e2e";
import { web } from "@e2e-dev/web";
import { email } from "./services/email.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: ({ services }) => ({
            PORT: "{port}",
            RESEND_API_KEY: "re_test",
            RESEND_BASE_URL: services.email.url,
          }),
        },
      },
    },
  ],
});
```

The services are also available as a parameter in the [`env` function](https://e2e.com/docs/reference/config#command) of the app, so values from services can be passed as environment variables.

### Writing the test

Once registered, services become available to the tests as a `services` parameter. To type it, pass your config's type to [`test.extend`](https://e2e.com/docs/reference/test#test-extend):

```ts title="tests/fixtures.ts"
import { test as base } from "e2e";
import type config from "../e2e.config.ts";

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

Then use `services` in the test:

```ts title="tests/signup.e2e.ts"
import { randomUUID } from "node:crypto";
import { expect } from "e2e";
import { test } from "./fixtures.ts";

test("signing up sends a welcome email", 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.email.url}/emails`);
  const { data } = await response.json();

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

Here, we sign up with a unique email address in the test, then read the sent emails from the emulator and check that the welcome email went to that address. All workers share one emulator, so the unique address keeps each test from seeing emails sent by other tests.

[Video: e2e run starts the email service and the app, then passes the signup test](https://e2e.com/blog/videos/e2e-services-demo.mp4)

Apart from tests, services also run when using [`e2e explore`](https://e2e.com/docs/explore), [`e2e mcp`](https://e2e.com/docs/coding-agents#let-the-agent-use-the-app) and [`openHost`](https://e2e.com/docs/reference/runner#openhost).

## Mocking SMS, OAuth, payments, and databases in e2e tests

A local email server is just one example. You can use the same pattern with any dependency your backend talks to:

- SMS and phone verification
- OAuth and SSO sign-in
- Payments, SaaS APIs, and webhooks
- Deterministic AI backends
- Databases, storage, and workers

Check out our [documentation for services](https://e2e.com/docs/services) to learn more and see how to write your own services with custom implementations. Also see our [emulate guide](https://e2e.com/docs/integrations/emulate) for prebuilt examples using [emulate.dev](https://emulate.dev).

## Wrapping up

What makes `services` nice is that everything stays in one place. Your app runs its real backend code, your dependencies start and stop with the test run, and the same config works across `e2e run`, `e2e explore`, and `e2e mcp`. You don't have to keep a separate setup around for [CI](https://e2e.com/docs/ci), local runs, or [coding agents](https://e2e.com/docs/coding-agents).

Make sure to check our [docs for services](https://e2e.com/docs/services) and let us know what you'll be using it for.
