Nuxt/test/ssr.test.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-07-03 18:19:22 +00:00
import { Nuxt, Builder, Utils } from '..'
import { uniq } from 'lodash'
2017-08-29 17:37:19 +00:00
import rp from 'request-promise-native'
2018-03-16 16:12:06 +00:00
import { loadConfig } from './helpers/config'
2017-07-03 18:19:22 +00:00
const port = 4008
let nuxt = null
// Utils
const range = n => [...Array(n).keys()]
const FOOBAR_REGEX = /<foobar>([\s\S]*)<\/foobar>/
const match = (regex, text) => (regex.exec(text) || [])[1]
2018-01-13 05:22:11 +00:00
const url = route => 'http://localhost:' + port + route
2017-08-29 17:37:19 +00:00
2018-03-17 10:01:57 +00:00
// const isWindows = /^win/.test(process.platform)
2017-10-07 08:51:28 +00:00
2018-03-18 19:31:32 +00:00
describe('ssr', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => {
const config = loadConfig('ssr')
nuxt = new Nuxt(config)
2018-03-18 19:31:32 +00:00
new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
2018-03-18 19:31:32 +00:00
}, 30000)
// == Uniq Test ==
// The idea behind is pages using a shared nextId() which returns an incrementing id
// So all responses should strictly be different and length of unique responses should equal to responses
// We strictly compare <foorbar>{id}</foorbar> section
// Because other response parts such as window.__NUXT may be different resulting false positive passes.
const uniqueTest = async (t, url) => {
let results = []
await Utils.parallel(range(5), async () => {
let { html } = await nuxt.renderRoute(url)
let foobar = match(FOOBAR_REGEX, html)
results.push(parseInt(foobar))
})
2018-03-18 19:31:32 +00:00
let isUnique = uniq(results).length === results.length
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
if (!isUnique) {
/* eslint-disable no-console */
console.log(url + '\n' + results.join(', ') + '\n')
}
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
expect(isUnique).toBe(true)
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
return results
2017-07-03 18:19:22 +00:00
}
2018-03-18 19:31:32 +00:00
test('unique responses with data()', async () => {
await uniqueTest('/data')
})
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
test('unique responses with component', async () => {
await uniqueTest('/component')
})
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
test('unique responses with async components', async () => {
await uniqueTest('/asyncComponent')
})
2017-07-03 18:29:38 +00:00
2018-03-18 19:31:32 +00:00
test('unique responses with asyncData()', async () => {
await uniqueTest('/asyncData')
})
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
test('unique responses with store initial state', async () => {
await uniqueTest('/store')
})
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
test('unique responses with nuxtServerInit', async () => {
await uniqueTest('/store?onServerInit=1')
})
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
test('unique responses with fetch', async () => {
await uniqueTest('/fetch')
})
2017-07-03 18:19:22 +00:00
2018-03-18 19:31:32 +00:00
// == Stress Test ==
// The idea of this test is to ensure there is no memory or data leak during SSR requests
// Or pending promises/sockets and function calls.
// Related issue: https://github.com/nuxt/nuxt.js/issues/1354
const stressTest = async (t, _url, concurrency = 2, steps = 4) => {
let statusCodes = {}
await Utils.sequence(range(steps), async () => {
await Utils.parallel(range(concurrency), async () => {
let response = await rp(url(_url), { resolveWithFullResponse: true })
// Status Code
let code = response.statusCode
if (!statusCodes[code]) {
statusCodes[code] = 0
}
statusCodes[code]++
})
2017-08-29 17:37:19 +00:00
})
2018-03-18 19:31:32 +00:00
expect(statusCodes[200]).toBe(concurrency * steps)
}
2017-08-29 17:37:19 +00:00
2018-03-18 19:31:32 +00:00
test('stress test with asyncData', async () => {
await stressTest('/asyncData')
})
2017-08-29 17:37:19 +00:00
2018-03-18 19:31:32 +00:00
// Close server and ask nuxt to stop listening to file changes
test('Closing server and nuxt.js', async () => {
await nuxt.close()
})
2017-07-03 18:19:22 +00:00
})