Nuxt/test/dev/cli.test.js

83 lines
2.2 KiB
JavaScript
Raw Normal View History

import { resolve, join } from 'path'
import { spawn } from 'cross-spawn'
import { writeFileSync } from 'fs-extra'
import { getPort, rp, waitFor } from '../utils'
2018-05-15 11:45:09 +00:00
2018-05-16 07:10:20 +00:00
let port
const rootDir = resolve(__dirname, '..', 'fixtures/cli')
2023-10-16 21:23:38 +00:00
const url = route => 'http://127.0.0.1:' + port + route
const nuxtBin = resolve(__dirname, '../../packages/cli/bin/nuxt-cli.js')
const spawnNuxt = (command, opts) => spawn(nuxtBin, [command, rootDir], opts)
const start = (cmd, env, cb) => {
return new Promise((resolve) => {
const nuxt = spawnNuxt(cmd, { env, detached: true })
const listener = (data) => {
if (data.includes(`${port}`)) {
nuxt.stdout.removeListener('data', listener)
resolve(nuxt)
}
}
if (typeof cb === 'function') {
cb(nuxt)
}
nuxt.stdout.on('data', listener)
})
}
const close = (nuxt) => {
return new Promise((resolve) => {
nuxt.on('exit', resolve)
process.kill(-nuxt.pid)
})
}
describe.posix('cli', () => {
test.skip('nuxt dev', async () => {
const { env } = process
env.PORT = port = await getPort()
const nuxtDev = await start('dev', env)
// Change file specified in `watchers` (nuxt.config.js)
const customFilePath = join(rootDir, 'custom.file')
writeFileSync(customFilePath, 'This file is used to test custom chokidar watchers.')
// Change file specified in `serverMiddleware` (nuxt.config.js)
const serverMiddlewarePath = join(rootDir, 'middleware.js')
writeFileSync(serverMiddlewarePath, '// This file is used to test custom chokidar watchers.\n')
// Wait 2s for picking up changes
2018-10-24 13:53:34 +00:00
await waitFor(2000)
// [Add actual test for changes here]
2018-08-12 12:40:27 +00:00
await close(nuxtDev)
})
test.skip('nuxt start', async () => {
2018-03-18 19:31:32 +00:00
let error
const { env } = process
env.PORT = port = await getPort()
2018-03-18 19:31:32 +00:00
await new Promise((resolve) => {
const nuxtBuild = spawnNuxt('build', { env })
nuxtBuild.on('close', resolve)
2018-03-18 19:31:32 +00:00
})
const nuxtStart = await start('start', env, (nuxtStart) => {
nuxtStart.on('error', (err) => { error = err })
})
2018-03-18 19:31:32 +00:00
expect(error).toBe(undefined)
const { body: html } = await rp(url('/'))
expect(html).toMatch(('<div>CLI Test</div>'))
2018-03-18 19:31:32 +00:00
2018-08-12 12:40:27 +00:00
await close(nuxtStart)
})
})