2017-07-03 18:19:22 +00:00
|
|
|
import { uniq } from 'lodash'
|
2018-10-24 13:53:34 +00:00
|
|
|
import { loadFixture, getPort, Nuxt, rp, sequence, parallel } from '../utils'
|
2017-07-03 18:19:22 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
let port
|
2017-07-03 18:19:22 +00:00
|
|
|
let nuxt = null
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
const range = n => [...Array(n).keys()]
|
2018-11-24 18:49:19 +00:00
|
|
|
const FOOBAR_REGEX = /<Foobar>([\s\S]*)<\/Foobar>/
|
2017-07-03 18:19:22 +00:00
|
|
|
const match = (regex, text) => (regex.exec(text) || [])[1]
|
|
|
|
|
2023-10-16 21:23:38 +00:00
|
|
|
const url = route => 'http://127.0.0.1:' + 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 23:41:14 +00:00
|
|
|
// == 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
|
2020-04-14 09:26:40 +00:00
|
|
|
// We strictly compare <Foobar>{id}</Foobar> section
|
2018-03-18 23:41:14 +00:00
|
|
|
// Because other response parts such as window.__NUXT may be different resulting false positive passes.
|
|
|
|
const uniqueTest = async (url) => {
|
2018-08-08 10:54:05 +00:00
|
|
|
const results = []
|
2018-03-18 23:41:14 +00:00
|
|
|
|
2018-10-24 13:53:34 +00:00
|
|
|
await parallel(range(5), async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute(url)
|
2018-08-08 10:54:05 +00:00
|
|
|
const foobar = match(FOOBAR_REGEX, html)
|
2018-03-18 23:41:14 +00:00
|
|
|
results.push(parseInt(foobar))
|
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
2018-08-08 10:54:05 +00:00
|
|
|
const isUnique = uniq(results).length === results.length
|
2017-12-17 19:30:26 +00:00
|
|
|
|
2018-03-18 23:41:14 +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 23:41:14 +00:00
|
|
|
expect(isUnique).toBe(true)
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
// == 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
|
2018-03-18 23:53:36 +00:00
|
|
|
const stressTest = async (_url, concurrency = 2, steps = 4) => {
|
2018-08-08 10:54:05 +00:00
|
|
|
const statusCodes = {}
|
2018-03-18 23:41:14 +00:00
|
|
|
|
2018-10-24 13:53:34 +00:00
|
|
|
await sequence(range(steps), async () => {
|
|
|
|
await parallel(range(concurrency), async () => {
|
2019-12-02 15:23:56 +00:00
|
|
|
const response = await rp(url(_url))
|
2018-03-18 23:41:14 +00:00
|
|
|
// Status Code
|
2018-08-08 10:54:05 +00:00
|
|
|
const code = response.statusCode
|
2018-03-18 23:41:14 +00:00
|
|
|
if (!statusCodes[code]) {
|
|
|
|
statusCodes[code] = 0
|
|
|
|
}
|
|
|
|
statusCodes[code]++
|
|
|
|
})
|
|
|
|
})
|
2017-07-03 18:19:22 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
expect(statusCodes[200]).toBe(concurrency * steps)
|
|
|
|
}
|
2017-07-03 18:19:22 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
describe('ssr', () => {
|
|
|
|
beforeAll(async () => {
|
2018-08-17 20:25:23 +00:00
|
|
|
const config = await loadFixture('ssr')
|
2018-03-18 23:41:14 +00:00
|
|
|
nuxt = new Nuxt(config)
|
2019-03-08 20:43:23 +00:00
|
|
|
await nuxt.ready()
|
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
port = await getPort()
|
2023-10-16 21:23:38 +00:00
|
|
|
await nuxt.server.listen(port, '127.0.0.1')
|
2018-03-18 23:41:14 +00:00
|
|
|
})
|
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-09-25 08:51:07 +00:00
|
|
|
test('store undefined variable response', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/store'))
|
2018-09-25 08:51:07 +00:00
|
|
|
expect('idUndefined' in window.__NUXT__.state).toBe(true)
|
|
|
|
expect(window.__NUXT__.state.idUndefined).toEqual(undefined)
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
2020-10-13 10:19:08 +00:00
|
|
|
test('does not share state', async () => {
|
|
|
|
const [page1, page2] = await Promise.all([
|
|
|
|
nuxt.server.renderRoute('/context'),
|
|
|
|
nuxt.server.renderRoute('/context/child')
|
|
|
|
])
|
|
|
|
|
|
|
|
expect(page1.html).toContain('vm: /context')
|
|
|
|
expect(page1.html).toContain('context: /context')
|
|
|
|
|
|
|
|
expect(page2.html).toContain('vm: /context/child')
|
|
|
|
expect(page2.html).toContain('context: /context/child')
|
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
2018-03-30 09:20:16 +00:00
|
|
|
afterAll(async () => {
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
|
|
|
})
|
2017-07-03 18:19:22 +00:00
|
|
|
})
|