Skip to content

Commit 8f60fbf

Browse files
authored
Enable async tranformers in test utils (#646)
* refactor: enable async tranformers in test utils * chore: add changeset
1 parent db66545 commit 8f60fbf

11 files changed

+214
-6
lines changed

.changeset/polite-lizards-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"jscodeshift": patch
3+
---
4+
5+
Enable async tranformers in test utils.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ defineSnapshotTestFromFixture(__dirname, transform, transformOptions, 'FirstFixt
536536

537537
Executes your transform using the options and the input given and returns the result.
538538
This function is used internally by the other helpers, but it can prove useful in other cases.
539+
(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`).
539540

540541
```js
541542
const applyTransform = require('jscodeshift/dist/testUtils').applyTransform;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sum = (a, b) => a + b;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const synchronousTestTransform = (fileInfo, api, options) => {
2+
return new Promise(resolve => {
3+
setTimeout(() => {
4+
resolve(api.jscodeshift(fileInfo.source)
5+
.findVariableDeclarators('sum')
6+
.renameTo('addition')
7+
.toSource());
8+
}, 100);
9+
});
10+
}
11+
module.exports = synchronousTestTransform;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const addition = (a, b) => a + b;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sum = (a, b) => a + b;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const synchronousTestTransform = (fileInfo, api, options) => {
2+
return api.jscodeshift(fileInfo.source)
3+
.findVariableDeclarators('sum')
4+
.renameTo('addition')
5+
.toSource();
6+
}
7+
module.exports = synchronousTestTransform;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const addition = (a, b) => a + b;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`testUtils async should run async defineSnapshotTest 1`] = `"export const addition = (a, b) => a + b;"`;
4+
5+
exports[`testUtils async should run async defineSnapshotTestFromFixture 1`] = `"export const addition = (a, b) => a + b;"`;
6+
7+
exports[`testUtils async should run snapshot test 1`] = `"export const addition = (a, b) => a + b;"`;
8+
9+
exports[`testUtils synchronous should run snapshot test 1`] = `"export const addition = (a, b) => a + b;"`;
10+
11+
exports[`testUtils synchronous should run sync defineSnapshotTest 1`] = `"export const addition = (a, b) => a + b;"`;
12+
13+
exports[`testUtils synchronous should run sync defineSnapshotTestFromFixture 1`] = `"export const addition = (a, b) => a + b;"`;

src/__tests__/testUtils-test.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const fs = require('fs');
11+
const path = require('path');
12+
const testSyncTransform = require('../__testfixtures__/test-sync-transform');
13+
const testAsyncTransform = require('../__testfixtures__/test-async-transform');
14+
15+
const testUtils = require('../testUtils');
16+
17+
const testInputSource = 'export const sum = (a, b) => a + b;';
18+
const expectedInlineOutput = 'export const addition = (a, b) => a + b;';
19+
20+
const getModuleToTransform = () => {
21+
const moduleToTransformPath = path.join(__dirname, '..', '__testfixtures__', 'test-sync-transform.input.js');
22+
const source = fs.readFileSync(moduleToTransformPath, 'utf8');
23+
return {
24+
path: moduleToTransformPath,
25+
source,
26+
}
27+
}
28+
29+
describe('testUtils', () => {
30+
describe('synchronous', () => {
31+
it('should apply transformation', () => {
32+
const moduleToTransform = getModuleToTransform();
33+
const transformedCode = testUtils.applyTransform(testSyncTransform, null, moduleToTransform);
34+
35+
expect(transformedCode).not.toMatch(/sum/);
36+
expect(transformedCode).toMatch(/addition/);
37+
});
38+
39+
it('should run test', () => {
40+
testUtils.runTest(
41+
__dirname,
42+
path.join('__testfixtures__',
43+
'test-sync-transform'),
44+
null,
45+
'test-sync-transform'
46+
);
47+
});
48+
49+
it ('should run snapshot test', () => {
50+
const moduleToTransform = getModuleToTransform();
51+
testUtils.runSnapshotTest(testSyncTransform, null, moduleToTransform);
52+
});
53+
54+
it('should run inline test', () => {
55+
const moduleToTransform = getModuleToTransform();
56+
testUtils.runInlineTest(testSyncTransform, null, moduleToTransform, expectedInlineOutput);
57+
});
58+
59+
testUtils.defineTest(
60+
__dirname,
61+
path.join('__testfixtures__', 'test-sync-transform'),
62+
null,
63+
'test-sync-transform'
64+
);
65+
66+
testUtils.defineInlineTest(
67+
testSyncTransform,
68+
null,
69+
testInputSource,
70+
expectedInlineOutput,
71+
'should run sync defineInlineTest'
72+
);
73+
74+
testUtils.defineSnapshotTest(
75+
testSyncTransform,
76+
null,
77+
testInputSource,
78+
'should run sync defineSnapshotTest'
79+
);
80+
81+
testUtils.defineSnapshotTestFromFixture(
82+
__dirname,
83+
testSyncTransform,
84+
null,
85+
'test-sync-transform',
86+
'should run sync defineSnapshotTestFromFixture'
87+
);
88+
});
89+
90+
describe('async', () => {
91+
it('should apply transformation', async () => {
92+
const moduleToTransform = getModuleToTransform();
93+
const transformedCode = await testUtils.applyTransform(testAsyncTransform, null, moduleToTransform);
94+
95+
expect(transformedCode).not.toMatch(/sum/);
96+
expect(transformedCode).toMatch(/addition/);
97+
});
98+
99+
it('should run test', () => {
100+
return testUtils.runTest(__dirname, path.join('__testfixtures__', 'test-async-transform'), null, 'test-async-transform');
101+
});
102+
103+
it ('should run snapshot test', () => {
104+
const moduleToTransform = getModuleToTransform();
105+
return testUtils.runSnapshotTest(testAsyncTransform, null, moduleToTransform);
106+
});
107+
108+
it('should run inline test', () => {
109+
const moduleToTransform = getModuleToTransform();
110+
return testUtils.runInlineTest(testAsyncTransform, null, moduleToTransform, expectedInlineOutput);
111+
});
112+
113+
testUtils.defineTest(
114+
__dirname,
115+
path.join('__testfixtures__', 'test-async-transform'),
116+
null,
117+
'test-async-transform'
118+
);
119+
120+
testUtils.defineInlineTest(
121+
testAsyncTransform,
122+
null,
123+
testInputSource,
124+
expectedInlineOutput,
125+
'should run async defineInlineTest'
126+
);
127+
128+
testUtils.defineSnapshotTest(
129+
testAsyncTransform,
130+
null,
131+
testInputSource,
132+
'should run async defineSnapshotTest'
133+
);
134+
135+
testUtils.defineSnapshotTestFromFixture(
136+
__dirname,
137+
testSyncTransform,
138+
null,
139+
'test-async-transform',
140+
'should run async defineSnapshotTestFromFixture'
141+
);
142+
});
143+
});

src/testUtils.js

+30-6
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,38 @@ function applyTransform(module, options, input, testOptions = {}) {
3333
options || {}
3434
);
3535

36+
// Support async transforms
37+
if (output instanceof Promise) {
38+
return output.then(output => (output || '').trim());
39+
}
40+
3641
return (output || '').trim();
3742
}
3843
exports.applyTransform = applyTransform;
3944

4045
function runSnapshotTest(module, options, input) {
4146
const output = applyTransform(module, options, input);
47+
if (output instanceof Promise) {
48+
return output.then(output => {
49+
expect(output).toMatchSnapshot();
50+
return output;
51+
});
52+
}
4253
expect(output).toMatchSnapshot();
4354
return output;
4455
}
4556
exports.runSnapshotTest = runSnapshotTest;
4657

4758
function runInlineTest(module, options, input, expectedOutput, testOptions) {
4859
const output = applyTransform(module, options, input, testOptions);
49-
expect(output).toEqual(expectedOutput.trim());
60+
const expectation = (output => expect(output).toEqual(expectedOutput.trim()))
61+
if (output instanceof Promise) {
62+
return output.then(output => {
63+
expectation(output);
64+
return output;
65+
});
66+
}
67+
expectation(output);
5068
return output;
5169
}
5270
exports.runInlineTest = runInlineTest;
@@ -95,10 +113,12 @@ function runTest(dirName, transformName, options, testFilePrefix, testOptions =
95113
path.join(fixtureDir, testFilePrefix + `.output.${extension}`),
96114
'utf8'
97115
);
98-
runInlineTest(module, options, {
116+
const testResult = runInlineTest(module, options, {
99117
path: inputPath,
100118
source
101119
}, expectedOutput, testOptions);
120+
121+
return testResult instanceof Promise ? testResult : undefined;
102122
}
103123
exports.runTest = runTest;
104124

@@ -112,26 +132,29 @@ function defineTest(dirName, transformName, options, testFilePrefix, testOptions
112132
: 'transforms correctly';
113133
describe(transformName, () => {
114134
it(testName, () => {
115-
runTest(dirName, transformName, options, testFilePrefix, testOptions);
135+
const testResult = runTest(dirName, transformName, options, testFilePrefix, testOptions);
136+
return testResult instanceof Promise ? testResult : undefined;
116137
});
117138
});
118139
}
119140
exports.defineTest = defineTest;
120141

121142
function defineInlineTest(module, options, input, expectedOutput, testName) {
122143
it(testName || 'transforms correctly', () => {
123-
runInlineTest(module, options, {
144+
const testResult = runInlineTest(module, options, {
124145
source: input
125146
}, expectedOutput);
147+
return testResult instanceof Promise ? testResult : undefined;
126148
});
127149
}
128150
exports.defineInlineTest = defineInlineTest;
129151

130152
function defineSnapshotTest(module, options, input, testName) {
131153
it(testName || 'transforms correctly', () => {
132-
runSnapshotTest(module, options, {
154+
const testResult = runSnapshotTest(module, options, {
133155
source: input
134156
});
157+
return testResult instanceof Promise ? testResult : undefined;
135158
});
136159
}
137160
exports.defineSnapshotTest = defineSnapshotTest;
@@ -144,6 +167,7 @@ function defineSnapshotTestFromFixture(dirName, module, options, testFilePrefix,
144167
const fixtureDir = path.join(dirName, '..', '__testfixtures__');
145168
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
146169
const source = fs.readFileSync(inputPath, 'utf8');
147-
defineSnapshotTest(module, options, source, testName)
170+
const testResult = defineSnapshotTest(module, options, source, testName)
171+
return testResult instanceof Promise ? testResult : undefined;
148172
}
149173
exports.defineSnapshotTestFromFixture = defineSnapshotTestFromFixture;

0 commit comments

Comments
 (0)