2018-01-27 00:20:03 +00:00
|
|
|
import http from 'http'
|
2018-03-16 19:52:17 +00:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
import { resolve } from 'path'
|
2018-01-27 00:20:03 +00:00
|
|
|
import serveStatic from 'serve-static'
|
|
|
|
import finalhandler from 'finalhandler'
|
2018-03-27 22:28:17 +00:00
|
|
|
import { loadFixture, getPort, Nuxt, Generator, Options, rp } from '../utils'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
let port
|
2018-01-27 00:20:03 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2018-03-19 10:06:45 +00:00
|
|
|
const distDir = resolve(__dirname, '..', 'fixtures/basic/.nuxt-generate-fallback')
|
2018-01-27 00:20:03 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
|
|
|
let server = null
|
|
|
|
let generator = null
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
describe('fallback generate', () => {
|
|
|
|
beforeAll(async () => {
|
2018-08-17 20:25:23 +00:00
|
|
|
const config = await loadFixture('basic', {generate: {dir: '.nuxt-generate-fallback'}})
|
2018-01-27 00:20:03 +00:00
|
|
|
|
|
|
|
nuxt = new Nuxt(config)
|
2018-03-18 23:41:14 +00:00
|
|
|
generator = new Generator(nuxt)
|
2018-01-27 00:20:03 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
await generator.generate({ build: false })
|
2018-01-27 00:20:03 +00:00
|
|
|
|
2018-03-19 04:21:04 +00:00
|
|
|
const serve = serveStatic(distDir)
|
2018-03-18 19:31:32 +00:00
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
serve(req, res, finalhandler(req, res))
|
|
|
|
})
|
2018-03-18 23:41:14 +00:00
|
|
|
|
|
|
|
port = await getPort()
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
server.listen(port)
|
2018-03-18 23:41:14 +00:00
|
|
|
})
|
2018-03-18 19:31:32 +00:00
|
|
|
|
|
|
|
test('default creates /200.html as fallback', async () => {
|
|
|
|
const html = await rp(url('/200.html'))
|
|
|
|
expect(html.includes('<h1>Index page</h1>')).toBe(false)
|
|
|
|
expect(html.includes('data-server-rendered')).toBe(false)
|
2018-03-19 04:21:04 +00:00
|
|
|
expect(existsSync(resolve(distDir, '200.html'))).toBe(true)
|
|
|
|
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
|
2018-01-27 00:20:03 +00:00
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('nuxt re-generating with generate.fallback = false', async () => {
|
2018-01-27 00:20:03 +00:00
|
|
|
nuxt.options.generate.fallback = false
|
2018-04-18 15:56:03 +00:00
|
|
|
await expect(generator.generate({ build: false })).resolves.toBeTruthy()
|
2018-01-27 00:20:03 +00:00
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('false creates no fallback', async () => {
|
|
|
|
await expect(rp(url('/200.html'))).rejects.toMatchObject({
|
|
|
|
statusCode: 404,
|
|
|
|
response: {
|
|
|
|
body: expect.stringContaining('Cannot GET /200.html')
|
|
|
|
}
|
|
|
|
})
|
2018-01-27 00:20:03 +00:00
|
|
|
|
2018-03-19 04:21:04 +00:00
|
|
|
expect(existsSync(resolve(distDir, '200.html'))).toBe(false)
|
|
|
|
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2018-01-27 00:20:03 +00:00
|
|
|
|
2018-08-23 17:33:02 +00:00
|
|
|
test('nuxt re-generating with generate.fallback = \'\'', async () => {
|
|
|
|
nuxt.options.generate.fallback = ''
|
|
|
|
await expect(generator.generate({ build: false })).resolves.toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('empty string creates no fallback', async () => {
|
|
|
|
await expect(rp(url('/200.html'))).rejects.toMatchObject({
|
|
|
|
statusCode: 404,
|
|
|
|
response: {
|
|
|
|
body: expect.stringContaining('Cannot GET /200.html')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(existsSync(resolve(distDir, '200.html'))).toBe(false)
|
|
|
|
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
|
|
|
|
})
|
|
|
|
|
2018-08-10 07:41:23 +00:00
|
|
|
test('generate.fallback = true is transformed to /404.html', () => {
|
2018-03-18 19:31:32 +00:00
|
|
|
nuxt.options.generate.fallback = true
|
|
|
|
const options = Options.from(nuxt.options)
|
|
|
|
expect(options.generate.fallback).toBe('404.html')
|
2018-01-27 00:20:03 +00:00
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test(
|
|
|
|
'nuxt re-generating with generate.fallback = "spa-fallback.html"',
|
|
|
|
async () => {
|
|
|
|
nuxt.options.generate.fallback = 'spa-fallback.html'
|
2018-04-18 15:56:03 +00:00
|
|
|
await expect(generator.generate({ build: false })).resolves.toBeTruthy()
|
2018-03-18 19:31:32 +00:00
|
|
|
}
|
2018-01-27 00:20:03 +00:00
|
|
|
)
|
|
|
|
|
2018-08-23 17:33:02 +00:00
|
|
|
test('spa-fallback.html was created', async () => {
|
|
|
|
const html = await rp(url('/spa-fallback.html'))
|
|
|
|
expect(html.includes('<h1>Index page</h1>')).toBe(false)
|
|
|
|
expect(html.includes('data-server-rendered')).toBe(false)
|
|
|
|
expect(existsSync(resolve(distDir, 'spa-fallback.html'))).toBe(true)
|
|
|
|
expect(existsSync(resolve(distDir, '200.html'))).toBe(false)
|
|
|
|
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
|
|
|
|
})
|
|
|
|
|
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 server.close()
|
|
|
|
})
|
2018-01-27 00:20:03 +00:00
|
|
|
})
|