Skip to content

Commit 9890da7

Browse files
authored
Allow config of act-ish method names (#16)
* Allow config of act-ish method names * Add docs * Remove unused method
1 parent 3fd0c4e commit 9890da7

File tree

8 files changed

+204
-100
lines changed

8 files changed

+204
-100
lines changed

.changeset/rude-bottles-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"codemod-missing-await-act": minor
3+
---
4+
5+
Allow config of method names

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ module.exports = {
1010
sourceType: "script",
1111
},
1212
rules: {},
13+
overrides: [
14+
{
15+
files: ["default-import-config.js"],
16+
parserOptions: { sourceType: "module" },
17+
rules: {
18+
"no-unused-vars": "off",
19+
},
20+
},
21+
],
1322
};

README.md

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,51 @@ test("focusing", async () => {
3939
});
4040
```
4141

42-
The following methods will be awaited when the codemod is applied:
43-
44-
- from `react`:
45-
- `unstable_act`
46-
- from `react-dom/test-utils`:
47-
- `act`
48-
- from `react-test-renderer`:
49-
- `act`
50-
- from `@testing-library/react`:
51-
- `act`
52-
- `cleanup`
53-
- `fireEvent`
54-
- `fireEvent.*`
55-
- `render`
56-
- `renderHook`
57-
5842
Right now we assume that any call to `rerender` and `unmount` should be awaited.
59-
These are all names of methods from React Testing Library.´
43+
44+
The following methods will be awaited when the codemod is applied by default:
45+
46+
```js
47+
// codemod-missing-await/default-iport-config.js
48+
// Import aliases have no effect on the codemod.
49+
// They're only used to not cause JS Syntax errors.
50+
// The codemod will only consider the imported name.
51+
import {
52+
act,
53+
cleanup,
54+
/**
55+
* @includeMemberCalls
56+
* e.g. fireEvent.click()
57+
*/
58+
fireEvent,
59+
render,
60+
renderHook,
61+
} from "@testing-library/react";
62+
import {
63+
act as act2,
64+
cleanup as cleanup2,
65+
/**
66+
* @includeMemberCalls
67+
* e.g. fireEvent.click()
68+
*/
69+
fireEvent as fireEvent2,
70+
render as render2,
71+
renderHook as renderHook2,
72+
} from "@testing-library/react/pure";
73+
import { unstable_act } from "react";
74+
import { act as act3 } from "react-dom/test-utils";
75+
import { act as act4 } from "react-test-renderer";
76+
```
77+
78+
You can add more methods if they're imported from somewhere by passing a JS file that only includes imports of those methods e.g. `npx codemod-missing-await-act ./src --import-config ./my-testing-library-imports.js` where `my-testing-library-imports.js` contains
79+
80+
```js
81+
import { renderWithProviders } from "@mycompany/testing-library";
82+
```
83+
84+
Now the codemod will also add an `await` to any `renderWithProviders` call imported from `@mycompany/testing-library`.
85+
86+
By default, the codemod uses the code listed in [codemod-missing-await/default-import-config.js](./default-import-config.js).
6087

6188
## Getting started
6289

@@ -80,19 +107,24 @@ $ npx codemod-missing-await-act
80107
codemod-missing-await-act <paths...>
81108

82109
Positionals:
83-
paths [string] [required]
110+
paths [string] [required]
84111

85112
Options:
86-
--version Show version number [boolean]
87-
--help Show help [boolean]
88-
--dry [boolean] [default: false]
89-
--ignore-pattern [string] [default: "**/node_modules/**"]
90-
--verbose [boolean] [default: false]
113+
--version Show version number [boolean]
114+
--help Show help [boolean]
115+
--dry [boolean] [default: false]
116+
--ignore-pattern [string] [default: "**/node_modules/**"]
117+
--import-config A path to a JS file importing all methods whose calls should
118+
be awaited. [string]
119+
--verbose [boolean] [default: false]
91120

92121
Examples:
93-
codemod-missing-await-act ./ Ignores `node_modules` and
94-
--ignore-pattern `build` folders
122+
codemod-missing-await-act ./ Ignores `node_modules` and `build`
123+
--ignore-pattern folders
95124
"**/{node_modules,build}/**"
125+
codemod-missing-await-act ./ Adds await to to all calls of
126+
--import-confg methods imported in that file.
127+
./missing-await-import-config.js
96128
```
97129

98130
## Limitations

bin/__tests__/codemod-missing-await-act.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ describe("codemod-missing-await-act", () => {
2828
--help Show help [boolean]
2929
--dry [boolean] [default: false]
3030
--ignore-pattern [string] [default: "**/node_modules/**"]
31+
--import-config A path to a JS file importing all methods whose calls should
32+
be awaited. [string]
3133
--verbose [boolean] [default: false]
3234
3335
Examples:
3436
codemod-missing-await-act ./ Ignores \`node_modules\` and \`build\`
3537
--ignore-pattern folders
3638
"**/{node_modules,build}/**"
39+
codemod-missing-await-act ./ Adds await to to all calls of
40+
--import-confg methods imported in that file.
41+
./missing-await-import-config.js
3742
",
3843
}
3944
`);

bin/codemod-missing-await-act.cjs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,30 @@ async function main() {
3030
default: "**/node_modules/**",
3131
type: "string",
3232
})
33+
.option("import-config", {
34+
description:
35+
"A path to a JS file importing all methods whose calls should be awaited.",
36+
type: "string",
37+
})
3338
.option("verbose", { default: false, type: "boolean" })
3439
// Ignoring `build`: https://www.digitalocean.com/community/tools/glob?comments=true&glob=%2A%2A%2F%7Bnode_modules%2Cbuild%7D%2F%2A%2A&matches=false&tests=package%2Fnode_modules%2Ftest.js&tests=package%2Fbuild%2Ftest.js&tests=package%2Ftest.js
3540
.example(
3641
'$0 ./ --ignore-pattern "**/{node_modules,build}/**"',
3742
"Ignores `node_modules` and `build` folders"
3843
)
44+
.example(
45+
"$0 ./ --import-confg ./missing-await-import-config.js",
46+
"Adds await to to all calls of methods imported in that file."
47+
)
3948
.demandOption(["paths"])
4049
);
4150
},
4251
async (argv) => {
43-
const { dry, paths, verbose } = argv;
52+
const { dry, importConfig: importConfigArg, paths, verbose } = argv;
53+
const importConfig =
54+
typeof importConfigArg === "string"
55+
? path.resolve(importConfigArg)
56+
: path.resolve(__dirname, "../default-import-config.js");
4457

4558
// TODO: npx instead?
4659
const jscodeshiftExecutable = require.resolve(
@@ -53,6 +66,7 @@ async function main() {
5366
const args = [
5467
"--extensions=js,jsx,mjs,cjs,ts,tsx,mts,cts",
5568
`"--ignore-pattern=${argv.ignorePattern}"`,
69+
`--importConfig=${importConfig}`,
5670
// The transforms are published as JS compatible with the supported Node.js versions.
5771
"--no-babel",
5872
`--transform ${path.join(transformsRoot, `${codemod}.js`)}`,

default-import-config.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Import aliases have no effect on the codemod.
2+
// They're only used to not cause JS Syntax errors in this file.
3+
// The codemod will only consider the imported name.
4+
import {
5+
act,
6+
cleanup,
7+
/**
8+
* @includeMemberCalls
9+
* e.g. fireEvent.click()
10+
*/
11+
fireEvent,
12+
render,
13+
renderHook,
14+
} from "@testing-library/react";
15+
import {
16+
act as act2,
17+
cleanup as cleanup2,
18+
/**
19+
* @includeMemberCalls
20+
* e.g. fireEvent.click()
21+
*/
22+
fireEvent as fireEvent2,
23+
render as render2,
24+
renderHook as renderHook2,
25+
} from "@testing-library/react/pure";
26+
import { unstable_act } from "react";
27+
import { act as act3 } from "react-dom/test-utils";
28+
import { act as act4 } from "react-test-renderer";

transforms/__tests__/codemod-missing-await-act.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const { afterEach, expect, jest, test } = require("@jest/globals");
22
const { default: dedent } = require("dedent-tabs");
33
const JscodeshiftTestUtils = require("jscodeshift/dist/testUtils");
4+
const path = require("path");
45
const codemodMissingAwaitTransform = require("../codemod-missing-await-act");
56

6-
function applyTransform(source, options = {}) {
7+
function applyTransform(
8+
source,
9+
options = {
10+
importConfig: path.resolve(__dirname, "../../default-import-config.js"),
11+
}
12+
) {
713
return JscodeshiftTestUtils.applyTransform(
814
codemodMissingAwaitTransform,
915
options,

0 commit comments

Comments
 (0)