-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration-test-macro.ts
88 lines (84 loc) · 2.63 KB
/
integration-test-macro.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Macro, ExecutionContext } from 'ava'
import { ConfigName } from './shared'
import tempy from 'tempy'
import { spawnSync } from 'child_process'
import { resolve } from 'path'
import readPkgUp from 'read-pkg-up'
import { writeFileSync, mkdirSync, readFileSync, readdirSync } from 'fs'
export interface IntegrationTestInput {
name: ConfigName
additionalDependencies: string[]
srcFiles: Array<{
filename: string
contents: string
}>
expectedOutputFiles: Array<{
filename: string
expectedContents: string
}>
}
const srcDir = 'src'
const outDir = 'lib'
export const integrationTest: Macro<[IntegrationTestInput]> = (
t: ExecutionContext,
{ name, additionalDependencies, srcFiles, expectedOutputFiles }
) => {
const tmpDirPath = tempy.directory()
const pkgJson = readPkgUp.sync()
if (pkgJson === undefined) throw new Error()
if (pkgJson.packageJson === undefined) throw new Error()
if (pkgJson.packageJson.devDependencies === undefined) throw new Error()
const typescriptVersion = pkgJson.packageJson.devDependencies.typescript
const npmInstallCmd = spawnSync(
'npm',
[
'install',
'--loglevel', 'error',
'--no-save',
'--no-package-lock',
`typescript@${typescriptVersion}`,
...additionalDependencies,
resolve(__dirname, '..')
],
{ cwd: tmpDirPath }
)
if (npmInstallCmd.status !== 0) {
t.log(npmInstallCmd.stdout.toString())
t.log(npmInstallCmd.stderr.toString())
t.fail()
}
writeFileSync(
resolve(tmpDirPath, 'tsconfig.json'),
JSON.stringify({
extends: `tsconfigs/${name}`,
compilerOptions: { outDir },
include: [`${srcDir}/**/*`]
})
)
mkdirSync(resolve(tmpDirPath, srcDir))
srcFiles.forEach(({ filename, contents }) => {
writeFileSync(
resolve(tmpDirPath, srcDir, filename),
contents
)
})
const tscCmd = spawnSync('npx', ['tsc'], { cwd: tmpDirPath })
if (tscCmd.status !== 0) {
t.log(tscCmd.stdout.toString())
t.log(tscCmd.stderr.toString())
t.fail()
}
expectedOutputFiles.forEach(({ filename, expectedContents }) => {
const resultJs = readFileSync(resolve(tmpDirPath, outDir, filename))
t.is(resultJs.toString(), expectedContents)
})
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
const emittedFilenames = readdirSync(resolve(tmpDirPath, outDir))
.sort()
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
const expectedEmittedFilenames = expectedOutputFiles
.map(({ filename }) => filename)
.sort()
t.deepEqual(emittedFilenames, expectedEmittedFilenames)
}
integrationTest.title = () => 'integration'