You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+87-13Lines changed: 87 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ The main goal for this package is to provide a simple and easy-to-use testing en
9
9
- 🥜 Run Nitro per test suite or globally
10
10
- ✅ Seamless integration with Vitest
11
11
- 🪝 Conditional code execution based on test mode (`import.meta.test`)
12
-
- 📡 Familiar [`$fetch`](#fetch) helper like Nuxt test utils
12
+
- 📡 Familiar [`$fetchRaw`](#fetchRaw) helper similar to Nuxt test utils
13
13
14
14
## Installation
15
15
@@ -56,12 +56,12 @@ Write your tests in a dedicated location, e.g. a `tests` directory. You can use
56
56
A simple teste case could look like this:
57
57
58
58
```ts
59
-
import { $fetch } from'nitro-test-utils/e2e'
59
+
import { $fetchRaw } from'nitro-test-utils/e2e'
60
60
import { describe, expect, it } from'vitest'
61
61
62
62
describe('api', () => {
63
63
it('responds successfully', async () => {
64
-
const { data, status } =await$fetch('/api/health')
64
+
const { data, status } =await$fetchRaw('/api/health')
65
65
66
66
expect(status).toBe(200)
67
67
expect(data).toMatchSnapshot()
@@ -84,11 +84,11 @@ export default defineConfig()
84
84
85
85
Contrary to the global setup, the Nitro server is not started automatically by Vitest. Instead, you need to call the `setup` function in each test suite to start the Nitro server. After each test suite, the Nitro server is shut down.
86
86
87
-
Use the `nitro-test-utils/e2e` module to import the `setup` function and the `$fetch` helper. The `setup` function accepts an options object with the `rootDir` property, which should point to the directory where the Nitro server is located. For more options, see the [Configuration](#configuration) section.
87
+
Use the `nitro-test-utils/e2e` module to import the `setup` function and the `$fetchRaw` helper. The `setup` function accepts an options object with the `rootDir` property, which should point to the directory where the Nitro server is located. For more options, see the [Configuration](#configuration) section.
The `$fetch` function is a simple wrapper around [`ofetch`](https://github.com/unjs/ofetch) and is used to make requests to your Nitro server during testing. Import the function from the `nitro-test-utils/e2e` module. It will dynamically use the base URL of the active test server.
222
+
To get the URL of the active test server for the current test suite or global test environment, you can use the `injectServerUrl` function.
223
223
224
-
`$fetch` returns a promise that resolves with the raw response from [`ofetch.raw`](https://github.com/unjs/ofetch?tab=readme-ov-file#-access-to-raw-response). This is useful because it allows you to access the response status code, headers, and body, even if the response failed.
import { createFetch } from 'nitro-test-utils/e2e'
260
+
261
+
const $fetch = createFetch()
262
+
```
263
+
264
+
**TypeDeclaration:**
265
+
266
+
```ts
267
+
function createFetch(): $Fetch
268
+
```
269
+
270
+
### `$fetchRaw`
271
+
272
+
The`$fetchRaw`function is a simple wrapper around the custom [`ofetch`](https://github.com/unjs/ofetch) `$Fetch` instance created by `createFetch`. It simplifies requesting data from your Nitro server during testing. Import the function from the `nitro-test-utils/e2e` module. It will dynamically use the base URL of the active test server.
273
+
274
+
`$fetchRaw`returnsapromisethatresolveswiththerawresponsefrom [`ofetch.raw`](https://github.com/unjs/ofetch?tab=readme-ov-file#-access-to-raw-response). This is useful because it allows you to access the response status code, headers, and body, even if the response failed.
225
275
226
276
**Usage:**
227
277
228
278
Insideatestcase:
229
279
230
280
```ts
231
281
// Use `data` instead of `body` for the parsed response body
>Fetchoptionswillbemergedwithsensibledefaultoptions, like [`ignoreResponseError`](https://github.com/unjs/ofetch?tab=readme-ov-file#%EF%B8%8F-handling-errors) set to `true` to prevent the function from throwing an error when the response status code is not in the range of 200-299.
303
+
> Alladditionaloptionssetin [`createFetch`](#createfetch) applyhereaswell, sucghas [`ignoreResponseError`](https://github.com/unjs/ofetch?tab=readme-ov-file#%EF%B8%8F-handling-errors) set to `true` to prevent the function from throwing an error when the response status code is not in the range of 200-299.
0 commit comments