Skip to content

Commit 08b8608

Browse files
authoredOct 29, 2019
Merge pull request #332 from skovy/skovy/support-parsers-in-test-utils
Support different parsers in the test utils
2 parents 8512b69 + 1201750 commit 08b8608

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const firstWord = 'Hello ';
2+
const secondWord = 'world';
3+
const message = firstWord + secondWord;
4+
5+
const getMessage = (): string => message
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const droWtsrif = 'Hello ';
2+
const droWdnoces = 'world';
3+
const egassem = droWtsrif + droWdnoces;
4+
5+
const egasseMteg = (): string => egassem

‎sample/__tests__/reverse-identifiers-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const transform = require('../reverse-identifiers');
2222

2323
defineTest(__dirname, 'reverse-identifiers');
2424

25+
defineTest(__dirname, 'reverse-identifiers', null, 'typescript/reverse-identifiers', { parser: 'ts' });
26+
2527
describe('reverse-identifiers', () => {
2628
defineInlineTest(transform, {}, `
2729
var firstWord = 'Hello ';

‎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.