-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-utils.js
33 lines (27 loc) · 1.13 KB
/
test-utils.js
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
import fs from 'fs';
import { join } from 'path';
import Promise from 'bluebird';
import * as diff from 'diff';
import chalk from 'chalk';
const readFile = Promise.promisify(fs.readFile);
const fixturesPath = name => join(__dirname, name, 'fixtures');
function getDiff(parts) {
return parts.map(part => {
const color = part.added ? 'green' : part.removed ? 'red' : 'grey'; // eslint-disable-line no-nested-ternary, max-len
return chalk[color](part.value);
}).join(' ');
}
export function generateTest(transformName, transform, testName) {
let fixtureName = `${fixturesPath(transformName)}/`;
if (testName) {
fixtureName = `${fixturesPath(transformName)}/${testName}.`;
}
return [testName || 'default', async t => {
const jscodeshift = require('jscodeshift'); // eslint-disable-line global-require
const path = `${fixtureName}input.js`;
const source = await readFile(path, 'utf8');
const expected = await readFile(`${fixtureName}output.js`, 'utf8');
const output = transform({ path, source }, { jscodeshift }, { quote: 'single' });
t.is(output, expected, getDiff(diff.diffLines(expected, output)));
}];
}