Home

Issue: 987

unit testing with v1.x

[issue link]

I believe that since v1.x nuxt now returns a promise, would this then be an acceptable way to run tests against it? And if not how? and should/can we update the documentation?



import test from 'ava'
import Nuxt from 'nuxt'
import { resolve } from 'path'

let nuxt = null
let server = null

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).then(nuxt => {

  test.before('Initializing Nuxt.js', async t => {
    await nuxt.build()
    server = new nuxt.Server(nuxt)
    server.listen(4000, 'localhost')
  })

  test('Route / exits and renders HTML', async t => {
    let context = {}
    const { html } = await nuxt.renderRoute('/', context)
    t.true(html.includes('<div class="page home">'))
  })

  test.after('Closing the server and nuxt.js', t => {
    server.close()
    nuxt.close()
  })

})

This question is available on Nuxt.js community (#c855)