Skip to content

Instantly share code, notes, and snippets.

@sapegin
Last active May 19, 2020 17:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapegin/110cc8e42638886775b7b969180b37da to your computer and use it in GitHub Desktop.
Save sapegin/110cc8e42638886775b7b969180b37da to your computer and use it in GitHub Desktop.
Common Testing Library antipatterns
import { render } from "@testing-library/react";
import { MyComponent } from './MyComponent';
describe("MyComponent", () => {
let wrapper;
beforeEach(async () => {
wrapper = render(<MyComponent butiful />);
});
it("should render a butiful component", async () => {
expect(wrapper.getByText("I am butiful")).toBeInTheDocument();
});
it("should render an ugly component", async () => {
wrapper.rerender(<MyComponent butiful={false} />);
expect(wrapper.getByText("I am butiful")).not.toBeInTheDocument();
});
});
import { render } from "@testing-library/react";
import { MyComponent } from './MyComponent';
describe("MyComponent", () => {
it("should render a butiful component", async () => {
const { getByText } = render(<MyComponent butiful />);
expect(getByText(/I am butiful/i)).toBeInTheDocument();
});
it("should render an ugly component", async () => {
const { getByText } = render(<MyComponent butiful={false} />);
expect(getByText(/I am butiful/i)).not.toBeInTheDocument();
});
});
import { render } from "@testing-library/react";
import { MyComponent } from "./MyComponent";
const defaultProps = {
size: "large",
color: "salmon",
butiful: true,
};
describe("MyComponent", () => {
it("should render a butiful component", async () => {
const { getByText } = render(<MyComponent {...defaultProps} />);
expect(getByText(/I am butiful/i)).toBeInTheDocument();
});
it("should render an ugly component", async () => {
const { getByText } = render(
<MyComponent {...defaultProps} butiful={false} />
);
expect(getByText(/I am butiful/i)).not.toBeInTheDocument();
});
});
import { render, screen } from "@testing-library/react";
import { MyComponent } from "./MyComponent";
const defaultProps = {
size: "large",
color: "salmon",
butiful: true,
};
describe("MyComponent", () => {
it("should render a butiful component", () => {
render(<MyComponent {...defaultProps} />);
expect(screen.getByText(/I am butiful/i)).toBeInTheDocument();
});
it("should render an ugly component", () => {
render(
<MyComponent {...defaultProps} butiful={false} />
);
expect(screen.queryByText(/I am butiful/i)).not.toBeInTheDocument();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment