mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat: merge renderAndGetWindow options (#3761)
* feat: merge renderAndGetWindow options * fix: typoe * refactor: remove explicit comparison for truthy value * fix: setup defaults correctly * test: add custom params test
This commit is contained in:
parent
3612ecd435
commit
3e027269c0
@ -403,17 +403,20 @@ export default class Renderer {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
const options = {
|
||||
const options = Object.assign({
|
||||
resources: 'usable', // load subresources (https://github.com/tmpvar/jsdom#loading-subresources)
|
||||
runScripts: 'dangerously',
|
||||
virtualConsole: true,
|
||||
beforeParse(window) {
|
||||
// Mock window.scrollTo
|
||||
window.scrollTo = () => {}
|
||||
}
|
||||
}
|
||||
}, opts)
|
||||
const jsdomErrHandler = (err) => { throw err }
|
||||
if (opts.virtualConsole !== false) {
|
||||
options.virtualConsole = new jsdom.VirtualConsole().sendTo(consola)
|
||||
if (options.virtualConsole) {
|
||||
if (options.virtualConsole === true) {
|
||||
options.virtualConsole = new jsdom.VirtualConsole().sendTo(consola)
|
||||
}
|
||||
// throw error when window creation failed
|
||||
options.virtualConsole.on('jsdomError', jsdomErrHandler)
|
||||
}
|
||||
@ -433,7 +436,7 @@ export default class Renderer {
|
||||
await timeout(new Promise((resolve) => {
|
||||
window._onNuxtLoaded = () => resolve(window)
|
||||
}), 20000, 'Components loading in renderAndGetWindow was not completed in 20s')
|
||||
if (opts.virtualConsole !== false) {
|
||||
if (options.virtualConsole) {
|
||||
// after window initialized successfully
|
||||
options.virtualConsole.removeListener('jsdomError', jsdomErrHandler)
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { loadFixture, getPort, Nuxt, rp } from '../utils'
|
||||
import jsdom from 'jsdom'
|
||||
import { getPort, loadFixture, Nuxt, rp } from '../utils'
|
||||
|
||||
let port
|
||||
const url = route => 'http://localhost:' + port + route
|
||||
@ -139,6 +140,23 @@ describe('with-config', () => {
|
||||
.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.renderAndGetWindow(url('/test/error'), mockOptions)
|
||||
} catch (e) {}
|
||||
expect(mockOptions.beforeParse).toHaveBeenCalled()
|
||||
expect(fakeErrorLog).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
afterAll(async () => {
|
||||
await nuxt.close()
|
||||
|
Loading…
Reference in New Issue
Block a user