From 13cb0f73d9bd538b9cecd5d875077ea8e3166c0f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 9 Mar 2019 00:13:23 +0330 Subject: [PATCH] refactor: remove builder coupling from server (#5157) --- packages/builder/src/builder.js | 7 +- packages/cli/src/command.js | 2 + packages/cli/src/commands/build.js | 2 +- packages/cli/src/commands/dev.js | 9 ++- packages/core/src/nuxt.js | 45 ++++++++----- packages/core/test/nuxt.test.js | 43 ++++-------- packages/server/src/server.js | 80 +++++++++++------------ packages/server/test/server.test.js | 46 +------------ packages/vue-renderer/src/renderer.js | 11 ++++ packages/webpack/src/builder.js | 41 ++++++++---- test/e2e/basic.browser.test.js | 2 + test/e2e/basic.vue-config.test.js | 2 + test/e2e/children.patch.browser.test.js | 2 + test/unit/async-config.size-limit.test.js | 2 + test/unit/async-config.test.js | 2 + test/unit/basic.fail.generate.test.js | 3 +- test/unit/basic.generate.test.js | 1 + test/unit/basic.plugins.test.js | 2 + test/unit/basic.ssr.csp.test.js | 2 + test/unit/basic.ssr.test.js | 2 + test/unit/children.test.js | 2 + test/unit/custom-app-template.test.js | 2 + test/unit/custom-dirs.test.js | 2 + test/unit/dist-options.test.js | 2 + test/unit/error.test.js | 2 + test/unit/extract-css.test.js | 2 + test/unit/fallback.test.js | 3 + test/unit/filenames-query-part.test.js | 2 + test/unit/generator.test.js | 4 ++ test/unit/https.test.js | 2 + test/unit/meta-attrs.test.js | 2 + test/unit/modern.client.test.js | 2 + test/unit/modern.server.test.js | 2 + test/unit/modern.spa.test.js | 2 + test/unit/module.test.js | 2 + test/unit/named-views.test.js | 2 + test/unit/nuxt.test.js | 10 +-- test/unit/server.listen.test.js | 6 ++ test/unit/sockets.test.js | 2 + test/unit/spa.test.js | 2 + test/unit/ssr.test.js | 2 + test/unit/typescript.test.js | 1 + test/unit/with-config.test.js | 4 ++ 43 files changed, 210 insertions(+), 158 deletions(-) diff --git a/packages/builder/src/builder.js b/packages/builder/src/builder.js index c50d21a9be..e4ebf4d3a2 100644 --- a/packages/builder/src/builder.js +++ b/packages/builder/src/builder.js @@ -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 }) diff --git a/packages/cli/src/command.js b/packages/cli/src/command.js index 0033811252..d09e9b4a68 100644 --- a/packages/cli/src/command.js +++ b/packages/cli/src/command.js @@ -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 } diff --git a/packages/cli/src/commands/build.js b/packages/cli/src/commands/build.js index 1787da6397..bb0d1342a2 100644 --- a/packages/cli/src/commands/build.js +++ b/packages/cli/src/commands/build.js @@ -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) { diff --git a/packages/cli/src/commands/dev.js b/packages/cli/src/commands/dev.js index 31329ec245..4cfe5d7838 100644 --- a/packages/cli/src/commands/dev.js +++ b/packages/cli/src/commands/dev.js @@ -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() diff --git a/packages/core/src/nuxt.js b/packages/core/src/nuxt.js index 486764a812..395437196c 100644 --- a/packages/core/src/nuxt.js +++ b/packages/core/src/nuxt.js @@ -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) diff --git a/packages/core/test/nuxt.test.js b/packages/core/test/nuxt.test.js index aecb1c8380..23850ace59 100644 --- a/packages/core/test/nuxt.test.js +++ b/packages/core/test/nuxt.test.js @@ -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) }) diff --git a/packages/server/src/server.js b/packages/server/src/server.js index a055c4ec1d..5e57cbba96 100644 --- a/packages/server/src/server.js +++ b/packages/server/src/server.js @@ -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 diff --git a/packages/server/test/server.test.js b/packages/server/test/server.test.js index de49133682..55e46e4579 100644 --- a/packages/server/test/server.test.js +++ b/packages/server/test/server.test.js @@ -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' }) diff --git a/packages/vue-renderer/src/renderer.js b/packages/vue-renderer/src/renderer.js index 8c5f7b14e2..97f79f15c8 100644 --- a/packages/vue-renderer/src/renderer.js +++ b/packages/vue-renderer/src/renderer.js @@ -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!') } diff --git a/packages/webpack/src/builder.js b/packages/webpack/src/builder.js index df03f63c9b..5b684772b4 100644 --- a/packages/webpack/src/builder.js +++ b/packages/webpack/src/builder.js @@ -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() { diff --git a/test/e2e/basic.browser.test.js b/test/e2e/basic.browser.test.js index 0b7a402ea5..544fd54767 100644 --- a/test/e2e/basic.browser.test.js +++ b/test/e2e/basic.browser.test.js @@ -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') diff --git a/test/e2e/basic.vue-config.test.js b/test/e2e/basic.vue-config.test.js index bb0a008558..079cc27448 100644 --- a/test/e2e/basic.vue-config.test.js +++ b/test/e2e/basic.vue-config.test.js @@ -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') diff --git a/test/e2e/children.patch.browser.test.js b/test/e2e/children.patch.browser.test.js index e2fef3a1bd..c21be63a1b 100644 --- a/test/e2e/children.patch.browser.test.js +++ b/test/e2e/children.patch.browser.test.js @@ -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') }) diff --git a/test/unit/async-config.size-limit.test.js b/test/unit/async-config.size-limit.test.js index b9152ea0e6..2cdb75ad81 100644 --- a/test/unit/async-config.size-limit.test.js +++ b/test/unit/async-config.size-limit.test.js @@ -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') diff --git a/test/unit/async-config.test.js b/test/unit/async-config.test.js index 15c4111edb..94d772e19d 100644 --- a/test/unit/async-config.test.js +++ b/test/unit/async-config.test.js @@ -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') }) diff --git a/test/unit/basic.fail.generate.test.js b/test/unit/basic.fail.generate.test.js index fa3b12b200..654571d4da 100644 --- a/test/unit/basic.fail.generate.test.js +++ b/test/unit/basic.fail.generate.test.js @@ -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) => { diff --git a/test/unit/basic.generate.test.js b/test/unit/basic.generate.test.js index 8ff311b7a7..9831ca5c10 100644 --- a/test/unit/basic.generate.test.js +++ b/test/unit/basic.generate.test.js @@ -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) diff --git a/test/unit/basic.plugins.test.js b/test/unit/basic.plugins.test.js index 4d1e470f1f..9f6cf47748 100644 --- a/test/unit/basic.plugins.test.js +++ b/test/unit/basic.plugins.test.js @@ -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') }) diff --git a/test/unit/basic.ssr.csp.test.js b/test/unit/basic.ssr.csp.test.js index eaedce24b2..eee5ea7332 100644 --- a/test/unit/basic.ssr.csp.test.js +++ b/test/unit/basic.ssr.csp.test.js @@ -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 diff --git a/test/unit/basic.ssr.test.js b/test/unit/basic.ssr.test.js index 9ca12be26c..c98cba07f6 100644 --- a/test/unit/basic.ssr.test.js +++ b/test/unit/basic.ssr.test.js @@ -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') }) diff --git a/test/unit/children.test.js b/test/unit/children.test.js index 24404670fe..500a119927 100644 --- a/test/unit/children.test.js +++ b/test/unit/children.test.js @@ -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') }) diff --git a/test/unit/custom-app-template.test.js b/test/unit/custom-app-template.test.js index bfee9da8cf..9a9c177920 100644 --- a/test/unit/custom-app-template.test.js +++ b/test/unit/custom-app-template.test.js @@ -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') }) diff --git a/test/unit/custom-dirs.test.js b/test/unit/custom-dirs.test.js index 53c46f9eac..f19f408999 100644 --- a/test/unit/custom-dirs.test.js +++ b/test/unit/custom-dirs.test.js @@ -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') }) diff --git a/test/unit/dist-options.test.js b/test/unit/dist-options.test.js index 8fa229492a..c0d781441d 100644 --- a/test/unit/dist-options.test.js +++ b/test/unit/dist-options.test.js @@ -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') }) diff --git a/test/unit/error.test.js b/test/unit/error.test.js index b5d48d15ac..0745b2c0f6 100644 --- a/test/unit/error.test.js +++ b/test/unit/error.test.js @@ -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') }) diff --git a/test/unit/extract-css.test.js b/test/unit/extract-css.test.js index 62222be8f2..41d837d671 100644 --- a/test/unit/extract-css.test.js +++ b/test/unit/extract-css.test.js @@ -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') }) diff --git a/test/unit/fallback.test.js b/test/unit/fallback.test.js index 983eb6d353..d2f96eb94a 100644 --- a/test/unit/fallback.test.js +++ b/test/unit/fallback.test.js @@ -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') }) diff --git a/test/unit/filenames-query-part.test.js b/test/unit/filenames-query-part.test.js index c99eb19050..4357654c6b 100644 --- a/test/unit/filenames-query-part.test.js +++ b/test/unit/filenames-query-part.test.js @@ -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') }) diff --git a/test/unit/generator.test.js b/test/unit/generator.test.js index 509ad46a59..9fec38c100 100644 --- a/test/unit/generator.test.js +++ b/test/unit/generator.test.js @@ -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) diff --git a/test/unit/https.test.js b/test/unit/https.test.js index 9b31d967b0..00e5febba3 100644 --- a/test/unit/https.test.js +++ b/test/unit/https.test.js @@ -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') }) diff --git a/test/unit/meta-attrs.test.js b/test/unit/meta-attrs.test.js index da98008411..8c4d830bec 100644 --- a/test/unit/meta-attrs.test.js +++ b/test/unit/meta-attrs.test.js @@ -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') }) diff --git a/test/unit/modern.client.test.js b/test/unit/modern.client.test.js index 4f2ca88264..ba2d322b4c 100644 --- a/test/unit/modern.client.test.js +++ b/test/unit/modern.client.test.js @@ -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') }) diff --git a/test/unit/modern.server.test.js b/test/unit/modern.server.test.js index 24b00068d8..cca9804b5a 100644 --- a/test/unit/modern.server.test.js +++ b/test/unit/modern.server.test.js @@ -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') }) diff --git a/test/unit/modern.spa.test.js b/test/unit/modern.spa.test.js index 1b5d86b570..5f88bb3a23 100644 --- a/test/unit/modern.spa.test.js +++ b/test/unit/modern.spa.test.js @@ -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') }) diff --git a/test/unit/module.test.js b/test/unit/module.test.js index 013a6f70ce..bf866c0a4c 100644 --- a/test/unit/module.test.js +++ b/test/unit/module.test.js @@ -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') }) diff --git a/test/unit/named-views.test.js b/test/unit/named-views.test.js index ca47db8c59..dd82032167 100644 --- a/test/unit/named-views.test.js +++ b/test/unit/named-views.test.js @@ -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') }) diff --git a/test/unit/nuxt.test.js b/test/unit/nuxt.test.js index c084dc8346..51f5e19dd6 100644 --- a/test/unit/nuxt.test.js +++ b/test/unit/nuxt.test.js @@ -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') }) diff --git a/test/unit/server.listen.test.js b/test/unit/server.listen.test.js index 2b25b08b34..83bf0a02a1 100644 --- a/test/unit/server.listen.test.js +++ b/test/unit/server.listen.test.js @@ -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}` diff --git a/test/unit/sockets.test.js b/test/unit/sockets.test.js index 607a349c7a..82cb3b7453 100644 --- a/test/unit/sockets.test.js +++ b/test/unit/sockets.test.js @@ -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('/') diff --git a/test/unit/spa.test.js b/test/unit/spa.test.js index 03a316e705..023458e42a 100644 --- a/test/unit/spa.test.js +++ b/test/unit/spa.test.js @@ -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') }) diff --git a/test/unit/ssr.test.js b/test/unit/ssr.test.js index b328a22e46..d0479fc7c9 100644 --- a/test/unit/ssr.test.js +++ b/test/unit/ssr.test.js @@ -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') }) diff --git a/test/unit/typescript.test.js b/test/unit/typescript.test.js index 7d015f55ee..efd412eb67 100644 --- a/test/unit/typescript.test.js +++ b/test/unit/typescript.test.js @@ -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') }) diff --git a/test/unit/with-config.test.js b/test/unit/with-config.test.js index dd34c79a32..17bf5d6ea5 100644 --- a/test/unit/with-config.test.js +++ b/test/unit/with-config.test.js @@ -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/')) })