Skip to content

Commit 1201750

Browse files
committedOct 23, 2019
Update the test utils to hanlde other parsers
1 parent 1764942 commit 1201750

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed
 

‎src/testUtils.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
const fs = require('fs');
1313
const path = require('path');
1414

15-
function applyTransform(module, options, input) {
15+
function applyTransform(module, options, input, testOptions = {}) {
1616
// Handle ES6 modules using default export for the transform
1717
const transform = module.default ? module.default : module;
1818

1919
// Jest resets the module registry after each test, so we need to always get
2020
// a fresh copy of jscodeshift on every test run.
2121
let jscodeshift = require('./core');
22-
if (module.parser) {
23-
jscodeshift = jscodeshift.withParser(module.parser);
22+
if (testOptions.parser || module.parser) {
23+
jscodeshift = jscodeshift.withParser(testOptions.parser || module.parser);
2424
}
2525

2626
const output = transform(
@@ -43,13 +43,23 @@ function runSnapshotTest(module, options, input) {
4343
}
4444
exports.runSnapshotTest = runSnapshotTest;
4545

46-
function runInlineTest(module, options, input, expectedOutput) {
47-
const output = applyTransform(module, options, input);
46+
function runInlineTest(module, options, input, expectedOutput, testOptions) {
47+
const output = applyTransform(module, options, input, testOptions);
4848
expect(output).toEqual(expectedOutput.trim());
4949
return output;
5050
}
5151
exports.runInlineTest = runInlineTest;
5252

53+
function extensionForParser(parser) {
54+
switch (parser) {
55+
case 'ts':
56+
case 'tsx':
57+
return parser;
58+
default:
59+
return 'js'
60+
}
61+
}
62+
5363
/**
5464
* Utility function to run a jscodeshift script within a unit test. This makes
5565
* several assumptions about the environment:
@@ -69,38 +79,39 @@ exports.runInlineTest = runInlineTest;
6979
* - Test data should be located in a directory called __testfixtures__
7080
* alongside the transform and __tests__ directory.
7181
*/
72-
function runTest(dirName, transformName, options, testFilePrefix) {
82+
function runTest(dirName, transformName, options, testFilePrefix, testOptions = {}) {
7383
if (!testFilePrefix) {
7484
testFilePrefix = transformName;
7585
}
7686

87+
const extension = extensionForParser(testOptions.parser)
7788
const fixtureDir = path.join(dirName, '..', '__testfixtures__');
78-
const inputPath = path.join(fixtureDir, testFilePrefix + '.input.js');
89+
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
7990
const source = fs.readFileSync(inputPath, 'utf8');
8091
const expectedOutput = fs.readFileSync(
81-
path.join(fixtureDir, testFilePrefix + '.output.js'),
92+
path.join(fixtureDir, testFilePrefix + `.output.${extension}`),
8293
'utf8'
8394
);
8495
// Assumes transform is one level up from __tests__ directory
8596
const module = require(path.join(dirName, '..', transformName));
8697
runInlineTest(module, options, {
8798
path: inputPath,
8899
source
89-
}, expectedOutput);
100+
}, expectedOutput, testOptions);
90101
}
91102
exports.runTest = runTest;
92103

93104
/**
94105
* Handles some boilerplate around defining a simple jest/Jasmine test for a
95106
* jscodeshift transform.
96107
*/
97-
function defineTest(dirName, transformName, options, testFilePrefix) {
108+
function defineTest(dirName, transformName, options, testFilePrefix, testOptions) {
98109
const testName = testFilePrefix
99110
? `transforms correctly using "${testFilePrefix}" data`
100111
: 'transforms correctly';
101112
describe(transformName, () => {
102113
it(testName, () => {
103-
runTest(dirName, transformName, options, testFilePrefix);
114+
runTest(dirName, transformName, options, testFilePrefix, testOptions);
104115
});
105116
});
106117
}

0 commit comments

Comments
 (0)
Please sign in to comment.