mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
test: refactor generate tests
This commit is contained in:
parent
6cd7f04a4e
commit
b4727eb4c4
@ -43,6 +43,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build-fixtures": "node ./scripts/build-fixtures",
|
"build-fixtures": "node ./scripts/build-fixtures",
|
||||||
"test": "npm run lint && npm run build-fixtures && jest test/",
|
"test": "npm run lint && npm run build-fixtures && jest test/",
|
||||||
|
"test:unit": "jest",
|
||||||
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
|
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
|
||||||
"lint": "eslint --ext .js,.mjs,.vue bin/* build/ lib/ test/ examples/",
|
"lint": "eslint --ext .js,.mjs,.vue bin/* build/ lib/ test/ examples/",
|
||||||
"precommit": "npm run lint",
|
"precommit": "npm run lint",
|
||||||
|
@ -11,6 +11,7 @@ import { loadFixture, getPort } from './utils'
|
|||||||
let port
|
let port
|
||||||
const url = route => 'http://localhost:' + port + route
|
const url = route => 'http://localhost:' + port + route
|
||||||
const rootDir = resolve(__dirname, 'fixtures/basic')
|
const rootDir = resolve(__dirname, 'fixtures/basic')
|
||||||
|
const distDir = resolve(rootDir, '.nuxt-generate')
|
||||||
|
|
||||||
let nuxt = null
|
let nuxt = null
|
||||||
let server = null
|
let server = null
|
||||||
@ -18,13 +19,13 @@ let generator = null
|
|||||||
|
|
||||||
describe('basic generate', () => {
|
describe('basic generate', () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const config = loadFixture('basic')
|
const config = loadFixture('basic', {generate: {dir: '.nuxt-generate'}})
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
generator = new Generator(nuxt)
|
generator = new Generator(nuxt)
|
||||||
|
|
||||||
await generator.generate({ build: false })
|
await generator.generate({ build: false })
|
||||||
|
|
||||||
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
|
const serve = serveStatic(distDir)
|
||||||
server = http.createServer((req, res) => {
|
server = http.createServer((req, res) => {
|
||||||
serve(req, res, finalhandler(req, res))
|
serve(req, res, finalhandler(req, res))
|
||||||
})
|
})
|
||||||
@ -84,9 +85,9 @@ describe('basic generate', () => {
|
|||||||
const html = await rp(url('/users/1/index.html'))
|
const html = await rp(url('/users/1/index.html'))
|
||||||
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
|
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
|
||||||
expect(
|
expect(
|
||||||
existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))
|
existsSync(resolve(distDir, 'users/1/index.html'))
|
||||||
).toBe(true)
|
).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 () => {
|
test('/users/2', async () => {
|
||||||
@ -139,7 +140,7 @@ describe('basic generate', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('/users/1 not found', async () => {
|
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({
|
await expect(rp(url('/users/1'))).rejects.toMatchObject({
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
response: {
|
response: {
|
||||||
@ -159,9 +160,9 @@ describe('basic generate', () => {
|
|||||||
test('/users/1.html', async () => {
|
test('/users/1.html', async () => {
|
||||||
const html = await rp(url('/users/1.html'))
|
const html = await rp(url('/users/1.html'))
|
||||||
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
|
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(
|
expect(
|
||||||
existsSync(resolve(__dirname, 'fixtures/basic/dist', 'users/1/index.html'))
|
existsSync(resolve(distDir, 'users/1/index.html'))
|
||||||
).toBe(false)
|
).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import { loadFixture, getPort } from './utils'
|
|||||||
|
|
||||||
let port
|
let port
|
||||||
const url = route => 'http://localhost:' + port + route
|
const url = route => 'http://localhost:' + port + route
|
||||||
|
const distDir = resolve(__dirname, 'fixtures/basic/.nuxt-generate-fallback')
|
||||||
|
|
||||||
let nuxt = null
|
let nuxt = null
|
||||||
let server = null
|
let server = null
|
||||||
@ -16,14 +17,14 @@ let generator = null
|
|||||||
|
|
||||||
describe('fallback generate', () => {
|
describe('fallback generate', () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const config = loadFixture('basic')
|
const config = loadFixture('basic', {generate: {dir: '.nuxt-generate-fallback'}})
|
||||||
|
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
generator = new Generator(nuxt)
|
generator = new Generator(nuxt)
|
||||||
|
|
||||||
await generator.generate({ build: false })
|
await generator.generate({ build: false })
|
||||||
|
|
||||||
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
|
const serve = serveStatic(distDir)
|
||||||
server = http.createServer((req, res) => {
|
server = http.createServer((req, res) => {
|
||||||
serve(req, res, finalhandler(req, res))
|
serve(req, res, finalhandler(req, res))
|
||||||
})
|
})
|
||||||
@ -37,8 +38,8 @@ describe('fallback generate', () => {
|
|||||||
const html = await rp(url('/200.html'))
|
const html = await rp(url('/200.html'))
|
||||||
expect(html.includes('<h1>Index page</h1>')).toBe(false)
|
expect(html.includes('<h1>Index page</h1>')).toBe(false)
|
||||||
expect(html.includes('data-server-rendered')).toBe(false)
|
expect(html.includes('data-server-rendered')).toBe(false)
|
||||||
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '200.html'))).toBe(true)
|
expect(existsSync(resolve(distDir, '200.html'))).toBe(true)
|
||||||
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '404.html'))).toBe(false)
|
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('nuxt re-generating with generate.fallback = false', async () => {
|
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(distDir, '200.html'))).toBe(false)
|
||||||
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '404.html'))).toBe(false)
|
expect(existsSync(resolve(distDir, '404.html'))).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('generate.fallback = true is transformed to /404.html', async () => {
|
test('generate.fallback = true is transformed to /404.html', async () => {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import _getPort from 'get-port'
|
import _getPort from 'get-port'
|
||||||
|
import { defaultsDeep } from 'lodash'
|
||||||
|
|
||||||
import { requireModule } from '../../lib/common/module'
|
import { requireModule } from '../../lib/common/module'
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ export function loadFixture(fixture, overrides) {
|
|||||||
config.rootDir = rootDir
|
config.rootDir = rootDir
|
||||||
config.dev = false
|
config.dev = false
|
||||||
|
|
||||||
return Object.assign({}, config, overrides)
|
return defaultsDeep({}, overrides, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPort() {
|
export function getPort() {
|
||||||
|
Loading…
Reference in New Issue
Block a user