Skip to content

Commit

Permalink
test: unit tests for server module (#5154)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdo committed Mar 4, 2019
1 parent e3991aa commit cc573a4
Show file tree
Hide file tree
Showing 14 changed files with 1,875 additions and 14 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = {

coveragePathIgnorePatterns: [
'node_modules/(?!(@nuxt|nuxt))',
'packages/webpack/src/config/plugins/vue'
'packages/webpack/src/config/plugins/vue',
'packages/server/src/jsdom'
],

testPathIgnorePatterns: [
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/test/unit/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('build', () => {
jest.spyOn(utils, 'createLock').mockImplementation(() => () => {})
})

afterAll(() => {
process.exit.mockRestore()
})

afterEach(() => jest.resetAllMocks())

test('has run function', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/test/unit/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('generate', () => {
jest.spyOn(utils, 'createLock').mockImplementation(() => () => {})
})

afterAll(() => {
process.exit.mockRestore()
})

afterEach(() => jest.resetAllMocks())

test('has run function', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/server/src/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default async function renderAndGetWindow(
ssr ? `window.${globals.context}` : `<div id="${globals.id}">`
)

/* istanbul ignore if */
if (!nuxtExists) {
const error = new Error('Could not load the nuxt app')
error.body = window.document.body.innerHTML
Expand Down
20 changes: 10 additions & 10 deletions packages/server/src/middleware/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ export default ({ resources, options }) => async function errorMiddleware(err, r
message: err.message || 'Nuxt Server Error',
name: !err.name || err.name === 'Error' ? 'NuxtServerError' : err.name
}
const errorFull = err instanceof Error
? err
: typeof err === 'string'
? new Error(err)
: new Error(err.message || JSON.stringify(err))

errorFull.name = error.name
errorFull.statusCode = error.statusCode
errorFull.stack = err.stack || undefined

const sendResponse = (content, type = 'text/html') => {
// Set Headers
Expand Down Expand Up @@ -62,6 +53,16 @@ export default ({ resources, options }) => async function errorMiddleware(err, r
return
}

const errorFull = err instanceof Error
? err
: typeof err === 'string'
? new Error(err)
: new Error(err.message || JSON.stringify(err))

errorFull.name = error.name
errorFull.statusCode = error.statusCode
errorFull.stack = err.stack || undefined

// Show stack trace
const youch = new Youch(
errorFull,
Expand Down Expand Up @@ -90,7 +91,6 @@ const readSourceFactory = ({ srcDir, rootDir, buildDir }) => async function read
frame.fileName = sanitizeName(frame.fileName)

// Return if fileName is unknown
/* istanbul ignore if */
if (!frame.fileName) {
return
}
Expand Down
2 changes: 0 additions & 2 deletions packages/server/src/middleware/nuxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export default ({ options, nuxt, renderRoute, resources }) => async function nux
await nuxt.callHook('render:routeDone', url, result, context)
return html
} catch (err) {
/* istanbul ignore if */
if (context && context.redirected) {
consola.error(err)
return err
Expand All @@ -95,7 +94,6 @@ const defaultPushAssets = (preloadFiles, shouldPush, publicPath, options) => {
const links = []
preloadFiles.forEach(({ file, asType, fileWithoutQuery, modern }) => {
// By default, we only preload scripts or css
/* istanbul ignore if */
if (!shouldPush && asType !== 'script' && asType !== 'style') {
return
}
Expand Down
17 changes: 17 additions & 0 deletions packages/server/test/contest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ServerContext from '../src/context'

describe('server: ServerContext', () => {
test('should construct context', () => {
const server = {
nuxt: { id: 'test-contest-nuxt' },
globals: { id: 'test-contest-globals' },
options: { id: 'test-contest-options' },
resources: { id: 'test-contest-resources' }
}
const context = new ServerContext(server)
expect(context.nuxt).toBe(server.nuxt)
expect(context.globals).toEqual(server.globals)
expect(context.options).toEqual(server.options)
expect(context.resources).toEqual(server.resources)
})
})
14 changes: 14 additions & 0 deletions packages/server/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Server, Listener } from '../src'

jest.mock('../src/server', () => ({
server: true
})).mock('../src/listener', () => ({
listener: true
}))

describe('server: entry', () => {
test('should export Server and Listener', () => {
expect(Server.server).toEqual(true)
expect(Listener.listener).toEqual(true)
})
})

0 comments on commit cc573a4

Please sign in to comment.