mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
refactor(examples/with-ava): replace 'nuxt.renderAndGetWindow' -> 'JSDOM'
detail: 1. 'beforeAll' no longer support change to 'test.before' 2. renderAndGetWindow const window = await nuxt.renderAndGetWindow('http://localhost:4000/') does not return 'window'. if url change to real server return window but test server just return html. so change to JSDOM way.
This commit is contained in:
parent
948d56622e
commit
2c33641b9f
@ -1,17 +1,21 @@
|
||||
import { resolve } from 'path'
|
||||
|
||||
import test from 'ava'
|
||||
import { resolve } from 'path'
|
||||
import { Nuxt, Builder } from 'nuxt'
|
||||
import { JSDOM } from 'jsdom'
|
||||
|
||||
// We keep the nuxt and server instance
|
||||
// So we can close them at the end of the test
|
||||
let nuxt = null
|
||||
|
||||
// Init Nuxt.js and create a server listening on localhost:4000
|
||||
beforeAll(async () => {
|
||||
test.before(async () => {
|
||||
const rootDir = resolve(__dirname, '..')
|
||||
let config = {}
|
||||
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
|
||||
try {
|
||||
config = require(resolve(rootDir, 'nuxt.config.js'))
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
config.rootDir = rootDir // project folder
|
||||
config.dev = false // production build
|
||||
nuxt = new Nuxt(config)
|
||||
@ -21,14 +25,16 @@ beforeAll(async () => {
|
||||
|
||||
// Example of testing only generated html
|
||||
test('Route / exits and render HTML', async t => {
|
||||
let context = {}
|
||||
const context = {}
|
||||
const { html } = await nuxt.renderRoute('/', context)
|
||||
t.true(html.includes('<h1 class="red">Hello world!</h1>'))
|
||||
})
|
||||
|
||||
// Example of testing via dom checking
|
||||
test('Route / exits and render HTML with CSS applied', async t => {
|
||||
const window = await nuxt.renderAndGetWindow('http://localhost:4000/')
|
||||
const context = {}
|
||||
const { html } = await nuxt.renderRoute('/', context)
|
||||
const { window } = new JSDOM(html).window
|
||||
const element = window.document.querySelector('.red')
|
||||
t.not(element, null)
|
||||
t.is(element.textContent, 'Hello world!')
|
||||
|
Loading…
Reference in New Issue
Block a user