Early package! We are looking for beginner Open Source contributors! ❤️
A complete and expressive testing library for React:
import $ from "react-test";
it("increments when clicked", async () => {
const counter = $(<Counter />);
expect(counter.text()).toEqual("0");
await counter.click();
expect(counter.text()).toEqual("1");
});
The react-test
syntax follows a similar schema to jQuery so it's very easy to write expressive tests. The best way to test declarative code is with an imperative library.
You need a React project already working. That's on you, but we recommend Create React App:
npx create-react-app my-app
cd my-app
Then you install react-test
, only for development:
npm install react-test --save-dev
Now you can write tests, let's say you have this component:
// src/Counter.js
import React, { useState } from "react";
export default function Counter () {
const [count, setCount] = useState(0);
const increment = () => setCount(count + 1);
return <button onClick={increment}>{count}</button>;
};
You can write the following test to make sure it's working:
// src/Counter.test.js
import React from "react";
import $ from "react-test";
import Counter from "./Counter";
describe("Counter.js", () => {
it("is initialized to 0", () => {
const counter = $(<Counter />);
expect(counter.text()).toBe("0");
});
it("can be incremented with a click", async () => {
const counter = $(<Counter />);
await counter.click();
expect(counter.text()).toBe("1");
});
it("can be incremented multiple times", async () => {
const counter = $(<Counter />);
await counter.click();
await counter.click();
await counter.click();
expect(counter.text()).toBe("3");
});
});
Finally run the tests with Jest:
npm run test
TODO: add some intro to testing and best practices here
No. This follows the community convention of calling a library related to React as react-NAME
. It is made by these contributors without any involvement of Facebook or React.
Thanks! Please read the Contributing Guide where we explain how to get started with the project. Right now there are some beginner-friendly issues so please feel free to implement those!
I will try to help as much as possible on the PRs.
Don't sweat it, just open an issue. React Test is in an early phase with incomplete documentation so feel free to read the code or ask directly in the issues.
This will change once the library is more stable, there's more documentation and if the community grows (maybe a chat, or reddit group, or ...).
I've written a blog post about this, but the gist of it is that the npm package was taken by Deepstream.io before but not used. So I asked politely and they allowed me to use it.
How is this different from React Testing Library?
This is a difficult one. First, React Testing Library, the documentation and the work from @kentcdodds and other collaborators is amazing and I've learned a lot from it. The main differences are:
The syntax follows jQuery-style chaining:
// react-test
import $ from "react-test";
test("Increments when clicked", async () => {
const $counter = $(<Counter />);
expect($counter).toHaveText("0");
await $counter.click();
expect($counter).toHaveText("1");
});
// react testing library
import { render, fireEvent } from "@testing-library/react"
test("Increments when clicked", () => {
const { getByRole, container } = render(<Counter />);
expect(container).toHaveTextContent("0");
fireEvent.click(getByRole("button"));
expect(container).toHaveTextContent("1");
});
React Test is a work in progress, so if you are writing tests for production right now please use one of the better known alternatives.
That's not really a question! But if for some reason you deeply despise those dollars, perhaps because they remind you of PHP, you can avoid them altogether:
import render from 'react-test';
test("Increments when clicked", async () => {
const counter = render(<Counter />);
expect(counter).toHaveText("0");
await counter.click();
expect(counter).toHaveText("1");
});
We obviously love React, but let's not forget that jQuery also has some great things as well. This library brings some of these nice things to react testing.