Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nuxt
node_modules
static/sw.js
22 changes: 22 additions & 0 deletions docs2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Documentation

## Development

```bash
# install dependencies
$ yarn install

# serve with hot reload at localhost:3000
$ yarn dev
```

Then you can start edit the [content](./content) directory.

# Test for production

```bash
$ yarn generate
$ serve dist/ # npm install -g serve
```

For detailed explanation on how things work, check out [Nuxt.js docs](https://nuxtjs.org).
89 changes: 89 additions & 0 deletions docs2/content/en/1.getting-started/1.installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: Installation
---

# Installation

Install `@nuxt/test-utils` and `jest` as development dependencies:

::code-group
```bash [yarn]
yarn add --dev @nuxt/test-utils jest
```

```bash [npm]
npm install --save-dev @nuxt/test-utils jest
```
::

::alert{type="info"}
At the moment [Jest](https://jestjs.io) is the only supported test runner.
::

### End-to-end

To run the end-to-end testing, it will be necessary to install the [`playwright`](https://playwright.dev/) as development dependencies:

::code-group
```bash [yarn]
yarn add --dev playwright
```

```bash [npm]
npm install --save-dev playwright
```
::

## Configuration

For a quick start simply add `@nuxt/test-utils` preset to `jest.config` file:

```js [jest.config.js]
export default {
preset: '@nuxt/test-utils'
}
```

::alert{type="warning"}
The preset configures `testEnvironment`, `transform` and `transformIgnorePatterns` options. Note that including them additionally in the `jest.config` would override [the preset values](https://github.com/nuxt/test-utils/blob/main/jest-preset.js).
::

## Use with Vue Test Utils

Might be your project already includes Jest, for instance, to test Vue components using [Vue Test Utils](https://vue-test-utils.vuejs.org). Keep in mind that `@vue/test-utils` is running tests in Jest’s browser-like, but `@nuxt/test-utils` requires node-like [test environment](https://jestjs.io/docs/configuration#testenvironment-string).

It is recommended to create a separate `jest.config` file for tests which use Nuxt Test Utils. For example, if components tests are located in `test/unit` and end-to-end tests are placed inside `test/e2e` directory, set up your `package.json` scripts like this:

```json [package.json]
"scripts": {
"test": "yarn test:unit && yarn test:e2e",
// configuration for Nuxt Test Utils
"test:e2e": "jest test/e2e -c jest.config.e2e.js",
"test:unit": "jest test/unit"
}
```

Usually end-to-end tests are slow. Running them separately will also help to keep unit tests performant.

## Advanced Configuration

Instead of using the preset you may also configure Jest manually. For example, to use your preferred [transformer](https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object):

```js [jest.config.js]
export default {
// add if your Jest version is lower than 27.0
testEnvironment: 'node',
// use any transformers you like
transform: {
'\\.ts$': 'ts-jest'
},
// add to transpile packages in ESM syntax
transformIgnorePatterns: [
'node_modules/(?!(nuxt-i18n)/)'
]
}
```

::alert{type="warning"}
Currently Jest does not transpile files found inside `node_modules`. Therefore, packages published in ESM syntax (e.g., [`nuxt-i18n`](https://github.com/nuxt-community/i18n-module) module) will cause an error. To make them reachable for the transformers use [`transformIgnorePatterns`](https://jestjs.io/docs/configuration#transformignorepatterns-arraystring) option.
::
123 changes: 123 additions & 0 deletions docs2/content/en/1.getting-started/2.setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Setup
---

# Setup

## Setting up the test context

In each describe block where you are taking advantage of the `@nuxt/test-utils` helper methods, you will need to set up the test context before beginning.

```js
import { setupTest } from '@nuxt/test-utils'

describe('My test', () => {
setupTest({
// test context options
})

test('my test', () => {
// ...
})
})
```

Behind the scenes, `setupTest` performs a number of tasks in `beforeEach`, `afterEach` and `afterAll` to setup the Nuxt test environment correctly. It also adds a single Nuxt setup task as an additional test. This means it must be run within the describe block of a test, before any other calls to `test` or `it`.

::alert{type="warning"}
Using `test.only` or `it.only` later in the describe block of your test will cause the tests to fail.
::


## Options for `setupTest`

### Nuxt configuration

#### rootDir

Path to a directory with a Nuxt app to be put under test.

* Type: `string`
* Default: `'.'`

#### configFile

Name of the configuration file.

* Type: `string`
* Default: `'nuxt.config.js' | 'nuxt.config.ts'`

#### config

Object with configuration overrides.

* Type: `NuxtConfig`
* Default: `{}`

### Build directory

#### randomBuildDir

To avoid conflicts between concurrent tests, a new random [build directory](https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-builddir) will be created each time `setupTest` is called.

* Type: `boolean`
* Default: `true` (ignored if [`build`](#build) step is not enabled)

#### randomPort

The test server will listen on a new random port each time `setupTest` is called. If disabled, the server will try to use [`server.port`](https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-server) value.

* Type: `boolean`
* Default: `true` (ignored if [`server`](#server) is not enabled)

### Setup timings

#### setupTimeout

The amount of time (in milliseconds) to allow for `setupTest` to complete its work (which could include building or generating files for a Nuxt application, depending on the options that are passed).

* Type: `number`
* Default: `60000`

#### waitFor

An additional aount of time (in milliseconds) to wait after setting up the test context before commencing the rest of the test suite.

* Type: `number`
* Default: `0`

### Features to enable

#### server

Whether to launch a server to respond to requests in the test suite.

* Type: `boolean`
* Default: `false`

#### build

Whether to run a separate build step.

* Type: `boolean`
* Default: `false` (`true` if `browser` or `server` is enabled)

#### generate

Whether to run generate pre-rendered HTML files for the application.

* Type: `boolean`
* Default: `false`

#### browser

Under the hood, Nuxt test utils uses [`playwright`](https://playwright.dev/) to carry out browser testing. If this option is set, a browser will be launched and can be controlled in the subsequent test suite. (More info can be found [here](/api-reference/browser-testing).)

* Type: `boolean`
* Default: `false`

#### browserOptions

* Type: `object` with the following properties
- **type**: The type of browser to launch - either `chromium`, `firefox` or `webkit`
- **launch**: `object` of options that will be passed to playwright when launching the browser. See [full API reference](https://playwright.dev/#version=master&path=docs%2Fapi.md&q=browsertypelaunchoptions).
112 changes: 112 additions & 0 deletions docs2/content/en/2.api/1.methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
title: Methods
---

# Methods

Nuxt test utils exposes a number of useful methods you can use when testing a Nuxt application.

## get(path)

You can get a response to a server-rendered page.

* `path`
* Type: `String`
* `required`

```js
import { setupTest, get } from '@nuxt/test-utils'

describe('ssr', () => {
setupTest({
server: true
})

test('should response to a server-rendered page', () => {
const html = await get('/')

expect(html).toContain('Something')
})
})
```

::alert{type="info"}
Remember to enabled [`server`](/getting-started/setup#server) feature.
::

## `url(path)`

This helper simply returns the full URL for a given page (including the port the test server is running on.)

* `path`
* Type: `String`
* `required`

```js
import { setupTest, url } from '@nuxt/test-utils'

describe('ssr', () => {
setupTest({
server: true
})

test('should generate url the index page', () => {
expect(url('/')).toBe('http://localhost:3000/')
})
})
```

::alert{type="info"}
Remember to enabled [`server`](/getting-started/setup#server) feature.
::

## `createPage(path, options)`

You can initiate a browser session for a given page with createPage.

You can find out more about the properties and methods on the page object in the [`playwright documentation'](https://playwright.dev/docs/pom).

* `path`
* Type: `String`
* `optional`

* `options`
* Type: `playwright.BrowserContextOptions`
* `optional`

```js
import { setupTest, createPage } from '@nuxt/test-utils'

describe('browser', () => {
setupTest({
browser: true
})

test('should render index page', async () => {
const page = await createPage('/')
const body = await page.innerHTML('body')

expect(body).toContain('Something')
})
})
```

::alert{type="info"}
Remember to enabled [`browser`](/getting-started/setup#browser) feature.
::

## `getNuxt`

Returns Nuxt instance of context.

```js
import { setupTest, getNuxt } from '@nuxt/test-utils'

describe('nuxt instance', () => {
setupTest()

test('should nuxt instance defined', () => {
expect(getNuxt()).toBeDefined()
})
})
```
Loading