refactor: remove builder coupling from server (#5157)

This commit is contained in:
Pooya Parsa 2019-03-09 00:13:23 +03:30 committed by Xin Du (Clark)
parent 9e1ef888d8
commit 13cb0f73d9
43 changed files with 210 additions and 158 deletions

View File

@ -75,12 +75,9 @@ export default class Builder {
this.template = this.nuxt.resolver.requireModule(this.template).template
}
// if(!this.options.dev) {
// TODO: enable again when unsafe concern resolved.(common/options.js:42)
// this.nuxt.hook('build:done', () => this.generateConfig())
// }
// Create a new bundle builder
this.bundleBuilder = this.getBundleBuilder(bundleBuilder)
this.ignore = new Ignore({
rootDir: this.options.srcDir
})

View File

@ -86,8 +86,10 @@ export default class NuxtCommand {
async getNuxt(options) {
const { Nuxt } = await imports.core()
const nuxt = new Nuxt(options)
await nuxt.ready()
return nuxt
}

View File

@ -61,7 +61,7 @@ export default {
}
},
async run(cmd) {
const config = await cmd.getNuxtConfig({ dev: false })
const config = await cmd.getNuxtConfig({ dev: false, server: false })
const nuxt = await cmd.getNuxt(config)
if (cmd.argv.lock) {

View File

@ -47,12 +47,15 @@ export default {
nuxt.hook('watch:restart', payload => this.onWatchRestart(payload, { nuxt, builder, cmd, argv }))
nuxt.hook('bundler:change', changedFileName => this.onBundlerChange(changedFileName))
// Start listening
await nuxt.server.listen()
// Create builder instance
const builder = await cmd.getBuilder(nuxt)
// Wait for nuxt to be ready
await nuxt.ready()
// Start listening
await nuxt.server.listen()
// Start Build
await builder.build()

View File

@ -22,7 +22,6 @@ export default class Nuxt extends Hookable {
// Create instance of core components
this.resolver = new Resolver(this)
this.moduleContainer = new ModuleContainer(this)
this.server = new Server(this)
// Deprecated hooks
this._deprecatedHooks = {
@ -32,27 +31,33 @@ export default class Nuxt extends Hookable {
}
// Add Legacy aliases
defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
defineAlias(this, this.resolver, ['resolveAlias', 'resolvePath'])
this.renderer = this.server
this.render = this.server.app
this.showReady = () => { this.callHook('webpack:done') }
// Wait for Nuxt to be ready
this.initialized = false
this._ready = this.ready().catch((err) => {
consola.fatal(err)
})
// Init server
if (this.options.server !== false) {
this._initServer()
}
}
static get version() {
return (global.__NUXT && global.__NUXT.version) || `v${version}`
}
async ready() {
if (this._ready) {
return this._ready
ready() {
if (!this._ready) {
this._ready = this._init().catch((err) => {
consola.fatal(err)
})
}
return this._ready
}
async _init() {
if (this._initCalled) {
return this
}
this._initCalled = true
// Add hooks
if (isPlainObject(this.options.hooks)) {
@ -65,9 +70,9 @@ export default class Nuxt extends Hookable {
await this.moduleContainer.ready()
// Await for server to be ready
await this.server.ready()
this.initialized = true
if (this.server) {
await this.server.ready()
}
// Call ready hook
await this.callHook('ready', this)
@ -75,6 +80,16 @@ export default class Nuxt extends Hookable {
return this
}
_initServer() {
if (this.server) {
return
}
this.server = new Server(this)
this.renderer = this.server
this.render = this.server.app
defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
}
async close(callback) {
await this.callHook('close', this)

View File

@ -10,26 +10,22 @@ import Resolver from '../src/resolver'
import { version } from '../package.json'
jest.mock('@nuxt/utils')
jest.mock('@nuxt/server')
jest.mock('@nuxt/config', () => ({
getNuxtConfig: jest.fn(() => ({}))
}))
jest.mock('@nuxt/server')
describe('core: nuxt', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(Nuxt.prototype, 'ready').mockImplementation(() => Promise.resolve())
})
afterEach(() => {
if (Nuxt.prototype.ready.mockRestore) {
Nuxt.prototype.ready.mockRestore()
}
})
test('should construct nuxt with options', () => {
test('should construct nuxt with options', async () => {
const options = {}
const nuxt = new Nuxt(options)
await nuxt.ready()
expect(nuxt).toBeInstanceOf(Hookable)
expect(getNuxtConfig).toBeCalledTimes(1)
@ -46,15 +42,14 @@ describe('core: nuxt', () => {
})
expect(defineAlias).toBeCalledTimes(2)
expect(defineAlias).nthCalledWith(1, nuxt, nuxt.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
expect(defineAlias).nthCalledWith(2, nuxt, nuxt.resolver, ['resolveAlias', 'resolvePath'])
expect(defineAlias).nthCalledWith(1, nuxt, nuxt.resolver, ['resolveAlias', 'resolvePath'])
expect(defineAlias).nthCalledWith(2, nuxt, nuxt.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
expect(nuxt.renderer).toBe(nuxt.server)
expect(nuxt.render).toBe(nuxt.server.app)
expect(nuxt.showReady).toBeInstanceOf(Function)
expect(nuxt.initialized).toEqual(false)
expect(nuxt.ready).toBeCalledTimes(1)
expect(nuxt._ready).toBeInstanceOf(Promise)
})
// TODO: Remove in next major release
@ -70,10 +65,9 @@ describe('core: nuxt', () => {
test('should display fatal message if ready failed', async () => {
const err = new Error('nuxt ready failed')
Nuxt.prototype.ready.mockImplementation(() => Promise.reject(err))
const nuxt = new Nuxt()
await nuxt._ready
nuxt._init = () => Promise.reject(err)
await nuxt.ready()
expect(consola.fatal).toBeCalledTimes(1)
expect(consola.fatal).toBeCalledWith(err)
@ -95,8 +89,6 @@ describe('core: nuxt', () => {
test('should call module/server ready in nuxt.ready', async () => {
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()
nuxt.callHook = jest.fn()
nuxt.server = { ready: jest.fn() }
@ -107,29 +99,22 @@ describe('core: nuxt', () => {
expect(result).toBe(nuxt)
expect(nuxt.moduleContainer.ready).toBeCalledTimes(1)
expect(nuxt.server.ready).toBeCalledTimes(1)
expect(nuxt.initialized).toEqual(true)
expect(nuxt._initCalled).toEqual(true)
expect(nuxt.callHook).toBeCalledTimes(1)
expect(nuxt.callHook).toBeCalledWith('ready', nuxt)
})
test('should ignore ready when _ready exists', async () => {
const nuxt = new Nuxt()
Nuxt.prototype.ready.mockRestore()
const _ready = nuxt._ready = jest.fn()
nuxt.server = { ready: jest.fn() }
const result = await nuxt.ready()
expect(result).toBe(_ready)
expect(nuxt.server.ready).not.toBeCalled()
})
test('should add object hooks', async () => {
const hooks = {}
getNuxtConfig.mockReturnValueOnce({ hooks })
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()
nuxt.addHooks = jest.fn()
nuxt.server = { ready: jest.fn() }
@ -145,8 +130,6 @@ describe('core: nuxt', () => {
const hooks = jest.fn()
getNuxtConfig.mockReturnValueOnce({ hooks })
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()
nuxt.addHooks = jest.fn()
nuxt.server = { ready: jest.fn() }
@ -174,8 +157,6 @@ describe('core: nuxt', () => {
test('should ignore non-function callback in close', async () => {
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()
nuxt.callHook = jest.fn()
nuxt.server = { ready: jest.fn() }
@ -186,7 +167,7 @@ describe('core: nuxt', () => {
expect(result).toBe(nuxt)
expect(nuxt.moduleContainer.ready).toBeCalledTimes(1)
expect(nuxt.server.ready).toBeCalledTimes(1)
expect(nuxt.initialized).toEqual(true)
expect(nuxt._initCalled).toEqual(true)
expect(nuxt.callHook).toBeCalledTimes(1)
expect(nuxt.callHook).toBeCalledWith('ready', nuxt)
})

View File

@ -28,10 +28,6 @@ export default class Server {
// Runtime shared resources
this.resources = {}
// Will be available on dev
this.devMiddleware = null
this.hotMiddleware = null
// Will be set after listen
this.listeners = []
@ -40,9 +36,21 @@ export default class Server {
// Close hook
this.nuxt.hook('close', () => this.close())
// devMiddleware placeholder
if (this.options.dev) {
this.nuxt.hook('server:devMiddleware', (devMiddleware) => {
this.devMiddleware = devMiddleware
})
}
}
async ready() {
if (this._readyCalled) {
return this
}
this._readyCalled = true
await this.nuxt.callHook('render:before', this, this.options.render)
// Initialize vue-renderer
@ -57,6 +65,8 @@ export default class Server {
// Call done hook
await this.nuxt.callHook('render:done', this)
return this
}
async setupMiddleware() {
@ -80,33 +90,6 @@ export default class Server {
this.useMiddleware(createTimingMiddleware(this.options.server.timing))
}
const modernMiddleware = createModernMiddleware({
context: this.renderer.context
})
// Add webpack middleware support only for development
if (this.options.dev) {
this.useMiddleware(modernMiddleware)
this.useMiddleware(async (req, res, next) => {
const name = req.modernMode ? 'modern' : 'client'
if (this.devMiddleware && this.devMiddleware[name]) {
await this.devMiddleware[name](req, res)
}
if (this.hotMiddleware && this.hotMiddleware[name]) {
await this.hotMiddleware[name](req, res)
}
next()
})
}
// open in editor for debug mode only
if (this.options.debug && this.options.dev) {
this.useMiddleware({
path: '__open-in-editor',
handler: launchMiddleware(this.options.editor)
})
}
// For serving static/ files to /
const staticMiddleware = serveStatic(
path.resolve(this.options.srcDir, this.options.dir.static),
@ -126,7 +109,27 @@ export default class Server {
this.options.render.dist
)
})
this.useMiddleware(modernMiddleware)
}
this.useMiddleware(createModernMiddleware({
context: this.renderer.context
}))
if (this.options.dev) {
this.useMiddleware((req, res, next) => {
if (!this.devMiddleware) {
return next()
}
this.devMiddleware(req, res, next)
})
// open in editor for debug mode only
if (this.options.debug) {
this.useMiddleware({
path: '__open-in-editor',
handler: launchMiddleware(this.options.editor)
})
}
}
// Add user provided middleware
@ -134,9 +137,10 @@ export default class Server {
this.useMiddleware(m)
}
// Graceful 404 error handler
const { fallback } = this.options.render
if (fallback) {
// Graceful 404 errors for dist files
// Dist files
if (fallback.dist) {
this.useMiddleware({
path: this.publicPath,
@ -144,7 +148,7 @@ export default class Server {
})
}
// Graceful 404 errors for other paths
// Other paths
if (fallback.static) {
this.useMiddleware({
path: '/',
@ -161,14 +165,10 @@ export default class Server {
resources: this.resources
}))
// Error middleware for errors that occurred in middleware that declared above
// Middleware should exactly take 4 arguments
// https://github.com/senchalabs/connect#error-middleware
// Apply errorMiddleware from modules first
await this.nuxt.callHook('render:errorMiddleware', this.app)
// Apply errorMiddleware from Nuxt
// Error middleware for errors that occurred in middleware that declared above
this.useMiddleware(errorMiddleware({
resources: this.resources,
options: this.options
@ -228,7 +228,7 @@ export default class Server {
}
async listen(port, host, socket) {
// Don't start listening before nuxt is ready
// Ensure nuxt is ready
await this.nuxt.ready()
// Create a new listener

View File

@ -116,8 +116,6 @@ describe('server: server', () => {
expect(server.options).toBe(nuxt.options)
expect(server.publicPath).toBe('__nuxt_test')
expect(server.resources).toEqual({})
expect(server.devMiddleware).toBeNull()
expect(server.hotMiddleware).toBeNull()
expect(server.listeners).toEqual([])
expect(connect).toBeCalledTimes(1)
expect(server.nuxt.hook).toBeCalledTimes(1)
@ -272,48 +270,6 @@ describe('server: server', () => {
expect(server.useMiddleware).nthCalledWith(1, { id: 'test-server-timing' })
})
test('should setup dev middleware', async () => {
const nuxt = createNuxt()
nuxt.options.dev = true
const server = new Server(nuxt)
server.useMiddleware = jest.fn()
server.renderer = {
context: { id: 'test-server-context' }
}
await server.setupMiddleware()
expect(server.useMiddleware).nthCalledWith(1, {
id: 'test-modern-middleware',
context: server.renderer.context
})
const devMiddleware = server.useMiddleware.mock.calls[1][0]
const req = { id: 'req' }
const res = { id: 'res' }
const next = jest.fn()
await devMiddleware(req, res, next)
expect(next).toBeCalledTimes(1)
next.mockClear()
server.devMiddleware = { client: jest.fn() }
server.hotMiddleware = { client: jest.fn() }
await devMiddleware(req, res, next)
expect(server.devMiddleware.client).nthCalledWith(1, req, res)
expect(server.hotMiddleware.client).nthCalledWith(1, req, res)
expect(next).toBeCalledTimes(1)
next.mockClear()
req.modernMode = true
server.devMiddleware = { modern: jest.fn() }
server.hotMiddleware = { modern: jest.fn() }
await devMiddleware(req, res, next)
expect(server.devMiddleware.modern).nthCalledWith(1, req, res)
expect(server.hotMiddleware.modern).nthCalledWith(1, req, res)
expect(next).toBeCalledTimes(1)
})
test('should setup open-in-editor middleware', async () => {
const nuxt = createNuxt()
nuxt.options.dev = true
@ -330,7 +286,7 @@ describe('server: server', () => {
expect(launchMiddleware).toBeCalledTimes(1)
expect(launchMiddleware).toBeCalledWith({ id: 'test-editor' })
expect(server.useMiddleware).nthCalledWith(3, {
expect(server.useMiddleware).nthCalledWith(4, {
handler: { id: 'test-editor' },
path: '__open-in-editor'
})

View File

@ -111,6 +111,11 @@ export default class VueRenderer {
}
async ready() {
if (this._readyCalled) {
return this
}
this._readyCalled = true
// -- Development mode --
if (this.context.options.dev) {
this.context.nuxt.hook('build:resources', mfs => this.loadResources(mfs))
@ -139,6 +144,8 @@ export default class VueRenderer {
'No modern build files found. Use either `nuxt build --modern` or `modern` option to build modern files.'
)
}
return this
}
async loadResources(_fs) {
@ -413,6 +420,10 @@ export default class VueRenderer {
async renderRoute(url, context = {}, retries = 5) {
/* istanbul ignore if */
if (!this.isReady) {
if (!this._readyCalled) {
throw new Error('Nuxt is not initialized! `nuxt.ready()` should be called!')
}
if (!this.context.options.dev || retries <= 0) {
throw new Error('Server resources are not available!')
}

View File

@ -21,12 +21,16 @@ const glob = pify(Glob)
export class WebpackBundler {
constructor(buildContext) {
this.buildContext = buildContext
// Fields that set on build
// Class fields
this.compilers = []
this.compilersWatching = []
this.devMiddleware = {}
this.hotMiddleware = {}
// Bind middleware to self
this.middleware = this.middleware.bind(this)
// Initialize shared MFS for dev
if (this.buildContext.options.dev) {
this.mfs = new AsyncMFS()
@ -140,7 +144,7 @@ export class WebpackBundler {
if (['client', 'modern'].includes(name)) {
return new Promise((resolve, reject) => {
compiler.hooks.done.tap('nuxt-dev', () => resolve())
this.webpackDev(compiler)
return this.webpackDev(compiler)
})
}
@ -171,12 +175,12 @@ export class WebpackBundler {
}
}
webpackDev(compiler) {
consola.debug('Adding webpack middleware...')
async webpackDev(compiler) {
consola.debug('Creating webpack middleware...')
const { name } = compiler.options
const { nuxt: { server }, options } = this.buildContext
const { client, ...hotMiddlewareOptions } = options.build.hotMiddleware || {}
const buildOptions = this.buildContext.options.build
const { client, ...hotMiddlewareOptions } = buildOptions.hotMiddleware || {}
// Create webpack dev middleware
this.devMiddleware[name] = pify(
@ -184,12 +188,12 @@ export class WebpackBundler {
compiler,
Object.assign(
{
publicPath: options.build.publicPath,
publicPath: buildOptions.publicPath,
stats: false,
logLevel: 'silent',
watchOptions: options.watchers.webpack
watchOptions: this.buildContext.options.watchers.webpack
},
options.build.devMiddleware
buildOptions.devMiddleware
)
)
)
@ -212,11 +216,22 @@ export class WebpackBundler {
)
)
// Inject to renderer instance
if (server) {
server.devMiddleware = this.devMiddleware
server.hotMiddleware = this.hotMiddleware
// Register devMiddleware on server
await this.buildContext.nuxt.callHook('server:devMiddleware', this.middleware)
}
async middleware(req, res, next) {
const name = req.modernMode ? 'modern' : 'client'
if (this.devMiddleware && this.devMiddleware[name]) {
await this.devMiddleware[name](req, res)
}
if (this.hotMiddleware && this.hotMiddleware[name]) {
await this.hotMiddleware[name](req, res)
}
next()
}
async unwatch() {

View File

@ -12,6 +12,8 @@ describe('basic browser', () => {
beforeAll(async () => {
const config = await loadFixture('basic')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')

View File

@ -11,6 +11,8 @@ let page = null
const startServer = async (type = 'basic') => {
const config = await loadFixture(type)
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')

View File

@ -13,6 +13,8 @@ describe('children patch (browser)', () => {
beforeAll(async () => {
const options = await loadFixture('children')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -11,6 +11,8 @@ describe('size-limit test', () => {
beforeAll(async () => {
const options = await loadFixture('async-config')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')

View File

@ -7,6 +7,8 @@ describe('basic ssr', () => {
beforeAll(async () => {
const options = await loadFixture('async-config')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
})

View File

@ -9,8 +9,9 @@ describe('basic fail generate', () => {
}
}
})
const nuxt = new Nuxt(options)
await nuxt.ready()
const generator = new Generator(nuxt)
await generator.generate({ build: false }).catch((e) => {

View File

@ -21,6 +21,7 @@ describe('basic generate', () => {
beforeAll(async () => {
const config = await loadFixture('basic', { generate: { dir: '.nuxt-generate' } })
const nuxt = new Nuxt(config)
await nuxt.ready()
pathsBefore = listPaths(nuxt.options.rootDir)

View File

@ -9,6 +9,8 @@ describe('with-config', () => {
beforeAll(async () => {
const config = await loadFixture('basic')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -9,6 +9,8 @@ const startCspServer = async (csp, isProduction = true) => {
render: { csp }
})
const nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
return nuxt

View File

@ -10,6 +10,8 @@ describe('basic ssr', () => {
beforeAll(async () => {
const options = await loadFixture('basic')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
})

View File

@ -9,6 +9,8 @@ describe('children', () => {
beforeAll(async () => {
const options = await loadFixture('children')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -7,6 +7,8 @@ describe('custom-app-template', () => {
beforeAll(async () => {
const options = await loadFixture('custom-app-template')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
})

View File

@ -12,6 +12,8 @@ describe('custom-dirs', () => {
beforeAll(async () => {
const config = await loadFixture('custom-dirs')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -9,6 +9,8 @@ describe('dist options', () => {
beforeAll(async () => {
const options = await loadFixture('basic')
nuxt = new Nuxt(Object.assign(options, { dev: false }))
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
})

View File

@ -12,6 +12,8 @@ describe('error', () => {
beforeAll(async () => {
const config = await loadFixture('error')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -11,6 +11,8 @@ describe('extract css', () => {
beforeAll(async () => {
const options = await loadFixture('extract-css')
nuxt = new Nuxt(options)
await nuxt.ready()
await nuxt.server.listen(await getPort(), '0.0.0.0')
})

View File

@ -8,7 +8,10 @@ let nuxt = null
describe('fallback', () => {
beforeAll(async () => {
const config = await loadFixture('with-config')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -26,6 +26,8 @@ describe('build filenames with query part', () => {
beforeAll(async () => {
const config = await loadFixture('filenames-query-part')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -9,6 +9,7 @@ describe('generator', () => {
}
}
const nuxt = new Nuxt(config)
await nuxt.ready()
const generator = new Generator(nuxt)
const routes = await generator.initRoutes()
@ -28,6 +29,7 @@ describe('generator', () => {
}
}
const nuxt = new Nuxt(config)
await nuxt.ready()
const generator = new Generator(nuxt)
const routes = await generator.initRoutes()
@ -46,6 +48,7 @@ describe('generator', () => {
}
}
const nuxt = new Nuxt(config)
await nuxt.ready()
const generator = new Generator(nuxt)
const array = ['/1', '/2', '/3', '/4']
const routes = await generator.initRoutes(array)
@ -65,6 +68,7 @@ describe('generator', () => {
}
}
const nuxt = new Nuxt(config)
await nuxt.ready()
const generator = new Generator(nuxt)
const array = ['/1', '/2', '/3', '/4']
const routes = await generator.initRoutes(...array)

View File

@ -6,6 +6,8 @@ describe('basic https', () => {
beforeAll(async () => {
const options = await loadFixture('https')
nuxt = new Nuxt(options)
await nuxt.ready()
const port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
})

View File

@ -6,6 +6,8 @@ describe('meta-attrs', () => {
beforeAll(async () => {
const options = await loadFixture('meta-attrs')
nuxt = new Nuxt(options)
await nuxt.ready()
await nuxt.server.listen(await getPort(), '0.0.0.0')
})

View File

@ -7,6 +7,8 @@ describe('modern client mode (SSR)', () => {
beforeAll(async () => {
const options = await loadFixture('modern', { modern: 'client' })
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -11,6 +11,8 @@ describe('modern server mode', () => {
beforeAll(async () => {
const options = await loadFixture('modern')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -11,6 +11,8 @@ describe('modern client mode (SPA)', () => {
beforeAll(async () => {
options = await loadFixture('modern', { render: { ssr: false } })
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -13,6 +13,8 @@ describe('module', () => {
beforeAll(async () => {
const config = await loadFixture('module')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -7,6 +7,8 @@ describe('named views', () => {
beforeAll(async () => {
const options = await loadFixture('named-views')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
})

View File

@ -9,14 +9,12 @@ describe('nuxt', () => {
test('Nuxt.js Instance', async () => {
const config = await loadFixture('empty')
const nuxt = new Nuxt(config)
await nuxt.ready()
expect(typeof nuxt).toBe('object')
expect(nuxt.options.dev).toBe(false)
expect(typeof nuxt._ready.then).toBe('function')
await nuxt.ready()
expect(nuxt.initialized).toBe(true)
expect(nuxt._initCalled).toBe(true)
})
test('Fail to build when no pages/ directory but is in the parent', async () => {
@ -38,6 +36,8 @@ describe('nuxt', () => {
test('Build with default page when no pages/ directory', async () => {
const config = await loadFixture('missing-pages-dir')
const nuxt = new Nuxt(config)
await nuxt.ready()
const port = await getPort()
await nuxt.server.listen(port, 'localhost')
@ -51,6 +51,7 @@ describe('nuxt', () => {
test('Fail to build when specified plugin isn\'t found', async () => {
const config = await loadFixture('missing-plugin')
const nuxt = new Nuxt(config)
await nuxt.ready()
await expect(new Builder(nuxt).build()).rejects.toThrow('Plugin not found')
})
@ -58,6 +59,7 @@ describe('nuxt', () => {
test('Warn when styleResource isn\'t found', async () => {
const config = await loadFixture('missing-style-resource')
const nuxt = new Nuxt(config)
await nuxt.ready()
await expect(new Builder(nuxt).build()).rejects.toThrow('Style Resource not found')
})

View File

@ -11,6 +11,8 @@ describe('server listen', () => {
test('should throw error when listening on same port (prod)', async () => {
const nuxt = new Nuxt(config)
await nuxt.ready()
const port = await getPort()
const listen = () => nuxt.server.listen(port, 'localhost')
@ -26,6 +28,8 @@ describe('server listen', () => {
test('should assign a random port when listening on same port (dev)', async () => {
const nuxt = new Nuxt({ ...config, dev: true })
await nuxt.ready()
const port = await getPort()
const listen = () => nuxt.server.listen(port, 'localhost')
@ -53,6 +57,8 @@ describe('server listen', () => {
// Setup test
const nuxt = new Nuxt({ ...config, dev: true })
await nuxt.ready()
const listen = () => nuxt.server.listen(0, 'localhost') // Use port 0 to let allow host to randomly assign a free PORT
const toString = (x = '') => `${x}`

View File

@ -4,6 +4,8 @@ describe.posix('basic sockets', () => {
test('/', async () => {
const options = await loadFixture('sockets')
const nuxt = new Nuxt(options)
await nuxt.ready()
await nuxt.server.listen()
const { html } = await nuxt.server.renderRoute('/')

View File

@ -15,6 +15,8 @@ describe('spa', () => {
beforeAll(async () => {
const config = await loadFixture('spa')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -66,6 +66,8 @@ describe('ssr', () => {
beforeAll(async () => {
const config = await loadFixture('ssr')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})

View File

@ -9,6 +9,7 @@ describe('typescript', () => {
const options = await loadFixture('typescript')
nuxt = new Nuxt(options)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '0.0.0.0')
})

View File

@ -16,6 +16,8 @@ describe('with-config', () => {
beforeAll(async () => {
const config = await loadFixture('with-config')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})
@ -220,6 +222,8 @@ describe('server config', () => {
const config = await loadFixture('with-config')
config.server.port = port = await getPort()
nuxt = new Nuxt(config)
await nuxt.ready()
await nuxt.server.listen()
await nuxt.server.renderAndGetWindow(url('/test/'))
})