Node.js v10.9.0
win32 10.0.17134
npm v6.4.1
ava 0.25.0
In this reproduction there is one test file, which has "before" section, where we build Nuxt and listen to http://localhost:4000, but test never ends.
import test from 'ava'
import { Nuxt, Builder } from 'nuxt'
let nuxt = null
test.before.serial(async () => {
nuxt = new Nuxt({
dev: false,
env: {
isDev: false
},
mode: 'universal'
})
try {
console.log('building...')
await new Builder(nuxt).build()
console.log('built')
await nuxt.listen(4000, 'localhost')
console.log('listening')
} catch (e) {
console.log(e)
}
})
test('some test', async t => {
let res = await nuxt.renderRoute('/')
console.log(res) // never logs
t.pass() // never reaches
})
test.after('Closing server and nuxt.js', (t) => {
nuxt.close()
})
However, when I run such file without ava, all works fine:
const { Nuxt, Builder } = require('nuxt')
async function init() {
let nuxt = new Nuxt({
dev: false,
mode: 'universal'
})
console.log('building')
await new Builder(nuxt).build()
console.log('built')
await nuxt.listen(4000, 'localhost')
console.log('listening...')
const res = await nuxt.renderRoute('/')
console.log(res) // shows result
}
init()
same is for ava: 1.0.0-rc.1
reproduction link: https://github.com/p1pchenk0/nuxt-render-route-hangs
It's odd. When you run the standalone script there's build output, but not when it's run through AVA. I think Nuxt is acting a little different, but I'm not sure why.
For instance, the port 4000 is definitely open but it's not responding even if I curl it directly.
If there's something wrong with the test environment we'd be happy to fix it. That said though, given what Nuxt is doing I'd recommend starting it outside of AVA, not from inside a test file.
Closing this for housekeeping purposes, but again if somebody can figure out if there's something in the test environment that throws off Nuxt we'd be happy to fix it.
@novemberborn is there any way to let ava know to wait for nuxt build? I suppose, it has to be done inside setup file
@p1pchenk0 the simplest approach would be to assume the build server is running. You should be able to script it in CI flows. It's what I do with database or queue servers.
Hi @p1pchenk0 and @novemberborn , I just ran into this issue too and solved it....
TL;DR: @vinayakkulkarni is the man! When in doubt, copy exactly his setup to start. See his repo
---- More details ---
So, when you create a nuxt app using create-nuxt-app and specify to use the ava test framework, it generates the ava setup files, but, while the npm scripts specify to use the ENV vars "TEST" (i.e., TEST=unit and TEST=e2e) the setup files forget to actually key off those variable...so the generated ava.setup.js is require'ing ('browser-env') for e2e tests when it shouldn't have...
So, what's the fix for e2e? Answer:
ava.setup.js:
const hooks = require('require-extension-hooks')
if (process.env.TEST === 'unit') { // These checks are missing for create-nuxt-app generated files
require('jsdom-global')()
require('browser-env')
const Vue = require('vue')
Vue.config.productionTip = false
// https://github.com/nuxt/create-nuxt-app/issues/180#issuecomment-463069941
window.Date = global.Date = Date
}
if (process.env.TEST === 'e2e') { // These checks are missing for create-nuxt-app generated files
const Vue = require('vue')
Vue.config.productionTip = false
}
hooks('vue')
.plugin('vue')
.push()
hooks(['vue', 'js'])
.exclude(({ filename }) => filename.match(/\/node_modules\//))
.plugin('babel')
.push()
Most helpful comment
Hi @p1pchenk0 and @novemberborn , I just ran into this issue too and solved it....
TL;DR: @vinayakkulkarni is the man! When in doubt, copy exactly his setup to start. See his repo
---- More details ---
So, when you create a nuxt app using create-nuxt-app and specify to use the ava test framework, it generates the ava setup files, but, while the npm scripts specify to use the ENV vars "TEST" (i.e., TEST=unit and TEST=e2e) the setup files forget to actually key off those variable...so the generated ava.setup.js is require'ing ('browser-env') for e2e tests when it shouldn't have...
So, what's the fix for e2e? Answer:
ava.setup.js: