Skip to content

Commit

Permalink
Merge pull request #332 from skovy/skovy/support-parsers-in-test-utils
Browse files Browse the repository at this point in the history
Support different parsers in the test utils
  • Loading branch information
Daniel15 authored Oct 29, 2019
2 parents 8512b69 + 1201750 commit 08b8608
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const firstWord = 'Hello ';
const secondWord = 'world';
const message = firstWord + secondWord;

const getMessage = (): string => message
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const droWtsrif = 'Hello ';
const droWdnoces = 'world';
const egassem = droWtsrif + droWdnoces;

const egasseMteg = (): string => egassem
2 changes: 2 additions & 0 deletions sample/__tests__/reverse-identifiers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const transform = require('../reverse-identifiers');

defineTest(__dirname, 'reverse-identifiers');

defineTest(__dirname, 'reverse-identifiers', null, 'typescript/reverse-identifiers', { parser: 'ts' });

describe('reverse-identifiers', () => {
defineInlineTest(transform, {}, `
var firstWord = 'Hello ';
Expand Down
33 changes: 22 additions & 11 deletions src/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
const fs = require('fs');
const path = require('path');

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

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

const output = transform(
Expand All @@ -43,13 +43,23 @@ function runSnapshotTest(module, options, input) {
}
exports.runSnapshotTest = runSnapshotTest;

function runInlineTest(module, options, input, expectedOutput) {
const output = applyTransform(module, options, input);
function runInlineTest(module, options, input, expectedOutput, testOptions) {
const output = applyTransform(module, options, input, testOptions);
expect(output).toEqual(expectedOutput.trim());
return output;
}
exports.runInlineTest = runInlineTest;

function extensionForParser(parser) {
switch (parser) {
case 'ts':
case 'tsx':
return parser;
default:
return 'js'
}
}

/**
* Utility function to run a jscodeshift script within a unit test. This makes
* several assumptions about the environment:
Expand All @@ -69,38 +79,39 @@ exports.runInlineTest = runInlineTest;
* - Test data should be located in a directory called __testfixtures__
* alongside the transform and __tests__ directory.
*/
function runTest(dirName, transformName, options, testFilePrefix) {
function runTest(dirName, transformName, options, testFilePrefix, testOptions = {}) {
if (!testFilePrefix) {
testFilePrefix = transformName;
}

const extension = extensionForParser(testOptions.parser)
const fixtureDir = path.join(dirName, '..', '__testfixtures__');
const inputPath = path.join(fixtureDir, testFilePrefix + '.input.js');
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
const source = fs.readFileSync(inputPath, 'utf8');
const expectedOutput = fs.readFileSync(
path.join(fixtureDir, testFilePrefix + '.output.js'),
path.join(fixtureDir, testFilePrefix + `.output.${extension}`),
'utf8'
);
// Assumes transform is one level up from __tests__ directory
const module = require(path.join(dirName, '..', transformName));
runInlineTest(module, options, {
path: inputPath,
source
}, expectedOutput);
}, expectedOutput, testOptions);
}
exports.runTest = runTest;

/**
* Handles some boilerplate around defining a simple jest/Jasmine test for a
* jscodeshift transform.
*/
function defineTest(dirName, transformName, options, testFilePrefix) {
function defineTest(dirName, transformName, options, testFilePrefix, testOptions) {
const testName = testFilePrefix
? `transforms correctly using "${testFilePrefix}" data`
: 'transforms correctly';
describe(transformName, () => {
it(testName, () => {
runTest(dirName, transformName, options, testFilePrefix);
runTest(dirName, transformName, options, testFilePrefix, testOptions);
});
});
}
Expand Down

0 comments on commit 08b8608

Please sign in to comment.