mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
test: bring cli test back
This commit is contained in:
parent
3a76475ace
commit
e962ee19e7
@ -13,6 +13,28 @@ export const waitFor = function waitFor(ms) {
|
|||||||
return new Promise(resolve => setTimeout(resolve, ms || 0))
|
return new Promise(resolve => setTimeout(resolve, ms || 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare an object to pass to the createSportsSelectionView function
|
||||||
|
* @param {Function} condition return true to stop the waiting
|
||||||
|
* @param {Number} duration seconds totally wait
|
||||||
|
* @param {Number} interval milliseconds interval to check the condition
|
||||||
|
*
|
||||||
|
* @returns {Boolean} true: timeout, false: condition becomes true within total time
|
||||||
|
*/
|
||||||
|
export const waitUntil = async function waitUntil(condition, duration = 20, interval = 250) {
|
||||||
|
let iterator = 0
|
||||||
|
const steps = Math.floor(duration * 1000 / interval)
|
||||||
|
|
||||||
|
while (!condition() && iterator < steps) {
|
||||||
|
await waitFor(interval) && iterator++
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iterator === steps) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
async function promiseFinally(fn, finalFn) {
|
async function promiseFinally(fn, finalFn) {
|
||||||
let result
|
let result
|
||||||
try {
|
try {
|
||||||
|
27
test/fixtures/cli/cli.test.js
vendored
Normal file
27
test/fixtures/cli/cli.test.js
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { exec } from 'child_process'
|
||||||
|
import { resolve } from 'path'
|
||||||
|
import { promisify } from 'util'
|
||||||
|
|
||||||
|
const execify = promisify(exec)
|
||||||
|
const rootDir = __dirname
|
||||||
|
const nuxtBin = resolve(__dirname, '..', '..', '..', 'bin', 'nuxt')
|
||||||
|
|
||||||
|
describe('cli', () => {
|
||||||
|
test('nuxt build', async () => {
|
||||||
|
const { stdout } = await execify(`node ${nuxtBin} build ${rootDir}`)
|
||||||
|
|
||||||
|
expect(stdout.includes('Compiled successfully')).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('nuxt build -> error config', async () => {
|
||||||
|
await expect(execify(`node ${nuxtBin} build ${rootDir} -c config.js`)).rejects.toMatchObject({
|
||||||
|
stdout: expect.stringContaining('Could not load config file: config.js')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('nuxt generate', async () => {
|
||||||
|
const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`)
|
||||||
|
|
||||||
|
expect(stdout.includes('Generated successfully')).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
22
test/fixtures/cli/nuxt.config.js
vendored
Normal file
22
test/fixtures/cli/nuxt.config.js
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import consola from 'consola'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
hooks(hook) {
|
||||||
|
hook('build:done', builder => {
|
||||||
|
consola.success('Compiled successfully')
|
||||||
|
})
|
||||||
|
hook('generate:done', (generator, errors) => {
|
||||||
|
if (!errors || errors.length === 0) {
|
||||||
|
consola.success('Generated successfully')
|
||||||
|
} else {
|
||||||
|
consola.error('Generated failed')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
hook('listen', (server, { port, host }) => {
|
||||||
|
consola.success(`Listening on http://${host}:${port}`)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
generate: {
|
||||||
|
dir: '.nuxt-generate'
|
||||||
|
}
|
||||||
|
}
|
3
test/fixtures/cli/pages/index.vue
vendored
Normal file
3
test/fixtures/cli/pages/index.vue
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<template>
|
||||||
|
<div>CLI Test</div>
|
||||||
|
</template>
|
@ -1,48 +1,29 @@
|
|||||||
import { exec, spawn } from 'child_process'
|
import { spawn } from 'child_process'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import { promisify } from 'util'
|
import { getPort, Utils, rp } from '../utils'
|
||||||
import { Utils, rp } from '../utils'
|
|
||||||
|
|
||||||
const execify = promisify(exec)
|
|
||||||
const rootDir = resolve(__dirname, '..', 'fixtures/basic')
|
|
||||||
|
|
||||||
let port
|
let port
|
||||||
|
const rootDir = resolve(__dirname, '..', 'fixtures/cli')
|
||||||
|
|
||||||
const url = route => 'http://localhost:' + port + route
|
const url = route => 'http://localhost:' + port + route
|
||||||
|
|
||||||
const nuxtBin = resolve(__dirname, '..', '..', 'bin', 'nuxt')
|
const nuxtBin = resolve(__dirname, '..', '..', 'bin', 'nuxt')
|
||||||
|
|
||||||
describe.skip('cli', () => {
|
describe('cli', () => {
|
||||||
test('nuxt build', async () => {
|
|
||||||
const { stdout } = await execify(`node ${nuxtBin} build ${rootDir}`)
|
|
||||||
|
|
||||||
expect(stdout.includes('Compiled successfully')).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('nuxt build -> error config', async () => {
|
|
||||||
await expect(execify(`node ${nuxtBin} build ${rootDir} -c config.js`)).rejects.toMatchObject({
|
|
||||||
stderr: expect.stringContaining('Could not load config file')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('nuxt start', async () => {
|
test('nuxt start', async () => {
|
||||||
let stdout = ''
|
let stdout = ''
|
||||||
// let stderr = ''
|
|
||||||
let error
|
let error
|
||||||
let exitCode
|
let exitCode
|
||||||
|
|
||||||
const env = process.env
|
const env = process.env
|
||||||
env.PORT = port
|
env.PORT = port = await getPort()
|
||||||
|
|
||||||
const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env: env })
|
const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env })
|
||||||
|
|
||||||
nuxtStart.stdout.on('data', data => {
|
nuxtStart.stdout.on('data', data => {
|
||||||
stdout += data
|
stdout += data
|
||||||
})
|
})
|
||||||
|
|
||||||
nuxtStart.stderr.on('data', data => {
|
|
||||||
// stderr += data
|
|
||||||
})
|
|
||||||
|
|
||||||
nuxtStart.on('error', err => {
|
nuxtStart.on('error', err => {
|
||||||
error = err
|
error = err
|
||||||
})
|
})
|
||||||
@ -51,37 +32,27 @@ describe.skip('cli', () => {
|
|||||||
exitCode = code
|
exitCode = code
|
||||||
})
|
})
|
||||||
|
|
||||||
// Give the process max 20s to start
|
// Wait max 20s for the starting
|
||||||
let iterator = 0
|
let timeout = await Utils.waitUntil(() => stdout.includes('Listening on'))
|
||||||
while (!stdout.includes('OPEN') && iterator < 80) {
|
|
||||||
await Utils.waitFor(250)
|
|
||||||
iterator++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (iterator === 80) {
|
if (timeout === true) {
|
||||||
test.log('WARN: server failed to start successfully in 20 seconds')
|
error = 'server failed to start successfully in 20 seconds'
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(error).toBe(undefined)
|
expect(error).toBe(undefined)
|
||||||
expect(stdout.includes('OPEN')).toBe(true)
|
expect(stdout.includes('Listening on')).toBe(true)
|
||||||
|
|
||||||
const html = await rp(url('/users/1'))
|
const html = await rp(url('/'))
|
||||||
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
|
expect(html).toMatch(('<div>CLI Test</div>'))
|
||||||
|
|
||||||
nuxtStart.kill()
|
nuxtStart.kill()
|
||||||
|
|
||||||
// Wait max 10s for the process to be killed
|
// Wait max 10s for the process to be killed
|
||||||
iterator = 0
|
timeout = await Utils.waitUntil(() => exitCode !== undefined, 10)
|
||||||
// eslint-disable-next-line no-unmodified-loop-condition
|
|
||||||
while (exitCode === undefined && iterator < 40) {
|
|
||||||
await Utils.waitFor(250)
|
|
||||||
iterator++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (iterator >= 40) {
|
if (timeout === true) {
|
||||||
// eslint-disable-line no-console
|
console.warn( // eslint-disable-line no-console
|
||||||
test.log(
|
`we were unable to automatically kill the child process with pid: ${
|
||||||
`WARN: we were unable to automatically kill the child process with pid: ${
|
|
||||||
nuxtStart.pid
|
nuxtStart.pid
|
||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
@ -89,10 +60,4 @@ describe.skip('cli', () => {
|
|||||||
|
|
||||||
expect(exitCode).toBe(null)
|
expect(exitCode).toBe(null)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('nuxt generate', async () => {
|
|
||||||
const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`)
|
|
||||||
|
|
||||||
expect(stdout.includes('vue-ssr-client-manifest.json')).toBe(true)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user