1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | const fs = require('fs');
|
13 | const path = require('path');
|
14 |
|
15 | function applyTransform(module, options, input, testOptions = {}) {
|
16 |
|
17 | const transform = module.default ? module.default : module;
|
18 |
|
19 |
|
20 |
|
21 | let jscodeshift = require('./core');
|
22 | if (testOptions.parser || module.parser) {
|
23 | jscodeshift = jscodeshift.withParser(testOptions.parser || module.parser);
|
24 | }
|
25 |
|
26 | const output = transform(
|
27 | input,
|
28 | {
|
29 | jscodeshift,
|
30 | j: jscodeshift,
|
31 | stats: () => {},
|
32 | },
|
33 | options || {}
|
34 | );
|
35 |
|
36 | return (output || '').trim();
|
37 | }
|
38 | exports.applyTransform = applyTransform;
|
39 |
|
40 | function runSnapshotTest(module, options, input) {
|
41 | const output = applyTransform(module, options, input);
|
42 | expect(output).toMatchSnapshot();
|
43 | return output;
|
44 | }
|
45 | exports.runSnapshotTest = runSnapshotTest;
|
46 |
|
47 | function runInlineTest(module, options, input, expectedOutput, testOptions) {
|
48 | const output = applyTransform(module, options, input, testOptions);
|
49 | expect(output).toEqual(expectedOutput.trim());
|
50 | return output;
|
51 | }
|
52 | exports.runInlineTest = runInlineTest;
|
53 |
|
54 | function extensionForParser(parser) {
|
55 | switch (parser) {
|
56 | case 'ts':
|
57 | case 'tsx':
|
58 | return parser;
|
59 | default:
|
60 | return 'js'
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function runTest(dirName, transformName, options, testFilePrefix, testOptions = {}) {
|
84 | if (!testFilePrefix) {
|
85 | testFilePrefix = transformName;
|
86 | }
|
87 |
|
88 |
|
89 | const module = require(path.join(dirName, '..', transformName));
|
90 | const extension = extensionForParser(testOptions.parser || module.parser)
|
91 | const fixtureDir = path.join(dirName, '..', '__testfixtures__');
|
92 | const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
|
93 | const source = fs.readFileSync(inputPath, 'utf8');
|
94 | const expectedOutput = fs.readFileSync(
|
95 | path.join(fixtureDir, testFilePrefix + `.output.${extension}`),
|
96 | 'utf8'
|
97 | );
|
98 | runInlineTest(module, options, {
|
99 | path: inputPath,
|
100 | source
|
101 | }, expectedOutput, testOptions);
|
102 | }
|
103 | exports.runTest = runTest;
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | function defineTest(dirName, transformName, options, testFilePrefix, testOptions) {
|
110 | const testName = testFilePrefix
|
111 | ? `transforms correctly using "${testFilePrefix}" data`
|
112 | : 'transforms correctly';
|
113 | describe(transformName, () => {
|
114 | it(testName, () => {
|
115 | runTest(dirName, transformName, options, testFilePrefix, testOptions);
|
116 | });
|
117 | });
|
118 | }
|
119 | exports.defineTest = defineTest;
|
120 |
|
121 | function defineInlineTest(module, options, input, expectedOutput, testName) {
|
122 | it(testName || 'transforms correctly', () => {
|
123 | runInlineTest(module, options, {
|
124 | source: input
|
125 | }, expectedOutput);
|
126 | });
|
127 | }
|
128 | exports.defineInlineTest = defineInlineTest;
|
129 |
|
130 | function defineSnapshotTest(module, options, input, testName) {
|
131 | it(testName || 'transforms correctly', () => {
|
132 | runSnapshotTest(module, options, {
|
133 | source: input
|
134 | });
|
135 | });
|
136 | }
|
137 | exports.defineSnapshotTest = defineSnapshotTest;
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function defineSnapshotTestFromFixture(dirName, module, options, testFilePrefix, testName, testOptions = {}) {
|
143 | const extension = extensionForParser(testOptions.parser || module.parser)
|
144 | const fixtureDir = path.join(dirName, '..', '__testfixtures__');
|
145 | const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
|
146 | const source = fs.readFileSync(inputPath, 'utf8');
|
147 | defineSnapshotTest(module, options, source, testName)
|
148 | }
|
149 | exports.defineSnapshotTestFromFixture = defineSnapshotTestFromFixture;
|