Skip to content

Commit

Permalink
Updatable tests (thanks @DylanVann)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Sep 3, 2019
1 parent 329e351 commit 4f68ec9
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import test from 'ava'
import { sync } from 'glob'
import { readFile } from 'mz/fs'
import { readFile, writeFile } from 'mz/fs'
import { basename, resolve } from 'path'
import { compile } from '../src'

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

// Kind of hacky thing to figure out if -u or --update-snapshots was passed to AVA.
// We manually write expected output in this test.
// It's more clear, and allows us to have input and output files for each test.
const argvString = process.env.npm_config_argv as string
const argv = JSON.parse(argvString || '')
const argvOriginal = argv.original || []
const update =
argvOriginal.includes('-u') || argvOriginal.includes('--update-snapshots')

paths.forEach(path => {
// TODO: Why does glob catch tslint.json even with the trailing slash?
let folders = sync(resolve(__dirname, `../../test/${path}/*/`))
const folders = sync(resolve(__dirname, `../../test/${path}/*/`))
.filter(_ => !_.endsWith('.json'))
.filter(_ => !basename(_).startsWith('_'))

folders.forEach(folder =>
test(basename(folder), async t => {
const tests = folders.map(folder => ({ folder, name: basename(folder) }))

tests.forEach(({ name, folder }) =>
test(name, async t => {
try {
let filein = resolve(folder, 'input.txt')
let input = await readFile(filein, 'utf-8')
let output = await readFile(resolve(folder, 'output.txt'), 'utf-8')
t.is(await compile(input, filein), output)
const inputPath = resolve(folder, 'input.txt')
const outputPath = resolve(folder, 'output.txt')
const input = await readFile(inputPath, 'utf-8')
const output = await compile(input, inputPath)
if (update) {
await writeFile(outputPath, output)
t.pass()
} else {
const expectedOutput = await readFile(outputPath, 'utf-8')
t.is(output, expectedOutput)
}
} catch (e) {
console.log('error', e)
}
Expand Down

0 comments on commit 4f68ec9

Please sign in to comment.