Skip to content

Commit

Permalink
Enable async tranformers in test utils (#646)
Browse files Browse the repository at this point in the history
* refactor: enable async tranformers in test utils

* chore: add changeset
  • Loading branch information
PaquitoSoft authored Jan 10, 2025
1 parent db66545 commit 8f60fbf
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-lizards-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"jscodeshift": patch
---

Enable async tranformers in test utils.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ defineSnapshotTestFromFixture(__dirname, transform, transformOptions, 'FirstFixt

Executes your transform using the options and the input given and returns the result.
This function is used internally by the other helpers, but it can prove useful in other cases.
(bear in mind the `transform` module can be asynchronous. In that case, `applyTransform` will return a `Promise` with the transformed code. Otherwise, it will directly return the transformed code as a `string`).

```js
const applyTransform = require('jscodeshift/dist/testUtils').applyTransform;
Expand Down
1 change: 1 addition & 0 deletions src/__testfixtures__/test-async-transform.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sum = (a, b) => a + b;
11 changes: 11 additions & 0 deletions src/__testfixtures__/test-async-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const synchronousTestTransform = (fileInfo, api, options) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(api.jscodeshift(fileInfo.source)
.findVariableDeclarators('sum')
.renameTo('addition')
.toSource());
}, 100);
});
}
module.exports = synchronousTestTransform;
1 change: 1 addition & 0 deletions src/__testfixtures__/test-async-transform.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const addition = (a, b) => a + b;
1 change: 1 addition & 0 deletions src/__testfixtures__/test-sync-transform.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sum = (a, b) => a + b;
7 changes: 7 additions & 0 deletions src/__testfixtures__/test-sync-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const synchronousTestTransform = (fileInfo, api, options) => {
return api.jscodeshift(fileInfo.source)
.findVariableDeclarators('sum')
.renameTo('addition')
.toSource();
}
module.exports = synchronousTestTransform;
1 change: 1 addition & 0 deletions src/__testfixtures__/test-sync-transform.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const addition = (a, b) => a + b;
13 changes: 13 additions & 0 deletions src/__tests__/__snapshots__/testUtils-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`testUtils async should run async defineSnapshotTest 1`] = `"export const addition = (a, b) => a + b;"`;

exports[`testUtils async should run async defineSnapshotTestFromFixture 1`] = `"export const addition = (a, b) => a + b;"`;

exports[`testUtils async should run snapshot test 1`] = `"export const addition = (a, b) => a + b;"`;

exports[`testUtils synchronous should run snapshot test 1`] = `"export const addition = (a, b) => a + b;"`;

exports[`testUtils synchronous should run sync defineSnapshotTest 1`] = `"export const addition = (a, b) => a + b;"`;

exports[`testUtils synchronous should run sync defineSnapshotTestFromFixture 1`] = `"export const addition = (a, b) => a + b;"`;
143 changes: 143 additions & 0 deletions src/__tests__/testUtils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const fs = require('fs');
const path = require('path');
const testSyncTransform = require('../__testfixtures__/test-sync-transform');
const testAsyncTransform = require('../__testfixtures__/test-async-transform');

const testUtils = require('../testUtils');

const testInputSource = 'export const sum = (a, b) => a + b;';
const expectedInlineOutput = 'export const addition = (a, b) => a + b;';

const getModuleToTransform = () => {
const moduleToTransformPath = path.join(__dirname, '..', '__testfixtures__', 'test-sync-transform.input.js');
const source = fs.readFileSync(moduleToTransformPath, 'utf8');
return {
path: moduleToTransformPath,
source,
}
}

describe('testUtils', () => {
describe('synchronous', () => {
it('should apply transformation', () => {
const moduleToTransform = getModuleToTransform();
const transformedCode = testUtils.applyTransform(testSyncTransform, null, moduleToTransform);

expect(transformedCode).not.toMatch(/sum/);
expect(transformedCode).toMatch(/addition/);
});

it('should run test', () => {
testUtils.runTest(
__dirname,
path.join('__testfixtures__',
'test-sync-transform'),
null,
'test-sync-transform'
);
});

it ('should run snapshot test', () => {
const moduleToTransform = getModuleToTransform();
testUtils.runSnapshotTest(testSyncTransform, null, moduleToTransform);
});

it('should run inline test', () => {
const moduleToTransform = getModuleToTransform();
testUtils.runInlineTest(testSyncTransform, null, moduleToTransform, expectedInlineOutput);
});

testUtils.defineTest(
__dirname,
path.join('__testfixtures__', 'test-sync-transform'),
null,
'test-sync-transform'
);

testUtils.defineInlineTest(
testSyncTransform,
null,
testInputSource,
expectedInlineOutput,
'should run sync defineInlineTest'
);

testUtils.defineSnapshotTest(
testSyncTransform,
null,
testInputSource,
'should run sync defineSnapshotTest'
);

testUtils.defineSnapshotTestFromFixture(
__dirname,
testSyncTransform,
null,
'test-sync-transform',
'should run sync defineSnapshotTestFromFixture'
);
});

describe('async', () => {
it('should apply transformation', async () => {
const moduleToTransform = getModuleToTransform();
const transformedCode = await testUtils.applyTransform(testAsyncTransform, null, moduleToTransform);

expect(transformedCode).not.toMatch(/sum/);
expect(transformedCode).toMatch(/addition/);
});

it('should run test', () => {
return testUtils.runTest(__dirname, path.join('__testfixtures__', 'test-async-transform'), null, 'test-async-transform');
});

it ('should run snapshot test', () => {
const moduleToTransform = getModuleToTransform();
return testUtils.runSnapshotTest(testAsyncTransform, null, moduleToTransform);
});

it('should run inline test', () => {
const moduleToTransform = getModuleToTransform();
return testUtils.runInlineTest(testAsyncTransform, null, moduleToTransform, expectedInlineOutput);
});

testUtils.defineTest(
__dirname,
path.join('__testfixtures__', 'test-async-transform'),
null,
'test-async-transform'
);

testUtils.defineInlineTest(
testAsyncTransform,
null,
testInputSource,
expectedInlineOutput,
'should run async defineInlineTest'
);

testUtils.defineSnapshotTest(
testAsyncTransform,
null,
testInputSource,
'should run async defineSnapshotTest'
);

testUtils.defineSnapshotTestFromFixture(
__dirname,
testSyncTransform,
null,
'test-async-transform',
'should run async defineSnapshotTestFromFixture'
);
});
});
36 changes: 30 additions & 6 deletions src/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,38 @@ function applyTransform(module, options, input, testOptions = {}) {
options || {}
);

// Support async transforms
if (output instanceof Promise) {
return output.then(output => (output || '').trim());
}

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

function runSnapshotTest(module, options, input) {
const output = applyTransform(module, options, input);
if (output instanceof Promise) {
return output.then(output => {
expect(output).toMatchSnapshot();
return output;
});
}
expect(output).toMatchSnapshot();
return output;
}
exports.runSnapshotTest = runSnapshotTest;

function runInlineTest(module, options, input, expectedOutput, testOptions) {
const output = applyTransform(module, options, input, testOptions);
expect(output).toEqual(expectedOutput.trim());
const expectation = (output => expect(output).toEqual(expectedOutput.trim()))
if (output instanceof Promise) {
return output.then(output => {
expectation(output);
return output;
});
}
expectation(output);
return output;
}
exports.runInlineTest = runInlineTest;
Expand Down Expand Up @@ -95,10 +113,12 @@ function runTest(dirName, transformName, options, testFilePrefix, testOptions =
path.join(fixtureDir, testFilePrefix + `.output.${extension}`),
'utf8'
);
runInlineTest(module, options, {
const testResult = runInlineTest(module, options, {
path: inputPath,
source
}, expectedOutput, testOptions);

return testResult instanceof Promise ? testResult : undefined;
}
exports.runTest = runTest;

Expand All @@ -112,26 +132,29 @@ function defineTest(dirName, transformName, options, testFilePrefix, testOptions
: 'transforms correctly';
describe(transformName, () => {
it(testName, () => {
runTest(dirName, transformName, options, testFilePrefix, testOptions);
const testResult = runTest(dirName, transformName, options, testFilePrefix, testOptions);
return testResult instanceof Promise ? testResult : undefined;
});
});
}
exports.defineTest = defineTest;

function defineInlineTest(module, options, input, expectedOutput, testName) {
it(testName || 'transforms correctly', () => {
runInlineTest(module, options, {
const testResult = runInlineTest(module, options, {
source: input
}, expectedOutput);
return testResult instanceof Promise ? testResult : undefined;
});
}
exports.defineInlineTest = defineInlineTest;

function defineSnapshotTest(module, options, input, testName) {
it(testName || 'transforms correctly', () => {
runSnapshotTest(module, options, {
const testResult = runSnapshotTest(module, options, {
source: input
});
return testResult instanceof Promise ? testResult : undefined;
});
}
exports.defineSnapshotTest = defineSnapshotTest;
Expand All @@ -144,6 +167,7 @@ function defineSnapshotTestFromFixture(dirName, module, options, testFilePrefix,
const fixtureDir = path.join(dirName, '..', '__testfixtures__');
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
const source = fs.readFileSync(inputPath, 'utf8');
defineSnapshotTest(module, options, source, testName)
const testResult = defineSnapshotTest(module, options, source, testName)
return testResult instanceof Promise ? testResult : undefined;
}
exports.defineSnapshotTestFromFixture = defineSnapshotTestFromFixture;

0 comments on commit 8f60fbf

Please sign in to comment.