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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sum = (a, b) => a + b;
Lines changed: 11 additions & 0 deletions
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;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const addition = (a, b) => a + b;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sum = (a, b) => a + b;
Lines changed: 7 additions & 0 deletions
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;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const addition = (a, b) => a + b;
Lines changed: 13 additions & 0 deletions
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

Lines changed: 143 additions & 0 deletions
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+
});

0 commit comments

Comments
 (0)