next-test-helper
v1.0.6
Published
test helpers for your next.js applications
Downloads
21
Readme
next-test-helpers
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')
})
})
the api
setup(dir): build the application pointed to by
dir
. Run withSKIP_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 torender
and populated inreq.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 byrender
.element
: the element returned by a call torender
.
chai-cheerio examples
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')
license
ISC