Last active
May 19, 2020 17:51
-
-
Save sapegin/110cc8e42638886775b7b969180b37da to your computer and use it in GitHub Desktop.
Common Testing Library antipatterns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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