Nuxt/examples/with-ava/test/index.test.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-03-16 19:52:17 +00:00
import { resolve } from 'path'
2016-11-07 01:34:58 +00:00
import test from 'ava'
2017-10-20 06:41:06 +00:00
import { Nuxt, Builder } from 'nuxt'
2016-11-07 01:34:58 +00:00
2016-12-30 11:17:52 +00:00
// We keep the nuxt and server instance
// So we can close them at the end of the test
2016-11-07 01:34:58 +00:00
let nuxt = null
2016-12-30 11:17:52 +00:00
// Init Nuxt.js and create a server listening on localhost:4000
2018-03-18 19:31:32 +00:00
beforeAll(async () => {
2016-12-30 11:17:52 +00:00
const rootDir = resolve(__dirname, '..')
let config = {}
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
config.rootDir = rootDir // project folder
config.dev = false // production build
nuxt = new Nuxt(config)
2018-03-18 19:31:32 +00:00
new Builder(nuxt).build()
2017-10-20 06:41:06 +00:00
await nuxt.listen(4000, 'localhost')
2018-03-18 19:31:32 +00:00
}, 30000)
2016-11-07 01:34:58 +00:00
2016-12-30 11:17:52 +00:00
// Example of testing only generated html
2016-11-07 01:34:58 +00:00
test('Route / exits and render HTML', async t => {
let context = {}
2016-11-10 01:19:47 +00:00
const { html } = await nuxt.renderRoute('/', context)
2016-12-30 11:17:52 +00:00
t.true(html.includes('<h1 class="red">Hello world!</h1>'))
2016-11-07 01:34:58 +00:00
})
2016-12-30 11:17:52 +00:00
// Example of testing via dom checking
test('Route / exits and render HTML with CSS applied', async t => {
2016-12-20 16:56:06 +00:00
const window = await nuxt.renderAndGetWindow('http://localhost:4000/')
2016-12-30 11:17:52 +00:00
const element = window.document.querySelector('.red')
t.not(element, null)
t.is(element.textContent, 'Hello world!')
2016-12-30 11:17:52 +00:00
t.is(element.className, 'red')
t.is(window.getComputedStyle(element).color, 'red')
2016-11-07 01:34:58 +00:00
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
2016-11-07 01:34:58 +00:00
})