-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefineTest.js
58 lines (56 loc) · 1.42 KB
/
defineTest.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
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
jest.autoMockOff();
const path = require("path");
const fs = require("fs");
const runInlineTest = require("jscodeshift/dist/testUtils").runInlineTest;
export default function defineTest(
dirName,
transformName,
options,
testFilePrefix,
testOptions = {}
) {
testFilePrefix = Array.isArray(testFilePrefix)
? path.join(...testFilePrefix)
: testFilePrefix;
const fixtureDir = path.join(dirName, "..", "__testfixtures__");
const fileExtension = testOptions.parser || "js";
const inputPath = path.join(
fixtureDir,
testFilePrefix + `.input.` + fileExtension
);
const outputPath = path.join(
fixtureDir,
testFilePrefix + `.output.` + fileExtension
);
const source = fs.readFileSync(inputPath, "utf8");
const expectedOutput = fs.readFileSync(outputPath, "utf8");
const transform = require(path.join(dirName, "..", transformName));
describe(transformName, () => {
describe(testFilePrefix, () => {
it("transforms correctly", () => {
runInlineTest(
transform,
options,
{
path: inputPath,
source,
},
expectedOutput,
testOptions
);
});
it("is idempotent", () => {
runInlineTest(
transform,
options,
{
path: outputPath,
source: expectedOutput,
},
"",
testOptions
);
});
});
});
}