npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

codemod-missing-await-act

v0.1.0

Published

Adds missing `await` to `act` calls or methods that like contain an `act` call using [jscodeshift](https://github.com/facebook/jscodeshift). We all track usage of these methods throughout the file. For example, given

Downloads

7

Readme

codemod-missing-await-act

Adds missing await to act calls or methods that like contain an act call using jscodeshift. We all track usage of these methods throughout the file. For example, given

import { act } from "react-dom/test-utils";

function focus(element) {
	act(() => {
		element.focus();
	});
}

test("focusing", () => {
	const { container } = render("<button />");

	focus(container);
});

will add an await to act and also add await to focus since focus is now an async method. The end result will be

import { act } from "react-dom/test-utils";

async function focus(element) {
	await act(() => {
		element.focus();
	});
}

test("focusing", async () => {
	const { container } = await render("<button />");

	await focus(container);
});

The following methods will be awaited when the codemod is applied:

  • from react:
    • unstable_act
  • from react-dom/test-utils:
    • act
  • from react-test-renderer:
    • act
  • from @testing-library/react:
    • act
    • cleanup
    • fireEvent
    • fireEvent.*
    • render
    • renderHook

Right now we assume that any call to rerender and unmount should be awaited. These are all names of methods from React Testing Library.´

Getting started

$ npx codemod-missing-await-act ./src
Processing 4 files...
All done.
Results:
0 errors
3 unmodified
0 skipped
1 ok
Time elapsed: 0.428seconds

Usage

$ npx codemod-missing-await-act

codemod-missing-await-act <paths...>

Positionals:
  paths                                               [string] [required]

Options:
  --version         Show version number                         [boolean]
  --help            Show help                                   [boolean]
  --dry                                        [boolean] [default: false]
  --ignore-pattern               [string] [default: "**/node_modules/**"]
  --verbose                                    [boolean] [default: false]

Examples:
  codemod-missing-await-act ./              Ignores `node_modules` and
  --ignore-pattern                      `build` folders
  "**/{node_modules,build}/**"

Limitations

Forces call expressions to be awaited

const acting = act(scope);
await anotherPromise;
await acting;

will add await to the act call.

This codemod is targetted at act calls. They're not allowed to be interleaved anyway so the original code was already problematic since it could also contain an act call. I don't think this is common in practice (TODO: check if we use this pattern).

async class methods are not propagated

We don't track references to class methods. Only references to (arrow-) function declarations or expressions are tracked.

class MyTestUtils {
	render() {
		act(scope);
	}
}

const utils = new MyTestUtils();

utils.render();

will be transformed to

class MyTestUtils {
	async render() {
		await act(scope);
	}
}

const utils = new MyTestUtils();

// no `await` added
utils.render();

Supported platforms

The following list contains officially supported runtimes. Please file an issue for runtimes that are not included in this list.

  • Node.js 14.x || 16.x || 18.x || 19.x || 20.x