From 16ff791397fb24d75aeec7d3a3ef74079b4fe1c4 Mon Sep 17 00:00:00 2001 From: abernh Date: Tue, 9 Mar 2021 14:53:45 +0100 Subject: [PATCH] test (nuxt): add basic test for static generated content also extend all route-tests to count number of occurrences for no-content meta tags, e.g. `charset` rel: https://github.com/TiagoDanin/Nuxt-SEO/issues/96 --- .gitignore | 1 + example/nuxt.config.static.js | 13 +++++++++ test/nuxt.js | 52 +++++++++++++++++++++++++++++------ 3 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 example/nuxt.config.static.js diff --git a/.gitignore b/.gitignore index 1e21d19..119494a 100644 --- a/.gitignore +++ b/.gitignore @@ -62,4 +62,5 @@ typings/ # Nuxt example/.nuxt +example/.nuxtStatic dist/ diff --git a/example/nuxt.config.static.js b/example/nuxt.config.static.js new file mode 100644 index 0000000..6d544bd --- /dev/null +++ b/example/nuxt.config.static.js @@ -0,0 +1,13 @@ +const path = require('path') +const baseConfig = require('./nuxt.config') + +const buildDir = path.resolve(__dirname, '.nuxtStatic') + +module.exports = { + ...baseConfig, + generate: { + dir: `${buildDir}/_static` + }, + buildDir, + target: 'static' +} diff --git a/test/nuxt.js b/test/nuxt.js index b4a589e..bc23f1f 100644 --- a/test/nuxt.js +++ b/test/nuxt.js @@ -1,7 +1,9 @@ const test = require('ava') -const {Nuxt, Builder} = require('nuxt') +const {Nuxt, Builder, Generator} = require('nuxt') +const fsPromises = require('fs').promises const got = require('got') const config = require('../example/nuxt.config') +const configStatic = require('../example/nuxt.config.static') const url = path => `http://localhost:4000${path}` const get = async path => { @@ -9,38 +11,72 @@ const get = async path => { return body } +const staticPath = path => `${configStatic.generate.dir}${path}index.html` +const getStatic = async path => { + return fsPromises.readFile(staticPath(path), {encoding: 'utf8'}) +} + let nuxt = null test.before('Init Nuxt.js', async () => { + // Init SSR config.dev = false config.seo.baseUrl = 'http://localhost:4000' nuxt = new Nuxt(config) await new Builder(nuxt).build() - nuxt.listen(4000, 'localhost') + await nuxt.listen(4000, 'localhost') + + // Build STATIC + configStatic.dev = false + const nuxtStatic = new Nuxt(configStatic) + const builder = await new Builder(nuxtStatic) + const generator = await new Generator(nuxtStatic, builder) + await nuxtStatic.listen(4001, 'localhost') + const {errors} = await generator.generate({build: false}) + if (errors.length > 0) { + throw new Error('Error generating pages, exiting with non-zero code') + } + + await nuxtStatic.close() }) test.after('Closing server', () => { nuxt.close() }) +test('Static route /', async t => { + const html = await getStatic('/') + t.true(html.includes('Home Page')) + t.true(html.match('').length === 1) +}) + +test('Static route /news', async t => { + const html = await getStatic('/news/') + t.true(html.includes('Nuxt is the best')) + t.true(html.match('').length === 1) +}) + test('Route / and render HTML', async t => { const html = await get('/') t.true(html.includes('Home Page')) - t.true(html.includes('')) - t.true(html.includes('')) + t.true(html.match('').length === 1) + t.true(html.includes('')) + t.true(html.includes('')) t.true(html.includes('')) t.true(html.includes('')) t.true(html.includes('')) }) test('Route /news and render HTML', async t => { + await get('/news') + await get('/') const html = await get('/news') t.true(html.includes('Nuxt is the best')) - t.true(html.includes('')) - t.true(html.includes('')) - t.true(html.includes('')) + t.true(html.match('').length === 1) + t.true(html.includes('')) + t.true(html.includes('')) t.true(html.includes('')) t.true(html.includes('')) t.true(html.includes(''))