From fffe7419869ac2b0256d288864646cc2c9716b93 Mon Sep 17 00:00:00 2001 From: pimlie Date: Sun, 17 Dec 2017 20:30:26 +0100 Subject: [PATCH 1/2] Use console helper with sinon Add stdout/stderr to console helpers Remove separate nosubfolders test file in favor of inclusion in basic.generate Add build.stats to silence webpack output Add .always to after hooks: (1) fixes issue with basic.dev test that watch.js is empty when one of the test fails and (2) fixes that sometimes when running multiple tests that failed after eachother the port is still used as the server did not automatically get killed Change all init nuxt before test to serial tests and add tests for DONE and OPEN logs --- .gitignore | 4 +- package.json | 2 +- test/basic.csr.test.js | 73 +++++++----- test/basic.dev.test.js | 38 ++++--- test/basic.fail.generate.test.js | 24 ++-- test/basic.generate.nosubfolders.test.js | 138 ----------------------- test/basic.generate.test.js | 98 ++++++++++------ test/basic.ssr.test.js | 102 ++++++++--------- test/children.patch.test.js | 56 +++++---- test/children.test.js | 25 ++-- test/cli.test.js | 6 +- test/debug.test.js | 52 +++++++-- test/deprecate.test.js | 40 +++---- test/dll.test.js | 14 ++- test/dynamic-routes.test.js | 17 ++- test/error.test.js | 46 +++++--- test/express.test.js | 21 ++-- test/fixtures/debug/nuxt.config.js | 4 + test/fixtures/deprecate/nuxt.config.js | 1 + test/fixtures/dll/nuxt.config.js | 1 + test/fixtures/module/nuxt.config.js | 3 + test/fixtures/spa/nuxt.config.js | 6 +- test/fixtures/ssr/nuxt.config.js | 10 ++ test/fixtures/with-config/nuxt.config.js | 1 + test/helpers/console.js | 36 +++++- test/index.test.js | 6 +- test/module.test.js | 44 ++++---- test/spa.test.js | 44 ++++++-- test/ssr.test.js | 32 +++--- test/with-config.test.js | 55 ++++----- 30 files changed, 543 insertions(+), 456 deletions(-) delete mode 100644 test/basic.generate.nosubfolders.test.js create mode 100644 test/fixtures/ssr/nuxt.config.js diff --git a/.gitignore b/.gitignore index 6b2d6f7e1d..0d87892d25 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ package-lock.json npm-debug.log* # Other -.nuxt +.nuxt* .cache # Dist folder @@ -47,4 +47,4 @@ coverage .AppleDesktop Network Trash Folder Temporary Items -.apdisk \ No newline at end of file +.apdisk diff --git a/package.json b/package.json index e87683bb05..55019696cd 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ ] }, "scripts": { - "test": "npm run lint && nyc ava --verbose --serial test/ -- && nyc report --reporter=html", + "test": "npm run lint && nyc ava --verbose test/ -- && nyc report --reporter=html", "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", "lint": "eslint --ext .js,.vue bin/* build/ lib/ test/ examples/", "precommit": "npm run lint", diff --git a/test/basic.csr.test.js b/test/basic.csr.test.js index 94fada4228..b8108dae16 100644 --- a/test/basic.csr.test.js +++ b/test/basic.csr.test.js @@ -2,6 +2,7 @@ import test from 'ava' import { resolve } from 'path' import { Nuxt, Builder } from '..' import * as browser from './helpers/browser' +import { interceptLog } from './helpers/console' const port = 4003 const url = (route) => 'http://localhost:' + port + route @@ -10,36 +11,46 @@ let nuxt = null let page = null // Init nuxt.js and create server listening on localhost:4003 -test.before('Init Nuxt.js', async t => { +test.serial('Init Nuxt.js', async t => { const options = { rootDir: resolve(__dirname, 'fixtures/basic'), - dev: false, + buildDir: '.nuxt-csr', + dev: true, head: { titleTemplate(titleChunk) { return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js' } + }, + build: { + stats: false } } - nuxt = new Nuxt(options) - await new Builder(nuxt).build() - await nuxt.listen(port, 'localhost') + const logSpy = await interceptLog(async () => { + nuxt = new Nuxt(options) + await new Builder(nuxt).build() + await nuxt.listen(port, 'localhost') + }) + + t.true(logSpy.calledWithMatch('DONE')) + t.true(logSpy.calledWithMatch('OPEN')) }) -test.before('Start browser', async t => { +test.serial('Start browser', async t => { + t.plan(0) // suppress 'no assertions' warning await browser.start({ // slowMo: 50, // headless: false }) }) -test('Open /', async t => { +test.serial('Open /', async t => { page = await browser.page(url('/')) t.is(await page.$text('h1'), 'Index page') }) -test('/stateless', async t => { +test.serial('/stateless', async t => { const { hook } = await page.nuxt.navigate('/stateless', false) const loading = await page.nuxt.loadingData() @@ -48,27 +59,27 @@ test('/stateless', async t => { t.is(await page.$text('h1'), 'My component!') }) -test('/css', async t => { +test.serial('/css', async t => { await page.nuxt.navigate('/css') t.is(await page.$text('.red'), 'This is red') t.is(await page.$eval('.red', (red) => window.getComputedStyle(red).color), 'rgb(255, 0, 0)') }) -test('/stateful', async t => { +test.serial('/stateful', async t => { await page.nuxt.navigate('/stateful') t.is(await page.$text('p'), 'The answer is 42') }) -test('/store', async t => { +test.serial('/store', async t => { await page.nuxt.navigate('/store') t.is(await page.$text('h1'), 'Vuex Nested Modules') t.is(await page.$text('p'), '1') }) -test('/head', async t => { +test.serial('/head', async t => { const msg = new Promise((resolve) => page.on('console', (msg) => resolve(msg.text))) await page.nuxt.navigate('/head') const metas = await page.$$attr('meta', 'content') @@ -79,31 +90,31 @@ test('/head', async t => { t.is(metas[0], 'my meta') }) -test('/async-data', async t => { +test.serial('/async-data', async t => { await page.nuxt.navigate('/async-data') t.is(await page.$text('p'), 'Nuxt.js') }) -test('/await-async-data', async t => { +test.serial('/await-async-data', async t => { await page.nuxt.navigate('/await-async-data') t.is(await page.$text('p'), 'Await Nuxt.js') }) -test('/callback-async-data', async t => { +test.serial('/callback-async-data', async t => { await page.nuxt.navigate('/callback-async-data') t.is(await page.$text('p'), 'Callback Nuxt.js') }) -test('/users/1', async t => { +test.serial('/users/1', async t => { await page.nuxt.navigate('/users/1') t.is(await page.$text('h1'), 'User: 1') }) -test('/validate should display a 404', async t => { +test.serial('/validate should display a 404', async t => { await page.nuxt.navigate('/validate') const error = await page.nuxt.errorData() @@ -111,39 +122,39 @@ test('/validate should display a 404', async t => { t.is(error.message, 'This page could not be found') }) -test('/validate?valid=true', async t => { +test.serial('/validate?valid=true', async t => { await page.nuxt.navigate('/validate?valid=true') t.is(await page.$text('h1'), 'I am valid') }) -test('/redirect', async t => { +test.serial('/redirect', async t => { await page.nuxt.navigate('/redirect') t.is(await page.$text('h1'), 'Index page') }) -test('/error', async t => { +test.serial('/error', async t => { await page.nuxt.navigate('/error') t.deepEqual(await page.nuxt.errorData(), { statusCode: 500 }) t.is(await page.$text('.title'), 'Error mouahahah') }) -test('/error2', async t => { +test.serial('/error2', async t => { await page.nuxt.navigate('/error2') t.is(await page.$text('.title'), 'Custom error') t.deepEqual(await page.nuxt.errorData(), { message: 'Custom error' }) }) -test('/redirect2', async t => { +test.serial('/redirect2', async t => { await page.nuxt.navigate('/redirect2') t.is(await page.$text('h1'), 'Index page') }) -test('/redirect3', async t => { +test.serial('/redirect3', async t => { // New page for redirecting to external link. const page = await browser.page(url('/')) await page.nuxt.navigate('/redirect3', false) @@ -152,44 +163,44 @@ test('/redirect3', async t => { t.pass() }) -test('/no-ssr', async t => { +test.serial('/no-ssr', async t => { await page.nuxt.navigate('/no-ssr') t.is(await page.$text('h1'), 'Displayed only on client-side') }) -test('/meta', async t => { +test.serial('/meta', async t => { await page.nuxt.navigate('/meta') const state = await page.nuxt.storeState() t.deepEqual(state.meta, [{ works: true }]) }) -test('/fn-midd', async t => { +test.serial('/fn-midd', async t => { await page.nuxt.navigate('/fn-midd') t.is(await page.$text('.title'), 'You need to ask the permission') t.deepEqual(await page.nuxt.errorData(), { message: 'You need to ask the permission', statusCode: 403 }) }) -test('/fn-midd?please=true', async t => { +test.serial('/fn-midd?please=true', async t => { await page.nuxt.navigate('/fn-midd?please=true') const h1 = await page.$text('h1') t.true(h1.includes('Date:')) }) -test('/router-guard', async t => { +test.serial('/router-guard', async t => { await page.nuxt.navigate('/router-guard') t.is(await page.$text('p'), 'Nuxt.js') }) // Close server and ask nuxt to stop listening to file changes -test.after('Closing server and nuxt.js', t => { - nuxt.close() +test.after.always('Closing server and nuxt.js', async t => { + await nuxt.close() }) -test.after('Stop browser', async t => { +test.after.always('Stop browser', async t => { await browser.stop() }) diff --git a/test/basic.dev.test.js b/test/basic.dev.test.js index af13547414..efdf90e3f4 100644 --- a/test/basic.dev.test.js +++ b/test/basic.dev.test.js @@ -1,6 +1,6 @@ import test from 'ava' import { resolve } from 'path' -import { interceptLog, release } from './helpers/console' +import { intercept, release } from './helpers/console' import { Nuxt, Builder, Utils } from '..' import { truncateSync, readFileSync, writeFileSync } from 'fs' @@ -13,32 +13,39 @@ const pluginContent = readFileSync(pluginPath) let nuxt = null // Init nuxt.js and create server listening on localhost:4000 -test.before('Init Nuxt.js', async t => { +test.serial('Init Nuxt.js', async t => { const options = { rootDir, + buildDir: '.nuxt-dev', dev: true, build: { + stats: false, profile: true }, plugins: [ '~/plugins/watch.js' ] } - nuxt = new Nuxt(options) - await new Builder(nuxt).build() - await nuxt.listen(port, 'localhost') + const spies = await intercept({ log: true, stderr: true }, async () => { + nuxt = new Nuxt(options) + await new Builder(nuxt).build() + await nuxt.listen(port, 'localhost') + }) + + t.true(spies.log.calledWithMatch('DONE')) + t.true(spies.log.calledWithMatch('OPEN')) }) -test('remove mixins in live reloading', async t => { - const logSpy = await interceptLog() +test.serial('remove mixins in live reloading', async t => { + const spies = await intercept() await nuxt.renderRoute(url('/')) - t.true(logSpy.calledWith('I am mixin')) + t.true(spies.log.calledWith('I am mixin')) truncateSync(pluginPath) await new Promise(async (resolve, reject) => { let waitTimes = 0 - while (logSpy.neverCalledWithMatch(/Compiled successfully/)) { + while (spies.log.neverCalledWithMatch(/Compiled successfully/)) { if (waitTimes++ >= 20) { t.fail('Dev server doesn\'t reload after 2000ms') reject(Error()) @@ -47,16 +54,21 @@ test('remove mixins in live reloading', async t => { } resolve() }) - logSpy.reset() + spies.log.reset() + await nuxt.renderRoute(url('/')) - t.true(logSpy.neverCalledWith('I am mixin')) + t.true(spies.log.neverCalledWith('I am mixin')) + t.is(spies.error.getCall(0).args[0].statusCode, 404) release() }) -test('/stateless', async t => { +test.serial('/stateless', async t => { + const spies = await intercept() const window = await nuxt.renderAndGetWindow(url('/stateless')) const html = window.document.body.innerHTML t.true(html.includes('

My component!

')) + t.true(spies.info.calledWithMatch('You are running Vue in development mode.')) + release() }) // test('/_nuxt/test.hot-update.json should returns empty html', async t => { @@ -69,7 +81,7 @@ test('/stateless', async t => { // }) // Close server and ask nuxt to stop listening to file changes -test.after('Closing server and nuxt.js', async t => { +test.after.always('Closing server and nuxt.js', async t => { writeFileSync(pluginPath, pluginContent) await nuxt.close() }) diff --git a/test/basic.fail.generate.test.js b/test/basic.fail.generate.test.js index 79653d3593..65e8874e75 100644 --- a/test/basic.fail.generate.test.js +++ b/test/basic.fail.generate.test.js @@ -1,22 +1,32 @@ import test from 'ava' import { resolve } from 'path' import { Nuxt, Builder, Generator } from '..' +import { intercept } from './helpers/console' test('Fail with routes() which throw an error', async t => { const options = { rootDir: resolve(__dirname, 'fixtures/basic'), + buildDir: '.nuxt-fail', dev: false, + build: { + stats: false + }, generate: { async routes() { throw new Error('Not today!') } } } - const nuxt = new Nuxt(options) - const builder = new Builder(nuxt) - const generator = new Generator(nuxt, builder) - return generator.generate() - .catch((e) => { - t.true(e.message === 'Not today!') - }) + const spies = await intercept(async () => { + const nuxt = new Nuxt(options) + const builder = new Builder(nuxt) + const generator = new Generator(nuxt, builder) + + return generator.generate() + .catch((e) => { + t.true(e.message === 'Not today!') + }) + }) + t.true(spies.log.calledWithMatch('DONE')) + t.true(spies.error.withArgs('Could not resolve routes').calledOnce) }) diff --git a/test/basic.generate.nosubfolders.test.js b/test/basic.generate.nosubfolders.test.js deleted file mode 100644 index 5aeb01a16f..0000000000 --- a/test/basic.generate.nosubfolders.test.js +++ /dev/null @@ -1,138 +0,0 @@ -import test from 'ava' -import { resolve } from 'path' -import { existsSync } from 'fs' -import http from 'http' -import serveStatic from 'serve-static' -import finalhandler from 'finalhandler' -import rp from 'request-promise-native' -import { Nuxt, Builder, Generator } from '..' - -const port = 4002 -const url = (route) => 'http://localhost:' + port + route - -let nuxt = null -let server = null - -// Init nuxt.js and create server listening on localhost:4000 -test.before('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/basic') - let config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.dev = false - config.generate.subFolders = false - - nuxt = new Nuxt(config) - const builder = new Builder(nuxt) - const generator = new Generator(nuxt, builder) - try { - await generator.generate() // throw an error (of /validate route) - } catch (err) { - } - - const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'), { extensions: ['html'] }) - server = http.createServer((req, res) => { - serve(req, res, finalhandler(req, res)) - }) - server.listen(port) -}) - -test('Check ready hook called', async t => { - t.true(nuxt.__hook_called__) -}) - -test('/stateless', async t => { - const window = await nuxt.renderAndGetWindow(url('/stateless')) - const html = window.document.body.innerHTML - t.true(html.includes('

My component!

')) -}) - -test('/css', async t => { - const window = await nuxt.renderAndGetWindow(url('/css')) - const element = window.document.querySelector('.red') - t.not(element, null) - t.is(element.textContent, 'This is red') - t.is(element.className, 'red') - t.is(window.getComputedStyle(element).color, 'red') -}) - -test('/stateful', async t => { - const window = await nuxt.renderAndGetWindow(url('/stateful')) - const html = window.document.body.innerHTML - t.true(html.includes('

The answer is 42

')) -}) - -test('/head', async t => { - const window = await nuxt.renderAndGetWindow(url('/head')) - const html = window.document.body.innerHTML - const metas = window.document.getElementsByTagName('meta') - t.is(window.document.title, 'My title') - t.is(metas[0].getAttribute('content'), 'my meta') - t.true(html.includes('

I can haz meta tags

')) -}) - -test('/async-data', async t => { - const window = await nuxt.renderAndGetWindow(url('/async-data')) - const html = window.document.body.innerHTML - t.true(html.includes('

Nuxt.js

')) -}) - -test('/users/1', async t => { - const html = await rp(url('/users/1')) - t.true(html.includes('

User: 1

')) - t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html'))) - t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))) -}) - -test('/users/2', async t => { - const html = await rp(url('/users/2')) - t.true(html.includes('

User: 2

')) -}) - -test('/users/3 (payload given)', async t => { - const html = await rp(url('/users/3')) - t.true(html.includes('

User: 3000

')) -}) - -test('/users/4 -> Not found', async t => { - try { - await rp(url('/users/4')) - } catch (error) { - t.true(error.statusCode === 404) - t.true(error.response.body.includes('Cannot GET /users/4')) - } -}) - -test('/validate should not be server-rendered', async t => { - const html = await rp(url('/validate')) - t.true(html.includes('
')) - t.true(html.includes('serverRendered:!1')) -}) - -test('/validate -> should display a 404', async t => { - const window = await nuxt.renderAndGetWindow(url('/validate')) - const html = window.document.body.innerHTML - t.true(html.includes('This page could not be found')) -}) - -test('/validate?valid=true', async t => { - const window = await nuxt.renderAndGetWindow(url('/validate?valid=true')) - const html = window.document.body.innerHTML - t.true(html.includes('I am valid')) -}) - -test('/redirect should not be server-rendered', async t => { - const html = await rp(url('/redirect')) - t.true(html.includes('
')) - t.true(html.includes('serverRendered:!1')) -}) - -test('/redirect -> check redirected source', async t => { - const window = await nuxt.renderAndGetWindow(url('/redirect')) - const html = window.document.body.innerHTML - t.true(html.includes('

Index page

')) -}) - -// Close server and ask nuxt to stop listening to file changes -test.after('Closing server', t => { - server.close() -}) diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index 877cd1c52a..27f169b492 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -1,31 +1,39 @@ import test from 'ava' import { resolve } from 'path' import { existsSync } from 'fs' +import { remove } from 'fs-extra' import http from 'http' import serveStatic from 'serve-static' import finalhandler from 'finalhandler' import rp from 'request-promise-native' +import { interceptLog, release } from './helpers/console' import { Nuxt, Builder, Generator } from '..' const port = 4002 const url = (route) => 'http://localhost:' + port + route +const rootDir = resolve(__dirname, 'fixtures/basic') let nuxt = null let server = null +let generator = null // Init nuxt.js and create server listening on localhost:4000 -test.before('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/basic') +test.serial('Init Nuxt.js', async t => { let config = require(resolve(rootDir, 'nuxt.config.js')) config.rootDir = rootDir + config.buildDir = '.nuxt-generate' config.dev = false - nuxt = new Nuxt(config) - const builder = new Builder(nuxt) - const generator = new Generator(nuxt, builder) - try { - await generator.generate() // throw an error (of /validate route) - } catch (err) { - } + config.build.stats = false + + const logSpy = await interceptLog(async () => { + nuxt = new Nuxt(config) + const builder = new Builder(nuxt) + generator = new Generator(nuxt, builder) + + await generator.generate() + }) + t.true(logSpy.calledWithMatch('DONE')) + const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist')) server = http.createServer((req, res) => { serve(req, res, finalhandler(req, res)) @@ -33,17 +41,17 @@ test.before('Init Nuxt.js', async t => { server.listen(port) }) -test('Check ready hook called', async t => { +test.serial('Check ready hook called', async t => { t.true(nuxt.__hook_called__) }) -test('/stateless', async t => { +test.serial('/stateless', async t => { const window = await nuxt.renderAndGetWindow(url('/stateless')) const html = window.document.body.innerHTML t.true(html.includes('

My component!

')) }) -test('/css', async t => { +test.serial('/css', async t => { const window = await nuxt.renderAndGetWindow(url('/css')) const element = window.document.querySelector('.red') t.not(element, null) @@ -52,84 +60,106 @@ test('/css', async t => { t.is(window.getComputedStyle(element).color, 'red') }) -test('/stateful', async t => { +test.serial('/stateful', async t => { const window = await nuxt.renderAndGetWindow(url('/stateful')) const html = window.document.body.innerHTML t.true(html.includes('

The answer is 42

')) }) -test('/head', async t => { +test.serial('/head', async t => { + const logSpy = await interceptLog() const window = await nuxt.renderAndGetWindow(url('/head')) const html = window.document.body.innerHTML const metas = window.document.getElementsByTagName('meta') t.is(window.document.title, 'My title') t.is(metas[0].getAttribute('content'), 'my meta') t.true(html.includes('

I can haz meta tags

')) + release() + t.is(logSpy.getCall(0).args[0], 'Body script!') }) -test('/async-data', async t => { +test.serial('/async-data', async t => { const window = await nuxt.renderAndGetWindow(url('/async-data')) const html = window.document.body.innerHTML t.true(html.includes('

Nuxt.js

')) }) -test('/users/1', async t => { - const html = await rp(url('/users/1')) +test.serial('/users/1/index.html', async t => { + const html = await rp(url('/users/1/index.html')) t.true(html.includes('

User: 1

')) t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))) t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html'))) }) -test('/users/2', async t => { +test.serial('/users/2', async t => { const html = await rp(url('/users/2')) t.true(html.includes('

User: 2

')) }) -test('/users/3 (payload given)', async t => { +test.serial('/users/3 (payload given)', async t => { const html = await rp(url('/users/3')) t.true(html.includes('

User: 3000

')) }) -test('/users/4 -> Not found', async t => { - try { - await rp(url('/users/4')) - } catch (error) { - t.true(error.statusCode === 404) - t.true(error.response.body.includes('Cannot GET /users/4')) - } +test.serial('/users/4 -> Not found', async t => { + const error = await t.throws(rp(url('/users/4'))) + t.true(error.statusCode === 404) + t.true(error.response.body.includes('Cannot GET /users/4')) }) -test('/validate should not be server-rendered', async t => { +test.serial('/validate should not be server-rendered', async t => { const html = await rp(url('/validate')) t.true(html.includes('
')) t.true(html.includes('serverRendered:!1')) }) -test('/validate -> should display a 404', async t => { +test.serial('/validate -> should display a 404', async t => { const window = await nuxt.renderAndGetWindow(url('/validate')) const html = window.document.body.innerHTML t.true(html.includes('This page could not be found')) }) -test('/validate?valid=true', async t => { +test.serial('/validate?valid=true', async t => { const window = await nuxt.renderAndGetWindow(url('/validate?valid=true')) const html = window.document.body.innerHTML t.true(html.includes('I am valid')) }) -test('/redirect should not be server-rendered', async t => { +test.serial('/redirect should not be server-rendered', async t => { const html = await rp(url('/redirect')) t.true(html.includes('
')) t.true(html.includes('serverRendered:!1')) }) -test('/redirect -> check redirected source', async t => { +test.serial('/redirect -> check redirected source', async t => { const window = await nuxt.renderAndGetWindow(url('/redirect')) const html = window.document.body.innerHTML t.true(html.includes('

Index page

')) }) -// Close server and ask nuxt to stop listening to file changes -test.after('Closing server', t => { - server.close() +test.serial('/users/1 not found', async t => { + await remove(resolve(rootDir, 'dist/users')) + const error = await t.throws(rp(url('/users/1'))) + t.true(error.statusCode === 404) + t.true(error.response.body.includes('Cannot GET /users/1')) +}) + +test.serial('nuxt re-generating with no subfolders', async t => { + const logSpy = await interceptLog() + nuxt.options.generate.subFolders = false + await generator.generate() + release() + t.true(logSpy.calledWithMatch('DONE')) +}) + +test.serial('/users/1.html', async t => { + const html = await rp(url('/users/1.html')) + t.true(html.includes('

User: 1

')) + t.true(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html'))) + t.false(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))) +}) + +// Close server and ask nuxt to stop listening to file changes +test.after.always('Closing server', async t => { + await server.close() }) diff --git a/test/basic.ssr.test.js b/test/basic.ssr.test.js index 0e5a98b237..d023acaa8b 100755 --- a/test/basic.ssr.test.js +++ b/test/basic.ssr.test.js @@ -2,30 +2,38 @@ import test from 'ava' import { resolve } from 'path' import rp from 'request-promise-native' import { Nuxt, Builder } from '..' -import { interceptLog, interceptError } from './helpers/console' +import { interceptLog, interceptError, release } from './helpers/console' -const port = 4003 +const port = 4004 const url = (route) => 'http://localhost:' + port + route let nuxt = null // Init nuxt.js and create server listening on localhost:4003 -test.before('Init Nuxt.js', async t => { +test.serial('Init Nuxt.js', async t => { const options = { rootDir: resolve(__dirname, 'fixtures/basic'), + buildDir: '.nuxt-ssr', dev: false, head: { titleTemplate(titleChunk) { return titleChunk ? `${titleChunk} - Nuxt.js` : 'Nuxt.js' } + }, + build: { + stats: false } } - await interceptLog('building nuxt', async () => { + const logSpy = await interceptLog(async () => { nuxt = new Nuxt(options) - await new Builder(nuxt).build() - await nuxt.listen(port, 'localhost') + const builder = await new Builder(nuxt) + await builder.build() + await nuxt.listen(port, '0.0.0.0') }) + + t.true(logSpy.calledWithMatch('DONE')) + t.true(logSpy.calledWithMatch('OPEN')) }) test('/stateless', async t => { @@ -62,18 +70,19 @@ test('/store', async t => { t.true(html.includes('

1

')) }) -test('/head', async t => { - const logSpy = await interceptLog(async () => { - const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false }) - t.is(window.document.title, 'My title - Nuxt.js') +test.serial('/head', async t => { + const logSpy = await interceptLog() + const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false }) + t.is(window.document.title, 'My title - Nuxt.js') - const html = window.document.body.innerHTML - t.true(html.includes('

I can haz meta tags

')) - t.true(html.includes('