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

rsc-test-helper

v0.1.4

Published

Allows passing in async/await RSC's to test renderers

Downloads

2,093

Readme

rsc-test-helper

Motivation

I've been messing around with Next JS's appDir beta lately on orbt and wanted to write some basic unit tests for some of our components. The beta utilizes RSC (async/await in server components), but you get a TS error any time you try to use an async server component in JSX (as the return type of the component is now a promise and no longer an element). NextJS knows how to handle the server components, so you're safe to just suppress the typescript error:

<Suspense fallback={<Skeleton />}>
  {/* @ts-expect-error Server Component */}
  <RoomChatParticipants />
</Suspense>

In tests though, this fails, as react test renderer create (and testing-library/react's render) will error out because they don't expect the component type to be an async function. I wrote this super dirty helper so that, until official support is added, I can still write some basic unit tests for pages that utilize async/await server components.

Usage

First, install it:
yarn add -D rsc-test-helper

Then import the patch function:
import { patch } from "rsc-test-helper";

Here's a basic example of a test that you would expect to work but doesn't:

import React from "react";
import { render, screen } from "@testing-library/react";
import RoomPage from "./page";

describe("Room Page", () => {
  it("renders chat box", () => {
    render(<RoomPage />);

    const chatBox = screen.getByTestId("chat-box");

    expect(chatBox).toBeInTheDocument();
  });
});

RoomPage doesn't utilize async/await directly, but it renders a child component that does:

export default function RoomPage() {
  return (
    ..
      <section className={styles.rightSection}>
        <RoomChatBox subheading={"Hello"} />
        <Suspense fallback={<Skeleton />}>
          {/* @ts-expect-error Server Component */}
          <RoomChatParticipants />
        </Suspense>
    ...
  );

Running the above test leads to this error:

 FAIL  src/app/room/page.test.tsx
  Room Page
    ✕ renders chat box (50 ms)

  ● Room Page › renders chat box

    Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.

Plugging in the helper, we get a test that actually completes:

import React from "react";
import { render, screen } from "@testing-library/react";
import RoomPage from "./page";
import { patch } from "rsc-test-helper";

describe("Room Page", () => {
  it("renders chat box", async () => {
    const Component = await patch(<RoomPage />);
    render(<Component />);

    const chatBox = screen.getByTestId("chat-box");

    expect(chatBox).toBeInTheDocument();
  });
});

 PASS  src/app/room/page.test.tsx
  Room Page
    ✓ renders chat box (542 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total

How it works

Traverses the tree and searches for ReactElement's with an async function type, if so, it awaits the promise, and replaces the async function in the original ReactElement with a sync function that just immediately returns the JSX.Element from the promise.

Caveats

There's no way to test the fallback state of <Suspense>, as the helper will await the promise (client-side fetching isn't yet official supported in the beta yet, so I introduced a 0.5s delay in the RoomChatParticipants component, which is why the test takes 500ms+ to run).

I haven't tested it a ton yet, code is pretty janky so I'm sure there are a lot of cases that aren't covered yet. I'll probably discover some cases as I continue to test out the beta, and update this helper if viable.

Another big caveat: don't think server-side-only code (like db reading/writing) will work? Might have to change testing env from jsdom to node? haven't messed around with testing this yet.