Skip to content

Commit

Permalink
build(readme.md): add quick start example
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Jan 6, 2021
1 parent d7fbf93 commit 4e5e12c
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ import { testApiHandler } from 'next-test-api-route-handler'
const { testApiHandler } = require('next-test-api-route-handler');
```

Quick start:

```TypeScript
// File: test/unit.test.js
import * as endpoint from '../pages/api/your-endpoint'
import { testApiHandler } from 'next-test-api-route-handler'

import type { WithConfig } from '@ergodark/next-types'

// Import the handler under test from the pages/api directory and respect the
// Next.js config object if it's exported
const endpoint: WithConfig<typeof Endpoint.default> = Endpoint.default;
endpoint.config = Endpoint.config;

await testApiHandler({
requestPatcher: req => req.headers = { key: SPECIAL_API_KEY },
handler: endpoint,
test: async ({ fetch }) => {
expect(await fetch({ method: 'GET', body: 'data' })).toStrictEqual({ json: 'response' });
}
});
```

The interface for `testApiHandler` looks like this:

```TypeScript
Expand Down Expand Up @@ -130,7 +153,7 @@ be run. This function receives one parameter: `fetch`, which is a simple
in [`fetch(...)`][8], is omitted**). Use this to send HTTP requests to the
handler under test.

## Examples
## Real-World Examples

### Testing an Unreliable API Handler @ `pages/api/unreliable`

Expand Down

0 comments on commit 4e5e12c

Please sign in to comment.