Skip to content

Commit 4f68ec9

Browse files
committed
Updatable tests (thanks @DylanVann)
1 parent 329e351 commit 4f68ec9

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

test/test.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
import test from 'ava'
22
import { sync } from 'glob'
3-
import { readFile } from 'mz/fs'
3+
import { readFile, writeFile } from 'mz/fs'
44
import { basename, resolve } from 'path'
55
import { compile } from '../src'
66

77
let paths = ['e2e', 'rules', 'unit']
88

9+
// Kind of hacky thing to figure out if -u or --update-snapshots was passed to AVA.
10+
// We manually write expected output in this test.
11+
// It's more clear, and allows us to have input and output files for each test.
12+
const argvString = process.env.npm_config_argv as string
13+
const argv = JSON.parse(argvString || '')
14+
const argvOriginal = argv.original || []
15+
const update =
16+
argvOriginal.includes('-u') || argvOriginal.includes('--update-snapshots')
17+
918
paths.forEach(path => {
1019
// TODO: Why does glob catch tslint.json even with the trailing slash?
11-
let folders = sync(resolve(__dirname, `../../test/${path}/*/`))
20+
const folders = sync(resolve(__dirname, `../../test/${path}/*/`))
1221
.filter(_ => !_.endsWith('.json'))
1322
.filter(_ => !basename(_).startsWith('_'))
1423

15-
folders.forEach(folder =>
16-
test(basename(folder), async t => {
24+
const tests = folders.map(folder => ({ folder, name: basename(folder) }))
25+
26+
tests.forEach(({ name, folder }) =>
27+
test(name, async t => {
1728
try {
18-
let filein = resolve(folder, 'input.txt')
19-
let input = await readFile(filein, 'utf-8')
20-
let output = await readFile(resolve(folder, 'output.txt'), 'utf-8')
21-
t.is(await compile(input, filein), output)
29+
const inputPath = resolve(folder, 'input.txt')
30+
const outputPath = resolve(folder, 'output.txt')
31+
const input = await readFile(inputPath, 'utf-8')
32+
const output = await compile(input, inputPath)
33+
if (update) {
34+
await writeFile(outputPath, output)
35+
t.pass()
36+
} else {
37+
const expectedOutput = await readFile(outputPath, 'utf-8')
38+
t.is(output, expectedOutput)
39+
}
2240
} catch (e) {
2341
console.log('error', e)
2442
}

0 commit comments

Comments
 (0)