2019-01-19 12:00:51 +00:00
|
|
|
import { defineAlias } from '@nuxt/utils'
|
|
|
|
import { getNuxtConfig } from '@nuxt/config'
|
|
|
|
import { Server } from '@nuxt/server'
|
2023-11-24 14:10:27 +00:00
|
|
|
import Hookable from 'hookable'
|
2019-01-19 12:00:51 +00:00
|
|
|
|
|
|
|
import Nuxt from '../src/nuxt'
|
|
|
|
import ModuleContainer from '../src/module'
|
|
|
|
import Resolver from '../src/resolver'
|
|
|
|
import { version } from '../package.json'
|
|
|
|
|
|
|
|
jest.mock('@nuxt/utils')
|
2019-03-08 20:43:23 +00:00
|
|
|
|
|
|
|
jest.mock('@nuxt/server')
|
|
|
|
|
2019-01-19 12:00:51 +00:00
|
|
|
jest.mock('@nuxt/config', () => ({
|
2019-03-30 18:53:56 +00:00
|
|
|
getNuxtConfig: jest.fn(() => ({
|
|
|
|
_ready: false
|
|
|
|
}))
|
2019-01-19 12:00:51 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
describe('core: nuxt', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
test('should construct nuxt with options', async () => {
|
2019-01-19 12:00:51 +00:00
|
|
|
const options = {}
|
|
|
|
const nuxt = new Nuxt(options)
|
2019-03-08 20:43:23 +00:00
|
|
|
await nuxt.ready()
|
2019-01-19 12:00:51 +00:00
|
|
|
|
|
|
|
expect(nuxt).toBeInstanceOf(Hookable)
|
|
|
|
expect(getNuxtConfig).toBeCalledTimes(1)
|
|
|
|
expect(getNuxtConfig).toBeCalledWith(options)
|
|
|
|
|
|
|
|
expect(nuxt.resolver).toBeInstanceOf(Resolver)
|
|
|
|
expect(nuxt.moduleContainer).toBeInstanceOf(ModuleContainer)
|
|
|
|
expect(nuxt.server).toBeInstanceOf(Server)
|
|
|
|
|
|
|
|
expect(defineAlias).toBeCalledTimes(2)
|
2019-03-08 20:43:23 +00:00
|
|
|
expect(defineAlias).nthCalledWith(1, nuxt, nuxt.resolver, ['resolveAlias', 'resolvePath'])
|
|
|
|
expect(defineAlias).nthCalledWith(2, nuxt, nuxt.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
|
2019-01-19 12:00:51 +00:00
|
|
|
|
|
|
|
expect(nuxt.renderer).toBe(nuxt.server)
|
|
|
|
expect(nuxt.render).toBe(nuxt.server.app)
|
|
|
|
expect(nuxt.showReady).toBeInstanceOf(Function)
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
expect(nuxt._ready).toBeInstanceOf(Promise)
|
2019-01-19 12:00:51 +00:00
|
|
|
})
|
|
|
|
|
2019-02-08 10:05:01 +00:00
|
|
|
// TODO: Remove in next major release
|
2019-01-19 12:00:51 +00:00
|
|
|
test('should call hook webpack:done in showReady', () => {
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
nuxt.callHook = jest.fn()
|
|
|
|
|
|
|
|
nuxt.showReady()
|
|
|
|
|
|
|
|
expect(nuxt.callHook).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.callHook).toBeCalledWith('webpack:done')
|
|
|
|
})
|
|
|
|
|
2020-02-25 16:15:40 +00:00
|
|
|
for (const name of ['render:context', 'render:routeContext']) {
|
|
|
|
test('should deprecate ' + name, async () => {
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
const fn = jest.fn()
|
|
|
|
nuxt.hook(name, fn)
|
|
|
|
await nuxt.callHook('_render:context')
|
|
|
|
expect(fn).toBeCalledTimes(1)
|
|
|
|
expect(fn).toBeCalledWith()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-19 12:00:51 +00:00
|
|
|
test('should display fatal message if ready failed', async () => {
|
|
|
|
const err = new Error('nuxt ready failed')
|
|
|
|
const nuxt = new Nuxt()
|
2019-03-08 20:43:23 +00:00
|
|
|
nuxt._init = () => Promise.reject(err)
|
2019-03-30 18:53:56 +00:00
|
|
|
await expect(nuxt.ready()).rejects.toThrow(err)
|
2019-01-19 12:00:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should return nuxt version from package.json', () => {
|
|
|
|
expect(Nuxt.version).toEqual(`v${version}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should call module/server ready in nuxt.ready', async () => {
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
|
|
|
|
nuxt.callHook = jest.fn()
|
|
|
|
nuxt.server = { ready: jest.fn() }
|
|
|
|
nuxt.moduleContainer = { ready: jest.fn() }
|
|
|
|
|
|
|
|
const result = await nuxt.ready()
|
|
|
|
|
|
|
|
expect(result).toBe(nuxt)
|
|
|
|
expect(nuxt.moduleContainer.ready).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.server.ready).toBeCalledTimes(1)
|
2019-03-08 20:43:23 +00:00
|
|
|
expect(nuxt._initCalled).toEqual(true)
|
2019-01-19 12:00:51 +00:00
|
|
|
expect(nuxt.callHook).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.callHook).toBeCalledWith('ready', nuxt)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should ignore ready when _ready exists', async () => {
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
const _ready = nuxt._ready = jest.fn()
|
|
|
|
const result = await nuxt.ready()
|
|
|
|
expect(result).toBe(_ready)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should add object hooks', async () => {
|
|
|
|
const hooks = {}
|
2019-03-30 18:53:56 +00:00
|
|
|
getNuxtConfig.mockReturnValueOnce({ hooks, _ready: false })
|
2019-01-19 12:00:51 +00:00
|
|
|
const nuxt = new Nuxt()
|
|
|
|
|
|
|
|
nuxt.addHooks = jest.fn()
|
|
|
|
nuxt.server = { ready: jest.fn() }
|
|
|
|
nuxt.moduleContainer = { ready: jest.fn() }
|
|
|
|
|
|
|
|
await nuxt.ready()
|
|
|
|
|
|
|
|
expect(nuxt.addHooks).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.addHooks).toBeCalledWith(hooks)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should add function hooks', async () => {
|
|
|
|
const hooks = jest.fn()
|
|
|
|
getNuxtConfig.mockReturnValueOnce({ hooks })
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
|
|
|
|
nuxt.addHooks = jest.fn()
|
|
|
|
nuxt.server = { ready: jest.fn() }
|
|
|
|
nuxt.moduleContainer = { ready: jest.fn() }
|
|
|
|
|
|
|
|
await nuxt.ready()
|
|
|
|
|
|
|
|
expect(nuxt.addHooks).not.toBeCalled()
|
|
|
|
expect(hooks).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should close nuxt with hook triggered', async () => {
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
nuxt.callHook = jest.fn()
|
|
|
|
nuxt.clearHooks = jest.fn()
|
|
|
|
|
|
|
|
const cb = jest.fn()
|
|
|
|
await nuxt.close(cb)
|
|
|
|
|
|
|
|
expect(cb).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.callHook).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.callHook).toBeCalledWith('close', nuxt)
|
|
|
|
expect(nuxt.clearHooks).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should ignore non-function callback in close', async () => {
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
|
|
|
|
nuxt.callHook = jest.fn()
|
|
|
|
nuxt.server = { ready: jest.fn() }
|
|
|
|
nuxt.moduleContainer = { ready: jest.fn() }
|
|
|
|
|
|
|
|
const result = await nuxt.ready()
|
|
|
|
|
|
|
|
expect(result).toBe(nuxt)
|
|
|
|
expect(nuxt.moduleContainer.ready).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.server.ready).toBeCalledTimes(1)
|
2019-03-08 20:43:23 +00:00
|
|
|
expect(nuxt._initCalled).toEqual(true)
|
2019-01-19 12:00:51 +00:00
|
|
|
expect(nuxt.callHook).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.callHook).toBeCalledWith('ready', nuxt)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should ignore non-function callback in close', async () => {
|
|
|
|
const nuxt = new Nuxt()
|
|
|
|
nuxt.callHook = jest.fn()
|
|
|
|
nuxt.clearHooks = jest.fn()
|
|
|
|
|
|
|
|
const cb = {}
|
|
|
|
await nuxt.close(cb)
|
|
|
|
|
|
|
|
expect(nuxt.callHook).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.clearHooks).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
})
|