Nuxt/packages/cli/test/utils/mocking.js
Pooya Parsa 0f104aa588
feat: improve SSR bundle (#4439)
- Better insights and inspection for server bundle
- Remove all vue related dependencies from vue-renderer package as much as possible to reduce install size of nuxt-start
- Support for single file distributions (serverless)
- Remove server-bundle.json and use the standard .js files for dist/server
- Mitigate CALL_AND_RETRY_LAST Allocation failed errors. Most of the cases happen on JSON.parse() the part when loading bundle. (#4225, #3465, #1728, #1601, #1481)
- Reduce server dist size by removing escape characters caused by JSON serialize
- Faster dev reloads and production start by removing extra JSON.serialize/JSON.parse time
- Less memory usage
- General performance improvements and refactors
2018-12-01 13:43:28 +03:30

118 lines
2.6 KiB
JavaScript

import * as imports from '../../src/imports'
import Command from '../../src/command'
jest.mock('../../src/imports', () => {
return {
core: jest.fn().mockImplementation(() => ({
Nuxt: function () {}
})),
builder: jest.fn().mockImplementation(() => ({
Builder: function () {}
})),
generator: jest.fn().mockImplementation(() => ({
Generator: function () {}
})),
webpack: jest.fn().mockImplementation(() => ({
BundleBuilder: function () {}
}))
}
})
export const mockGetNuxt = (options, implementation) => {
Command.prototype.getNuxt = jest.fn().mockImplementationOnce(() => {
return Object.assign({
hook: jest.fn(),
options
}, implementation || {})
})
}
export const mockGetBuilder = (ret) => {
const build = jest.fn().mockImplementationOnce(() => {
return ret
})
Command.prototype.getBuilder = jest.fn().mockImplementationOnce(() => {
return { build }
})
return build
}
export const mockGetGenerator = (ret) => {
const generate = jest.fn()
if (ret) {
generate.mockImplementationOnce(() => {
return ret
})
}
Command.prototype.getGenerator = jest.fn().mockImplementationOnce(() => {
return { generate }
})
return generate
}
export const mockGetNuxtStart = (ssr) => {
const listen = jest.fn().mockImplementationOnce(() => {
return Promise.resolve()
})
mockGetNuxt({
rootDir: '.',
render: {
ssr
}
}, {
server: {
listen,
listeners: []
}
})
return { listen }
}
export const mockGetNuxtConfig = () => {
const spy = jest.fn()
Command.prototype.getNuxtConfig = spy
return spy
}
export const mockNuxt = (implementation) => {
const Nuxt = function () {}
Object.assign(Nuxt.prototype, {
hook(type, fn) {
if (type === 'watch:fileChanged') {
Nuxt.fileChangedHook = fn
}
},
options: {},
clearHook: jest.fn(),
close: jest.fn(),
ready: jest.fn(),
server: {
listeners: [],
listen: jest.fn().mockImplementationOnce(() => Promise.resolve())
}
}, implementation || {})
imports.core.mockImplementation(() => ({ Nuxt }))
return Nuxt
}
export const mockBuilder = (implementation) => {
const Builder = function () {}
Object.assign(Builder.prototype, {
build: jest.fn().mockImplementationOnce(() => Promise.resolve()),
unwatch: jest.fn().mockImplementationOnce(() => Promise.resolve()),
watchServer: jest.fn().mockImplementationOnce(() => Promise.resolve())
}, implementation || {})
imports.builder.mockImplementation(() => ({ Builder }))
return Builder
}