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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-testgen

v1.0.4

Published

A generator that scaffolds unit tests for React components

Readme

react-testgen

React-testgen is a CLI tool to quickly generate unit test scaffolds for TypeScript-based React components.

Status

Abandoned due to lack of real applicability. I am not committed enough to push this from prototype to MVP.

Installation

You can either install it globally by running

npm i -g react-testgen

or use npx to execute it directly

npx react-testgen

Usage

react-testgen [path/to/file.tsx]

This will generate a 'file.test.tsx' adjacent to the specified file. If this file already exists, it will stop the generator without overwriting the existing file.

It currently supports React.Components, React.FC and other classes and functions that return JSX.Element. Used interfaces for props currently need to be included as part of the target file.

Example

For a simple component like this

import { FC } from "react";
import Child from "./Child";


interface ExampleProps {
    content: string
}

const Example: FC<ExampleProps> = (props) => {
    return (
        <div>
            Amazing content: {props.content}
            <Child />
        </div>
    )
}

export default Example

the following test file will be generated

import { render, screen } from "@testing-library/react"
import Example from "./Example"


describe("Example Tests", () => {
    beforeEach(() => {
        resetMocks()
    })

    test("Component renders", () => {
        whenComponentIsRendered()
        screen.debug()
    })
})

function resetMocks() {
    resetMockPropContent()
    resetMockChild()
}

let mockChild: jest.Mock
let mockChildReturn: JSX.Element
function resetMockChild() {
    mockChildReturn = <span data-testid={"mockChild"} />
    mockChild = jest.fn(() => mockChildReturn)
}
jest.mock("./Child", () => ({
    __esModule: true,
    default: (...args: any[]) => mockChild(args),
}))

let mockPropContent: string
function resetMockPropContent() {
    mockPropContent = "abcd"
}
function givenMockPropContent(given: string) {
    mockPropContent = given
}

function whenComponentIsRendered() {
    return render(
        <Example
            content={mockPropContent}
        />
    )
}

function thenChildIsRendered(expected: boolean) {
    if (expected) {
        expect(screen.getByTestId("mockChild")).toBeInTheDocument()
    } else {
        expect(screen.queryByTestId("mockChild")).toBeNull()
    }
}

function thenChildWasCalledWith(props: any) {
    expect(mockChild).toHaveBeenCalledWith([props])
}

This creates simple methods to write tests like the following

test("Child is rendered", () => {
    whenComponentIsRendered()
    thenChildIsRendered(true)
})

test("Prop is displayed", () => {
    givenMockPropContent("test string")
    whenComponentIsRendered()
    expect(screen.getByText("test string")).toBeInTheDocument()
})

Troubleshooting

Because react-testgen uses the TypeScript compiler via the amazing package ts-morph, the resulting test depends on the possible type inference through TypeScript. If the generated methods use unexpected types, it may help to add explicit type annotations.

If props, in specific, are not deconstructured correctly and you end up with a generated type "...Props: any", try adding the type annotation ": FC" or "extends Component<YourInterface, ...>" to the React component (using "FC/Component" instead of "React.FC/React.Component). As an alternative you can annotate the prop parameter "(props: YourInterface) => {}".

If your issues are not caused by a lack of type inference, feel free to open a issue/bug at GitHub.

Roadmap

  • More customizability via CLI arguments
  • Option to directly generate Snapshot tests
  • Handling parameters with Interface/Object types better

If you have some features that you would like to see added, please open a issue/enhancement at GitHub.

License

The MIT License