2018-12-10 21:34:41 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import { existsSync } from 'fs'
|
2018-08-22 15:14:15 +00:00
|
|
|
import jsdom from 'jsdom'
|
2018-12-10 21:34:41 +00:00
|
|
|
import Glob from 'glob'
|
|
|
|
import pify from 'pify'
|
2018-08-22 15:14:15 +00:00
|
|
|
import { getPort, loadFixture, Nuxt, rp } from '../utils'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-12-10 21:34:41 +00:00
|
|
|
const glob = pify(Glob)
|
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
let port
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2016-12-21 14:03:23 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
describe('with-config', () => {
|
|
|
|
beforeAll(async () => {
|
2018-08-17 20:25:23 +00:00
|
|
|
const config = await loadFixture('with-config')
|
2017-12-17 19:30:26 +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()
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.listen(port, 'localhost')
|
2018-03-18 23:41:14 +00:00
|
|
|
})
|
2018-03-18 19:31:32 +00:00
|
|
|
|
2018-12-10 21:34:41 +00:00
|
|
|
test('client source map generated', async () => {
|
|
|
|
const jsFiles = await glob(resolve(__dirname, '..', 'fixtures/with-config/.nuxt/dist/client/*.js'))
|
|
|
|
for (const file of jsFiles) {
|
|
|
|
expect(existsSync(`${file}.map`)).toBe(true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>I have custom configurations</h1>')
|
2017-11-24 21:19:39 +00:00
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
2018-09-30 16:11:20 +00:00
|
|
|
test('/ (asset name for analyze mode)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
2018-09-30 16:11:20 +00:00
|
|
|
expect(html).toContain('<script src="/test/orion/app.js"')
|
|
|
|
})
|
|
|
|
|
2018-11-08 09:41:24 +00:00
|
|
|
test('/ (global styles inlined)', async () => {
|
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/'))
|
|
|
|
const html = window.document.head.innerHTML
|
2018-03-27 22:28:17 +00:00
|
|
|
expect(html).toContain('.global-css-selector')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-08-30 14:18:37 +00:00
|
|
|
|
2018-11-08 09:41:24 +00:00
|
|
|
test('/ (preload fonts)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain(
|
2020-06-09 19:45:52 +00:00
|
|
|
'<link rel="preload" href="/test/orion/fonts/roboto.7cf5d7c.woff2" as="font" type="font/woff2" crossorigin'
|
2018-11-08 09:41:24 +00:00
|
|
|
)
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-05-05 14:25:17 +00:00
|
|
|
|
2018-10-25 10:55:05 +00:00
|
|
|
test('/ (styleResources styles inlined)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
2018-10-25 10:55:05 +00:00
|
|
|
expect(html).toContain('.pre-process-selector')
|
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/ (custom app.html)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<p>Made by Nuxt.js team</p>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-03-24 17:35:30 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/ (custom build.publicPath)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<script src="/test/orion/')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-03-24 17:35:30 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/ (custom postcss.config.js)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { html } = await nuxt.server.renderRoute('/')
|
2020-06-08 10:42:35 +00:00
|
|
|
expect(html).toContain('::-moz-placeholder')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-11-21 01:54:02 +00:00
|
|
|
|
2018-10-09 12:07:23 +00:00
|
|
|
test('/test/ (custom globalName)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/'))
|
2018-10-09 12:07:23 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('id="custom-nuxt-id">')
|
2018-10-09 12:07:23 +00:00
|
|
|
expect(html.includes('id="__nuxt">')).toBe(false)
|
|
|
|
expect(window.__NOXXT__).toBeDefined()
|
|
|
|
expect(window.__NUXT__).toBeUndefined()
|
|
|
|
expect(window.$noxxt).toBeDefined()
|
|
|
|
expect(window.$nuxt).toBeDefined() // for Vue Dev Tools detection
|
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/ (router base)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/'))
|
2018-03-18 19:31:32 +00:00
|
|
|
|
|
|
|
const html = window.document.body.innerHTML
|
2018-10-09 12:07:23 +00:00
|
|
|
expect(window.__NOXXT__.layout).toBe('default')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Default layout</h1>')
|
|
|
|
expect(html).toContain('<h1>I have custom configurations</h1>')
|
2018-03-27 22:28:17 +00:00
|
|
|
|
|
|
|
expect(window.__test_plugin).toBe(true)
|
2018-12-18 14:19:38 +00:00
|
|
|
expect(window.__test_plugin_ext).toBe(true)
|
2018-12-20 09:28:10 +00:00
|
|
|
expect(window.__test_plugin_client).toBe('test_plugin_client')
|
|
|
|
expect(window.__test_plugin_server).toBeUndefined()
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-21 14:03:23 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/about (custom layout)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/about'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-10-09 12:07:23 +00:00
|
|
|
expect(window.__NOXXT__.layout).toBe('custom')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Custom layout</h1>')
|
|
|
|
expect(html).toContain('<h1>About page</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-24 17:50:40 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/desktop (custom layout in desktop folder)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/desktop'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-10-09 12:07:23 +00:00
|
|
|
expect(window.__NOXXT__.layout).toBe('desktop/default')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Default desktop layout</h1>')
|
|
|
|
expect(html).toContain('<h1>Desktop page</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-10-15 19:31:01 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/mobile (custom layout in mobile folder)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/mobile'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-10-09 12:07:23 +00:00
|
|
|
expect(window.__NOXXT__.layout).toBe('mobile/default')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Default mobile layout</h1>')
|
|
|
|
expect(html).toContain('<h1>Mobile page</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-10-15 19:31:01 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/env', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/env'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Custom env layout</h1>')
|
|
|
|
expect(html).toContain('"bool": true')
|
|
|
|
expect(html).toContain('"num": 23')
|
|
|
|
expect(html).toContain('"string": "Nuxt.js"')
|
|
|
|
expect(html).toContain('"bool": false')
|
|
|
|
expect(html).toContain('"string": "ok"')
|
|
|
|
expect(html).toContain('"num2": 8.23')
|
|
|
|
expect(html).toContain('"obj": {')
|
2018-09-08 21:08:25 +00:00
|
|
|
expect(html).toContain('"NUXT_ENV_FOO": "manniL"')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-21 19:51:43 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/error', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/error'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('Error page')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-03-25 17:59:46 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/user-agent', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/user-agent'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<pre>Mozilla')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-02-09 23:45:49 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/about-bis (added with extendRoutes)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/about-bis'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Custom layout</h1>')
|
|
|
|
expect(html).toContain('<h1>About page</h1>')
|
2018-12-04 12:19:05 +00:00
|
|
|
expect(html).toContain('<h2>test-meta</h2>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-01-19 15:26:30 +00:00
|
|
|
|
2018-08-13 16:27:20 +00:00
|
|
|
test('/test/not-existed should return 404', async () => {
|
|
|
|
await expect(rp(url('/test/not-existed')))
|
2019-12-03 17:36:39 +00:00
|
|
|
.rejects.toMatchObject({ response: { statusCode: 404 } })
|
2018-08-13 16:27:20 +00:00
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/test/redirect/about-bis (redirect with extendRoutes)', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/redirect/about-bis'))
|
2018-03-18 19:31:32 +00:00
|
|
|
const windowHref = window.location.href
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(windowHref).toContain('/test/about-bis')
|
2018-03-15 11:57:00 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const html = window.document.body.innerHTML
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(html).toContain('<h1>Custom layout</h1>')
|
|
|
|
expect(html).toContain('<h1>About page</h1>')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2018-03-15 11:57:00 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('Check /test/test.txt with custom serve-static options', async () => {
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/test/test.txt'))
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(headers['cache-control']).toBe('public, max-age=31536000')
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
2017-05-22 10:51:03 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('Check /test.txt should return 404', async () => {
|
|
|
|
await expect(rp(url('/test.txt')))
|
2019-12-03 17:36:39 +00:00
|
|
|
.rejects.toMatchObject({ response: { statusCode: 404 } })
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2017-08-25 12:01:16 +00:00
|
|
|
|
2019-09-18 13:51:44 +00:00
|
|
|
test('/test/head', async () => {
|
|
|
|
const window = await nuxt.server.renderAndGetWindow(url('/test/head'))
|
|
|
|
const html = window.document.querySelector('head').innerHTML
|
|
|
|
expect(html).toContain('<noscript data-n-head="test-ssr-app-id">noscript</noscript>')
|
|
|
|
})
|
|
|
|
|
2019-01-29 09:31:14 +00:00
|
|
|
test('should ignore files in .nuxtignore', async () => {
|
|
|
|
await expect(rp(url('/test-ignore')))
|
2019-12-03 17:36:39 +00:00
|
|
|
.rejects.toMatchObject({ response: { statusCode: 404 } })
|
2019-01-29 09:31:14 +00:00
|
|
|
})
|
|
|
|
|
2018-08-22 15:14:15 +00:00
|
|
|
test('renderAndGetWindow options', async () => {
|
|
|
|
const fakeErrorLog = jest.fn()
|
|
|
|
const mockOptions = {
|
|
|
|
beforeParse: jest.fn((window) => {
|
|
|
|
// Mock window.scrollTo
|
|
|
|
window.scrollTo = () => {}
|
|
|
|
window._virtualConsole.emit('jsdomError', new Error('test'))
|
|
|
|
}),
|
2018-08-31 20:34:12 +00:00
|
|
|
virtualConsole: new jsdom.VirtualConsole().sendTo({ error: fakeErrorLog })
|
2018-08-22 15:14:15 +00:00
|
|
|
}
|
|
|
|
try {
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.renderAndGetWindow(url('/test/error'), mockOptions)
|
2018-08-22 15:14:15 +00:00
|
|
|
} catch (e) {}
|
|
|
|
expect(mockOptions.beforeParse).toHaveBeenCalled()
|
|
|
|
expect(fakeErrorLog).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2019-01-19 18:14:54 +00:00
|
|
|
test('/ with Server-Timing header', async () => {
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/test'))
|
2019-06-15 10:13:05 +00:00
|
|
|
expect(headers['server-timing']).toMatch(/total;dur=\d+(\.\d+)?;desc="Nuxt Server Time"/)
|
2019-01-19 18:14:54 +00:00
|
|
|
})
|
|
|
|
|
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()
|
|
|
|
})
|
2016-12-21 14:03:23 +00:00
|
|
|
})
|
2018-09-23 09:49:51 +00:00
|
|
|
|
|
|
|
describe('server config', () => {
|
|
|
|
test('opens on port defined in server.port', async () => {
|
|
|
|
const config = await loadFixture('with-config')
|
|
|
|
config.server.port = port = await getPort()
|
|
|
|
nuxt = new Nuxt(config)
|
2019-03-08 20:43:23 +00:00
|
|
|
await nuxt.ready()
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.listen()
|
|
|
|
await nuxt.server.renderAndGetWindow(url('/test/'))
|
2018-09-23 09:49:51 +00:00
|
|
|
})
|
|
|
|
afterAll(async () => {
|
|
|
|
await nuxt.close()
|
|
|
|
})
|
|
|
|
})
|