Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
test (nuxt): add basic test for static generated content
Browse files Browse the repository at this point in the history
also extend all route-tests to count number of occurrences for no-content meta tags, e.g. `charset`

rel: TiagoDanin#96
  • Loading branch information
abernh committed Mar 9, 2021
1 parent f8a0fab commit 16ff791
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ typings/

# Nuxt
example/.nuxt
example/.nuxtStatic
dist/
13 changes: 13 additions & 0 deletions example/nuxt.config.static.js
Original file line number Diff line number Diff line change
@@ -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'
}
52 changes: 44 additions & 8 deletions test/nuxt.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,82 @@
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 => {
const {body} = await got(url(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('<title>Home Page</title>'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
})

test('Static route /news', async t => {
const html = await getStatic('/news/')
t.true(html.includes('<title>Nuxt is the best</title>'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
})

test('Route / and render HTML', async t => {
const html = await get('/')

t.true(html.includes('<title>Home Page</title>'))
t.true(html.includes('<meta data-n-head="ssr" charset="utf-8"'))
t.true(html.includes('<meta data-n-head="ssr" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" language="English">'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
t.true(html.includes('<meta data-n-head="ssr" data-hid="charset" charset="utf-8"'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="lang" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="language" language="English">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="name" key="name" property="name" name="name" content="App name">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="description" key="description" property="description" name="description" content="Example app with Nuxt Seo">'))
t.true(html.includes('<link data-n-head="ssr" rel="canonical" href="http://localhost:4000/">'))
})

test('Route /news and render HTML', async t => {
await get('/news')
await get('/')
const html = await get('/news')
t.true(html.includes('<title>Nuxt is the best</title>'))
t.true(html.includes('<meta data-n-head="ssr" charset="utf-8">'))
t.true(html.includes('<meta data-n-head="ssr" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" language="English">'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
t.true(html.includes('<meta data-n-head="ssr" data-hid="lang" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="language" language="English">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="name" key="name" property="name" name="name" content="App name">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="description" key="description" property="description" name="description" content="Hello World page in blog">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="og:site_name" key="og:site_name" property="og:site_name" name="og:site_name" content="App name">'))
Expand Down

0 comments on commit 16ff791

Please sign in to comment.