test: refactor generate tests

This commit is contained in:
Clark Du 2018-03-19 12:21:04 +08:00
parent 6cd7f04a4e
commit b4727eb4c4
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
4 changed files with 18 additions and 14 deletions

View File

@ -43,6 +43,7 @@
"scripts": {
"build-fixtures": "node ./scripts/build-fixtures",
"test": "npm run lint && npm run build-fixtures && jest test/",
"test:unit": "jest",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"lint": "eslint --ext .js,.mjs,.vue bin/* build/ lib/ test/ examples/",
"precommit": "npm run lint",

View File

@ -11,6 +11,7 @@ import { loadFixture, getPort } from './utils'
let port
const url = route => 'http://localhost:' + port + route
const rootDir = resolve(__dirname, 'fixtures/basic')
const distDir = resolve(rootDir, '.nuxt-generate')
let nuxt = null
let server = null
@ -18,13 +19,13 @@ let generator = null
describe('basic generate', () => {
beforeAll(async () => {
const config = loadFixture('basic')
const config = loadFixture('basic', {generate: {dir: '.nuxt-generate'}})
nuxt = new Nuxt(config)
generator = new Generator(nuxt)
await generator.generate({ build: false })
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
const serve = serveStatic(distDir)
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
})
@ -84,9 +85,9 @@ describe('basic generate', () => {
const html = await rp(url('/users/1/index.html'))
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
expect(
existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))
existsSync(resolve(distDir, 'users/1/index.html'))
).toBe(true)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html'))).toBe(false)
expect(existsSync(resolve(distDir, 'users/1.html'))).toBe(false)
})
test('/users/2', async () => {
@ -139,7 +140,7 @@ describe('basic generate', () => {
})
test('/users/1 not found', async () => {
await remove(resolve(rootDir, 'dist/users'))
await remove(resolve(distDir, 'users'))
await expect(rp(url('/users/1'))).rejects.toMatchObject({
statusCode: 404,
response: {
@ -159,9 +160,9 @@ describe('basic generate', () => {
test('/users/1.html', async () => {
const html = await rp(url('/users/1.html'))
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1.html'))).toBe(true)
expect(existsSync(resolve(distDir, 'users/1.html'))).toBe(true)
expect(
existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))
existsSync(resolve(distDir, 'users/1/index.html'))
).toBe(false)
})

View File

@ -9,6 +9,7 @@ import { loadFixture, getPort } from './utils'
let port
const url = route => 'http://localhost:' + port + route
const distDir = resolve(__dirname, 'fixtures/basic/.nuxt-generate-fallback')
let nuxt = null
let server = null
@ -16,14 +17,14 @@ let generator = null
describe('fallback generate', () => {
beforeAll(async () => {
const config = loadFixture('basic')
const config = loadFixture('basic', {generate: {dir: '.nuxt-generate-fallback'}})
nuxt = new Nuxt(config)
generator = new Generator(nuxt)
await generator.generate({ build: false })
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
const serve = serveStatic(distDir)
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
})
@ -37,8 +38,8 @@ describe('fallback generate', () => {
const html = await rp(url('/200.html'))
expect(html.includes('<h1>Index page</h1>')).toBe(false)
expect(html.includes('data-server-rendered')).toBe(false)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '200.html'))).toBe(true)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '404.html'))).toBe(false)
expect(existsSync(resolve(distDir, '200.html'))).toBe(true)
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
})
test('nuxt re-generating with generate.fallback = false', async () => {
@ -56,8 +57,8 @@ describe('fallback generate', () => {
}
})
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '200.html'))).toBe(false)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '404.html'))).toBe(false)
expect(existsSync(resolve(distDir, '200.html'))).toBe(false)
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
})
test('generate.fallback = true is transformed to /404.html', async () => {

View File

@ -1,6 +1,7 @@
import path from 'path'
import fs from 'fs'
import _getPort from 'get-port'
import { defaultsDeep } from 'lodash'
import { requireModule } from '../../lib/common/module'
@ -13,7 +14,7 @@ export function loadFixture(fixture, overrides) {
config.rootDir = rootDir
config.dev = false
return Object.assign({}, config, overrides)
return defaultsDeep({}, overrides, config)
}
export function getPort() {