diff --git a/lib/core/renderer.js b/lib/core/renderer.js index 99d897b193..6e4827ce0c 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -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) } diff --git a/test/unit/with-config.test.js b/test/unit/with-config.test.js index 0292d8d823..9899bd7e83 100644 --- a/test/unit/with-config.test.js +++ b/test/unit/with-config.test.js @@ -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()