Some useful helpers for integration testing Next.js applications.
Jest example:
/* global jasmine, describe, it, beforeAll, afterAll */
'use strict'
import {expectElement, setup, render, teardown} from 'next-test-helper'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
describe('next-test-helper', () => {
beforeAll(async () => await setup('./test/fixtures'))
afterAll(async () => await teardown())
it('should render a "hello world" page', async () => {
const element = await render('/hello')
expectElement(element).to.have.text('Hello World. About')
})
})
setup(dir): build the application pointed to by dir. Run with
SKIP_BUILD=1 to run against the last app that was built.
dir: path to your Next.js application.beforeAll(async () => await setup('./test/fixtures'))
teardown(): cleanup after you application.
afterAll(async () => await teardown())
render(path, [query], [req], [res]): render the page pointed to by path. Returns
a cheerio DOM Element.
path: route to render.query: query string, this will be passed to render and populated in
req.url.req: the request object, populates with sensible defaults.res: the response object, populates with sensible defaults.expectElement(element): run a chai-cheerio expect() assertion against the element returned by render.
element: the element returned by a call to render.See this README for detailed documentation on the chai-cheerio/chai-jquery
assert against text content:
expectElement(element).to.have.text('Hello World. About')
using selectors:
expectElement(
element.find('#host')
).to.have.text('www.example.com')
ISC