Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests #31

Merged
merged 5 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
preset: 'ts-jest',
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts'],
testEnvironment: 'node'
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test": "yarn lint && jest"
},
"dependencies": {
"defu": "^2.0.4",
"defu": "^3.0.1",
"get-port": "^5.1.1",
"tib": "^0.7.4"
},
Expand All @@ -34,8 +34,10 @@
"codecov": "latest",
"eslint": "latest",
"husky": "latest",
"core-js": "latest",
"jest": "latest",
"nuxt-edge": "latest",
"puppeteer": "latest",
"rollup-plugin-typescript2": "latest",
"standard-version": "latest",
"ts-jest": "latest",
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createContext (options: Partial<NuxtTestContext>): NuxtTestConte

export function getContext (): NuxtTestContext {
if (!currentContext) {
throw new Error('No context is avilable. (Forgot calling setup or createContext?)')
throw new Error('No context is available. (Forgot calling setup or createContext?)')
}

return currentContext
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import './jest.matchers'

export * from './browser'
export * from './build'
export * from './context'
export * from './generate'
export * from './jest'
export * from './nuxt'
export * from './server'
export * from './types'
49 changes: 49 additions & 0 deletions src/jest.matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { NuxtTestContext } from './types'

declare global {
namespace jest {
interface Matchers<R> {
toNuxtPluginAdded(plugin: any): CustomMatcherResult
toNuxtLayoutAdded(layout: any, name?: string): CustomMatcherResult
toNuxtErrorLayoutAdded(errorLayout: any): CustomMatcherResult
toNuxtServerMiddlewareAdded(middleware: any): CustomMatcherResult
toNuxtRequireModule(moduleOpts: any): CustomMatcherResult
}
}
}

expect.extend({
toNuxtPluginAdded (ctx: NuxtTestContext, plugin: any) {
expect(ctx.nuxt.moduleContainer.addPlugin).toBeCalledWith(plugin)

return { pass: true, message: () => '' }
},

toNuxtLayoutAdded (ctx: NuxtTestContext, layout: any, name?: string) {
if (name) {
expect(ctx.nuxt.moduleContainer.addLayout).toBeCalledWith(layout, name)
} else {
expect(ctx.nuxt.moduleContainer.addLayout).toBeCalledWith(layout)
}

return { pass: true, message: () => '' }
},

toNuxtErrorLayoutAdded (ctx: NuxtTestContext, errorLayout: any) {
expect(ctx.nuxt.moduleContainer.addErrorLayout).toBeCalledWith(errorLayout)

return { pass: true, message: () => '' }
},

toNuxtServerMiddlewareAdded (ctx: NuxtTestContext, middleware: any) {
expect(ctx.nuxt.moduleContainer.addServerMiddleware).toBeCalledWith(middleware)

return { pass: true, message: () => '' }
},

toNuxtRequireModule (ctx: NuxtTestContext, moduleOpts: any) {
expect(ctx.nuxt.moduleContainer.requireModule).toBeCalledWith(moduleOpts)

return { pass: true, message: () => '' }
}
})
32 changes: 0 additions & 32 deletions src/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,3 @@ export function spyOnClass (instance: any) {
jest.spyOn(instance, key)
}
}

expect.extend({
toNuxtPluginAdded (ctx, plugin) {
expect(ctx.nuxt.moduleContainer.addPlugin).toBeCalledWith(plugin)

return { pass: true, message: () => '' }
},

toNuxtLayoutAdded (ctx, layout, name = undefined) {
expect(ctx.nuxt.moduleContainer.addLayout).toBeCalledWith(layout, name)

return { pass: true, message: () => '' }
},

toNuxtErrorLayoutAdded (ctx, errorLayout) {
expect(ctx.nuxt.moduleContainer.addErrorLayout).toBeCalledWith(errorLayout)

return { pass: true, message: () => '' }
},

toNuxtServerMiddlewareAdded (ctx, middleware) {
expect(ctx.nuxt.moduleContainer.addServerMiddleware).toBeCalledWith(middleware)

return { pass: true, message: () => '' }
},

toNuxtRequireModule (ctx, moduleOpts) {
expect(ctx.nuxt.moduleContainer.requireModule).toBeCalledWith(moduleOpts)

return { pass: true, message: () => '' }
}
})
8 changes: 4 additions & 4 deletions src/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ export async function loadNuxt () {
const ctx = getContext()
const { Nuxt } = await loadNuxtPackage()

ctx.nuxt = new Nuxt(ctx.config || {})
ctx.nuxt = new Nuxt(ctx.config)
}

export async function loadFixture () {
const ctx = getContext()

ctx.rootDir = resolve(ctx.__dirname, ctx.fixture)

const configPath = resolve(ctx.rootDir, ctx.configFile)
const loadedConfig = await import(configPath).then(m => m.default || m)
const loadedConfig = await import(resolve(ctx.rootDir, ctx.configFile))
.then(m => /* istanbul ignore next */ m.default || m)

ctx.config = defu(ctx.config, loadedConfig)

Expand All @@ -26,5 +26,5 @@ export async function loadFixture () {

export async function loadNuxtPackage (name: string = 'nuxt') {
return await import(name + '-edge')
.catch(() => import(name))
.catch(/* istanbul ignore next */ () => import(name))
}
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NuxtConfig } from '@nuxt/types'
import { NuxtConfig, NuxtOptions } from '@nuxt/types'

export interface NuxtTestContext {
__dirname: string
Expand All @@ -8,7 +8,7 @@ export interface NuxtTestContext {
rootDir: string
config: NuxtConfig
nuxt: {
config: NuxtConfig
options: NuxtOptions
listen: (port?: number) => any
ready: () => any
close: () => any
Expand Down
41 changes: 41 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { setupTest, createPage, NuxtTestContext } from '../src'

describe('basic', () => {
const ctx: NuxtTestContext = setupTest({
__dirname,
fixture: 'fixtures/basic',
browser: true,
waitFor: 100
})

test('should render page', async () => {
const page = await createPage('/')
const html = await page.getHtml()
expect(html).toContain('Works!')
})

test('should be added plugin', () => {
expect(ctx).toNuxtPluginAdded({
src: expect.stringContaining('plugin.js'),
fileName: 'plugin-a.js',
options: {}
})
})

test('should be added layout', () => {
expect(ctx).toNuxtLayoutAdded(expect.stringContaining('layout.vue'))
expect(ctx).toNuxtLayoutAdded(expect.stringContaining('layout.vue'), 'name-layout')
})

test('should be added error layout', () => {
expect(ctx).toNuxtErrorLayoutAdded(expect.stringContaining('error'))
})

test('should be added middleware', () => {
expect(ctx).toNuxtServerMiddlewareAdded(expect.stringContaining('middleware.js'))
})

test('should be require module', () => {
expect(ctx).toNuxtRequireModule('~/modules/module-b')
})
})
25 changes: 25 additions & 0 deletions test/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createContext, getContext, setContext, NuxtTestContext } from '../src'

describe('context', () => {
beforeEach(() => {
setContext(null)
})

test('should be error if no context available', () => {
expect(() => getContext()).toThrowError('No context is available. (Forgot calling setup or createContext?)')
})

test('default values from context', () => {
const ctx: NuxtTestContext = createContext({})

expect(ctx).toStrictEqual({
__dirname: ctx.__dirname,
configFile: 'nuxt.config.js',
browserString: 'puppeteer',
buildTimeout: 60000,
server: undefined,
build: undefined,
config: {}
})
})
})
5 changes: 5 additions & 0 deletions test/fixtures/basic/modules/module-a/error.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
Error Layout
</div>
</template>
18 changes: 18 additions & 0 deletions test/fixtures/basic/modules/module-a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { resolve } = require('path')

module.exports = function (options) {
this.addPlugin({
src: resolve(__dirname, 'plugin.js'),
fileName: 'plugin-a.js',
options
})

this.addLayout(resolve(__dirname, 'layout.vue'))
this.addLayout(resolve(__dirname, 'layout.vue'), 'name-layout')

this.addLayout(resolve(__dirname, 'error.vue'), 'error')

this.addServerMiddleware(resolve(__dirname, 'middleware.js'))

this.requireModule('~/modules/module-b')
}
5 changes: 5 additions & 0 deletions test/fixtures/basic/modules/module-a/layout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<nuxt />
</div>
</template>
3 changes: 3 additions & 0 deletions test/fixtures/basic/modules/module-a/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (_req, _res, next) {
next()
}
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/basic/modules/module-b/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {

}
7 changes: 7 additions & 0 deletions test/fixtures/basic/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
srcDir: __dirname,

modules: [
'~/modules/module-a'
]
}
10 changes: 10 additions & 0 deletions test/fixtures/basic/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<div>
Works!
</div>
</template>

<script>
export default {
}
</script>
3 changes: 3 additions & 0 deletions test/fixtures/generate/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {

}
10 changes: 10 additions & 0 deletions test/fixtures/generate/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<div>
Works!
</div>
</template>

<script>
export default {
}
</script>
19 changes: 19 additions & 0 deletions test/generate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { setupTest, NuxtTestContext } from '../src'

describe('generate', () => {
const ctx: NuxtTestContext = setupTest({
pi0 marked this conversation as resolved.
Show resolved Hide resolved
__dirname,
fixture: 'fixtures/generate',
generate: true,
config: {
rootDir: resolve(__dirname, 'fixtures/generate')
}
})

test('should generated page', () => {
const html = readFileSync(resolve(ctx.nuxt.options.generate.dir, 'index.html'), 'utf8')
expect(html).toContain('Works!')
})
})
Loading