Nuxt/test/unit/with-config.test.js

240 lines
8.3 KiB
JavaScript
Raw Normal View History

import { resolve } from 'path'
import { existsSync } from 'fs'
import jsdom from 'jsdom'
import Glob from 'glob'
import pify from 'pify'
import { getPort, loadFixture, Nuxt, rp } from '../utils'
2018-03-16 19:52:17 +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 () => {
const config = await loadFixture('with-config')
nuxt = new Nuxt(config)
await nuxt.ready()
2018-03-18 23:41:14 +00:00
port = await getPort()
await nuxt.server.listen(port, 'localhost')
2018-03-18 23:41:14 +00:00
})
2018-03-18 19:31:32 +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 () => {
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
})
test('/ (asset name for analyze mode)', async () => {
const { html } = await nuxt.server.renderRoute('/')
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
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 () => {
const { html } = await nuxt.server.renderRoute('/')
2018-11-08 09:41:24 +00:00
expect(html).toContain(
'<link rel="preload" href="/test/orion/fonts/7cf5d7c.woff2" as="font" type="font/woff2" crossorigin'
)
2018-03-18 19:31:32 +00:00
})
test('/ (styleResources styles inlined)', async () => {
const { html } = await nuxt.server.renderRoute('/')
expect(html).toContain('.pre-process-selector')
})
2018-03-18 19:31:32 +00:00
test('/ (custom app.html)', async () => {
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 () => {
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 () => {
const { html } = await nuxt.server.renderRoute('/')
2018-11-08 09:41:24 +00:00
expect(html).toContain('::-webkit-input-placeholder')
2018-03-18 19:31:32 +00:00
})
2017-11-21 01:54:02 +00:00
test('/test/ (custom globalName)', async () => {
const window = await nuxt.server.renderAndGetWindow(url('/test/'))
const html = window.document.body.innerHTML
2018-11-08 09:41:24 +00:00
expect(html).toContain('id="custom-nuxt-id">')
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 () => {
const window = await nuxt.server.renderAndGetWindow(url('/test/'))
2018-03-18 19:31:32 +00:00
const html = window.document.body.innerHTML
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>')
expect(window.__test_plugin).toBe(true)
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 () => {
const window = await nuxt.server.renderAndGetWindow(url('/test/about'))
2018-03-18 19:31:32 +00:00
const html = window.document.body.innerHTML
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 () => {
const window = await nuxt.server.renderAndGetWindow(url('/test/desktop'))
2018-03-18 19:31:32 +00:00
const html = window.document.body.innerHTML
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 () => {
const window = await nuxt.server.renderAndGetWindow(url('/test/mobile'))
2018-03-18 19:31:32 +00:00
const html = window.document.body.innerHTML
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 () => {
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": {')
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 () => {
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 () => {
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 () => {
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>')
expect(html).toContain('<h2>test-meta</h2>')
2018-03-18 19:31:32 +00:00
})
2017-01-19 15:26:30 +00:00
test('/test/not-existed should return 404', async () => {
await expect(rp(url('/test/not-existed')))
.rejects.toMatchObject({ statusCode: 404 })
})
2018-03-18 19:31:32 +00:00
test('/test/redirect/about-bis (redirect with extendRoutes)', async () => {
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 () => {
const { headers } = await rp(url('/test/test.txt'), {
resolveWithFullResponse: true
})
expect(headers['cache-control']).toBe('public, max-age=31536000')
2018-01-13 05:22:11 +00:00
})
2018-03-18 19:31:32 +00:00
test('Check /test.txt should return 404', async () => {
await expect(rp(url('/test.txt')))
.rejects.toMatchObject({ statusCode: 404 })
})
2017-08-25 12:01:16 +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')))
.rejects.toMatchObject({ statusCode: 404 })
})
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'))
}),
virtualConsole: new jsdom.VirtualConsole().sendTo({ error: fakeErrorLog })
}
try {
await nuxt.server.renderAndGetWindow(url('/test/error'), mockOptions)
} catch (e) {}
expect(mockOptions.beforeParse).toHaveBeenCalled()
expect(fakeErrorLog).toHaveBeenCalled()
})
test('/ with Server-Timing header', async () => {
const { headers } = await rp(url('/test'), {
resolveWithFullResponse: true
})
2019-06-15 10:13:05 +00:00
expect(headers['server-timing']).toMatch(/total;dur=\d+(\.\d+)?;desc="Nuxt Server Time"/)
})
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
})
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)
await nuxt.ready()
await nuxt.server.listen()
await nuxt.server.renderAndGetWindow(url('/test/'))
})
afterAll(async () => {
await nuxt.close()
})
})