-
Notifications
You must be signed in to change notification settings - Fork 26k
/
utils.ts
64 lines (57 loc) · 1.56 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @fileoverview
*
* This file contains utilities for `create-next-app` testing.
*/
import { ChildProcess, spawn, SpawnOptions } from 'child_process'
import { existsSync } from 'fs'
import { resolve } from 'path'
const cli = require.resolve('create-next-app/dist/index.js')
/**
* Run the built version of `create-next-app` with the given arguments.
*/
export const createNextApp = (args: string[], options?: SpawnOptions) => {
console.log(`[TEST] $ ${cli} ${args.join(' ')}`, { options })
return spawn('node', [cli].concat(args), options ?? {})
}
/**
* Return a Promise that resolves when the process exits with code 0 and rejects
* otherwise.
*/
export const spawnExitPromise = (childProcess: ChildProcess) => {
return new Promise((resolve, reject) => {
childProcess
.on('exit', (code) => {
if (code === 0) {
resolve(code)
} else {
reject(code)
}
})
.on('error', reject)
})
}
export const projectFilesShouldExist = (
projectRoot: string,
files: string[]
) => {
for (const file of files) {
expect(existsSync(resolve(projectRoot, file))).toBe(true)
}
}
export const projectFilesShouldntExist = (
projectRoot: string,
files: string[]
) => {
for (const file of files) {
expect(existsSync(resolve(projectRoot, file))).toBe(false)
}
}
export const projectDepsShouldBe = (
projectRoot: string,
depType: 'dependencies' | 'devDependencies',
value: any
) => {
const pkgJson = require(resolve(projectRoot, 'package.json'))
expect(Object.keys(pkgJson[depType])).toEqual(value)
}