Skip to content

Commit

Permalink
Merge pull request #8 from ElonVolo/SaveUnitTestTransformedOutput
Browse files Browse the repository at this point in the history
Adding ability to save actual test output
  • Loading branch information
ElonVolo authored Mar 10, 2022
2 parents c0b7973 + e97c9b5 commit 5bc20bb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## evcodeshift [2.2.1] 2022-03-09

### Added

- A `saveoutput` parameter to the defineTest() function that allows the results of the transformations to be be saved to a file so they can be more easily be inspected by a text editor or other tools.

### Changed

- Switched from `recast` to `@putout/recast` as the @coderaiser's fork seems to be more recently maintained than the `recast` upstream, and also because `@output/recast` fixed a bug introdcued by `recast@0.21.0` where transformations that specificied single quotes where still outputting with double quotes.
Expand Down
12 changes: 12 additions & 0 deletions sample/__tests__/reverse-identifiers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ defineTest(__dirname, 'reverse-identifiers');

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

// defineTest(__dirname, 'reverse-identifiers', null, 'typescript/reverse-identifiers', { parser: 'ts', saveoutput: true });
//
// The commented out test above would run the same as the uncommented one above it, but passing in value true for saveoutput will create
// the following directory structure in the project root
//
// unit_test_output
// /reverse-identifiers
// /reverse-identifiers.input.js # The original code given to the unit test
// /reverse-identifiers.output.js # The output that the unit test is expecting to see if all goes well
// /reverse-identifiers.transformed.js # The actual result of running the transformation (may different from output if unit test is failing)
//

describe('reverse-identifiers', () => {
defineInlineTest(transform, {}, `
var firstWord = 'Hello ';
Expand Down
37 changes: 33 additions & 4 deletions src/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');

function applyTransform(module, options, input, testOptions = {}) {
// Handle ES6 modules using default export for the transform
Expand All @@ -32,7 +33,8 @@ function applyTransform(module, options, input, testOptions = {}) {
options || {}
);

return (output || '').trim();
const trimmedOutput = (output || '').trim();
return trimmedOutput;
}
exports.applyTransform = applyTransform;

Expand All @@ -45,6 +47,11 @@ exports.runSnapshotTest = runSnapshotTest;

function runInlineTest(module, options, input, expectedOutput, testOptions) {
const output = applyTransform(module, options, input, testOptions);

if (testOptions && testOptions.saveoutput) {
fs.writeFileSync(testOptions.transformedOutputPath, output, 'utf-8');
}

expect(output).toEqual(expectedOutput.trim());
return output;
}
Expand Down Expand Up @@ -86,15 +93,37 @@ function runTest(dirName, transformName, options, testFilePrefix, testOptions =

const extension = extensionForParser(testOptions.parser)
const fixtureDir = path.join(dirName, '..', '__testfixtures__');
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);

const inputFilename = testFilePrefix + `.input.${extension}`;
const outputFileName = testFilePrefix + `.output.${extension}`;

const inputPath = path.join(fixtureDir, inputFilename);
const outputPath = path.join(fixtureDir, outputFileName );

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

if (testOptions && testOptions.saveoutput) {
let debugOutputSavePath = path.join(process.cwd(), 'unit_test_output', testFilePrefix);
mkdirp.sync(debugOutputSavePath);

let inputFileContentPath = path.join(debugOutputSavePath, inputFilename);
fs.writeFileSync(inputFileContentPath, source, 'utf-8');

let expectedOutputContentPath = path.join(debugOutputSavePath, outputFileName);
fs.writeFileSync(expectedOutputContentPath, expectedOutput, 'utf-8');

let fileExtension = extensionForParser(testOptions.parser);
let transformedContentPath = path.join(debugOutputSavePath, `${testFilePrefix}.tranformed.${fileExtension}`);
testOptions.transformedOutputPath = transformedContentPath;
}

const transformedContent = runInlineTest(module, options, {
path: inputPath,
source
}, expectedOutput, testOptions);
Expand Down

0 comments on commit 5bc20bb

Please sign in to comment.