Skip to content

Commit e962ee1

Browse files
committed
test: bring cli test back
1 parent 3a76475 commit e962ee1

5 files changed

Lines changed: 92 additions & 53 deletions

File tree

lib/common/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ export const waitFor = function waitFor(ms) {
1313
return new Promise(resolve => setTimeout(resolve, ms || 0))
1414
}
1515

16+
/**
17+
* Prepare an object to pass to the createSportsSelectionView function
18+
* @param {Function} condition return true to stop the waiting
19+
* @param {Number} duration seconds totally wait
20+
* @param {Number} interval milliseconds interval to check the condition
21+
*
22+
* @returns {Boolean} true: timeout, false: condition becomes true within total time
23+
*/
24+
export const waitUntil = async function waitUntil(condition, duration = 20, interval = 250) {
25+
let iterator = 0
26+
const steps = Math.floor(duration * 1000 / interval)
27+
28+
while (!condition() && iterator < steps) {
29+
await waitFor(interval) && iterator++
30+
}
31+
32+
if (iterator === steps) {
33+
return true
34+
}
35+
return false
36+
}
37+
1638
async function promiseFinally(fn, finalFn) {
1739
let result
1840
try {

test/fixtures/cli/cli.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { exec } from 'child_process'
2+
import { resolve } from 'path'
3+
import { promisify } from 'util'
4+
5+
const execify = promisify(exec)
6+
const rootDir = __dirname
7+
const nuxtBin = resolve(__dirname, '..', '..', '..', 'bin', 'nuxt')
8+
9+
describe('cli', () => {
10+
test('nuxt build', async () => {
11+
const { stdout } = await execify(`node ${nuxtBin} build ${rootDir}`)
12+
13+
expect(stdout.includes('Compiled successfully')).toBe(true)
14+
})
15+
16+
test('nuxt build -> error config', async () => {
17+
await expect(execify(`node ${nuxtBin} build ${rootDir} -c config.js`)).rejects.toMatchObject({
18+
stdout: expect.stringContaining('Could not load config file: config.js')
19+
})
20+
})
21+
22+
test('nuxt generate', async () => {
23+
const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`)
24+
25+
expect(stdout.includes('Generated successfully')).toBe(true)
26+
})
27+
})

test/fixtures/cli/nuxt.config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import consola from 'consola'
2+
3+
export default {
4+
hooks(hook) {
5+
hook('build:done', builder => {
6+
consola.success('Compiled successfully')
7+
})
8+
hook('generate:done', (generator, errors) => {
9+
if (!errors || errors.length === 0) {
10+
consola.success('Generated successfully')
11+
} else {
12+
consola.error('Generated failed')
13+
}
14+
})
15+
hook('listen', (server, { port, host }) => {
16+
consola.success(`Listening on http://${host}:${port}`)
17+
})
18+
},
19+
generate: {
20+
dir: '.nuxt-generate'
21+
}
22+
}

test/fixtures/cli/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>CLI Test</div>
3+
</template>

test/unit/cli.test.js

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,29 @@
1-
import { exec, spawn } from 'child_process'
1+
import { spawn } from 'child_process'
22
import { resolve } from 'path'
3-
import { promisify } from 'util'
4-
import { Utils, rp } from '../utils'
5-
6-
const execify = promisify(exec)
7-
const rootDir = resolve(__dirname, '..', 'fixtures/basic')
3+
import { getPort, Utils, rp } from '../utils'
84

95
let port
6+
const rootDir = resolve(__dirname, '..', 'fixtures/cli')
7+
108
const url = route => 'http://localhost:' + port + route
119

1210
const nuxtBin = resolve(__dirname, '..', '..', 'bin', 'nuxt')
1311

14-
describe.skip('cli', () => {
15-
test('nuxt build', async () => {
16-
const { stdout } = await execify(`node ${nuxtBin} build ${rootDir}`)
17-
18-
expect(stdout.includes('Compiled successfully')).toBe(true)
19-
})
20-
21-
test('nuxt build -> error config', async () => {
22-
await expect(execify(`node ${nuxtBin} build ${rootDir} -c config.js`)).rejects.toMatchObject({
23-
stderr: expect.stringContaining('Could not load config file')
24-
})
25-
})
26-
12+
describe('cli', () => {
2713
test('nuxt start', async () => {
2814
let stdout = ''
29-
// let stderr = ''
3015
let error
3116
let exitCode
3217

3318
const env = process.env
34-
env.PORT = port
19+
env.PORT = port = await getPort()
3520

36-
const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env: env })
21+
const nuxtStart = spawn('node', [nuxtBin, 'start', rootDir], { env })
3722

3823
nuxtStart.stdout.on('data', data => {
3924
stdout += data
4025
})
4126

42-
nuxtStart.stderr.on('data', data => {
43-
// stderr += data
44-
})
45-
4627
nuxtStart.on('error', err => {
4728
error = err
4829
})
@@ -51,48 +32,32 @@ describe.skip('cli', () => {
5132
exitCode = code
5233
})
5334

54-
// Give the process max 20s to start
55-
let iterator = 0
56-
while (!stdout.includes('OPEN') && iterator < 80) {
57-
await Utils.waitFor(250)
58-
iterator++
59-
}
35+
// Wait max 20s for the starting
36+
let timeout = await Utils.waitUntil(() => stdout.includes('Listening on'))
6037

61-
if (iterator === 80) {
62-
test.log('WARN: server failed to start successfully in 20 seconds')
38+
if (timeout === true) {
39+
error = 'server failed to start successfully in 20 seconds'
6340
}
6441

6542
expect(error).toBe(undefined)
66-
expect(stdout.includes('OPEN')).toBe(true)
43+
expect(stdout.includes('Listening on')).toBe(true)
6744

68-
const html = await rp(url('/users/1'))
69-
expect(html.includes('<h1>User: 1</h1>')).toBe(true)
45+
const html = await rp(url('/'))
46+
expect(html).toMatch(('<div>CLI Test</div>'))
7047

7148
nuxtStart.kill()
7249

7350
// Wait max 10s for the process to be killed
74-
iterator = 0
75-
// eslint-disable-next-line no-unmodified-loop-condition
76-
while (exitCode === undefined && iterator < 40) {
77-
await Utils.waitFor(250)
78-
iterator++
79-
}
51+
timeout = await Utils.waitUntil(() => exitCode !== undefined, 10)
8052

81-
if (iterator >= 40) {
82-
// eslint-disable-line no-console
83-
test.log(
84-
`WARN: we were unable to automatically kill the child process with pid: ${
53+
if (timeout === true) {
54+
console.warn( // eslint-disable-line no-console
55+
`we were unable to automatically kill the child process with pid: ${
8556
nuxtStart.pid
8657
}`
8758
)
8859
}
8960

9061
expect(exitCode).toBe(null)
9162
})
92-
93-
test('nuxt generate', async () => {
94-
const { stdout } = await execify(`node ${nuxtBin} generate ${rootDir}`)
95-
96-
expect(stdout.includes('vue-ssr-client-manifest.json')).toBe(true)
97-
})
9863
})

0 commit comments

Comments
 (0)