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 🙏

© 2026 – Pkg Stats / Ryan Hefner

auto-react-test

v1.5.4

Published

Automatically generates test files for React components

Downloads

39

Readme

Auto React Test

A CLI tool to analyze React components for JSX elements, state, props, effects, and API calls, and auto-generate Jest tests.


Features

  • Detects JSX elements (buttons, inputs, text, etc.)
  • Detects component state and props
  • Detects useEffect hooks and API calls (fetch / axios)
  • Generates test files automatically using @testing-library/react
  • Supports data-testid for more reliable DOM queries
  • TypeScript compatible (generates .test.tsx)
  • Works with both Jest and Vitest

Installation

npm install -g auto-react-test
# or
yarn global add auto-react-test

Usage Example

auto-react-test src/components/Todos.tsx

Example Component (Todos.tsx)


import React, { useState, useEffect } from "react";

interface Todo {
  id: number;
  title: string;
}

const Todos: React.FC = () => {
  const [todos, setTodos] = useState<Todo[]>([]);
  const [input, setInput] = useState("");

  useEffect(() => {
    // Simulate API fetch
    fetch("https://jsonplaceholder.typicode.com/todos?_limit=3")
      .then((res) => res.json())
      .then((data) => {
        const simplified = data.map((item: any) => ({
          id: item.id,
          title: item.title,
        }));
        setTodos(simplified);
      });
  }, []);

  const addTodo = () => {
    if (input.trim()) {
      setTodos([...todos, { id: Date.now(), title: input }]);
      setInput("");
    }
  };

  return (
    <div>
      <h1 data-testid="heading">Todo List</h1>
      <input
        data-testid="todo-input"
        value={input}
        onChange={(e) => setInput(e.target.value)}
        placeholder="Add a todo"
      />
      <button data-testid="add-button" onClick={addTodo}>
        Add
      </button>
      <ul>
        {todos.map((todo) => (
          <li key={todo.id} data-testid="todo">
            {todo.title}
          </li>
        ))}
      </ul>
    </div>
  );
};


export default Todos;

Generated File (Todos.test.tsx)


import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import "@testing-library/jest-dom";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import Todos from "../Todos";

beforeEach(() => {
  // Safe default: no network; components expecting arrays get []
  vi.spyOn(globalThis as any, "fetch").mockResolvedValue({
    json: async () => [],
  } as any);
});

afterEach(() => {
  vi.restoreAllMocks();
});

describe("Todos component", () => {
  test("renders without crashing", () => {
    render(<Todos />);
    expect(true).toBe(true);
  });

  test("renders expected element #1", () => {
    render(<Todos />);
    const el = screen.getByTestId("heading");
    expect(el).toHaveTextContent(/Todo List/i);
  });

  test("renders expected element #2", () => {
    render(<Todos />);
    const el = screen.getByRole("button", { name: /Add/i });
    expect(el).toHaveTextContent(/Add/i);
  });

  test("updates input value on typing", async () => {
    const user = userEvent.setup();
    render(<Todos />);
    const input = screen.getByRole("textbox");
    const value = `test-${Date.now()}`;
    await user.type(input as HTMLElement, value);
    // @ts-expect-error HTMLElement may be HTMLInputElement at runtime
    expect((input as HTMLInputElement).value).toBe(value);
  });

  test("adds a new item on button click", async () => {
    const user = userEvent.setup();
    render(<Todos />);
    const input = screen.getByRole("textbox");
    const button = screen.getByRole("button");
    const value = `test-${Date.now()}`;
    await user.type(input as HTMLElement, value);
    await user.click(button);
    const items = screen.queryAllByTestId("user");
    if (items.length) {
      expect(items[items.length - 1]).toHaveTextContent(new RegExp(value, "i"));
    } else {
      // Fallback if no testids for items are present
      expect(screen.getByText(new RegExp(value, "i"))).toBeInTheDocument();
    }
  });

  test("loads data from fetch on mount", async () => {
    // Override default fetch mock for this test with an ARRAY (component expects array)
    (globalThis.fetch as any) = vi.fn(() => Promise.resolve({
      json: async () => [{"input":"Sample Input","addTodo":"Sample AddTodo"}],
    }));
    render(<Todos />);
    expect(globalThis.fetch).toHaveBeenCalled();
  });

});