-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Tests don't fail gracefully when using wait #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
A few things:
So where you have: await wait(() => {
const passwordTextBox = getByTestId("passwordTextBox")
passwordTextBox.value = "fakePassword"
Simulate.change(passwordTextBox)
}) You should probably do: const passwordTextBox = await waitForElement(() => getByTestId("passwordTextBox"))
passwordTextBox.value = "fakePassword"
Simulate.change(passwordTextBox) But what'd be even better is to not use |
Hi Kent, thanks for the quick detailed reply. Sorry for the sloppiness in the code sandbox link. The browser had locked up and I think it didn't save the last version. This is what I was trying to post. https://codesandbox.io/s/rjozql4jnm Anyway I was able to get the test to work by simplifying the wait calls and wanted to share the working code for future generations. import sinon from "sinon"
import { Simulate, wait } from "react-testing-library"
import { client } from "../../../_old/graphql/apolloClient"
import { mountApp } from "../../../test/testBehaviors"
jest.mock("../../../_old/graphql/apolloClient")
afterEach(() => {
client.resetStore()
})
describe("Given we load the App", () => {
describe("When data is returned and there is no user ", () => {
describe("And we enter a valid username and password and click submit", () => {
it("Then we call the login mutation with correct arguments and are redirected to the search page", async () => {
const userResolverStub = sinon.stub()
userResolverStub.onFirstCall().returns(null)
userResolverStub.onSecondCall().returns({
id: "fakeId",
email: "fakeEmail",
role: "fakeRole",
firstName: "fakeFirstName",
lastName: "fakeLastName",
algoliaSearchApiKey: "fakeAlgoliaSearchApiKey"
})
const loginMuationMock = sinon.expectation.create("loginMutationMock")
loginMuationMock
.withArgs(sinon.match.any, {
input: { email: "[email protected]", password: "fakePassword" }
})
.once()
.returns({
user: {
id: "fakeId",
email: "fakeEmail",
role: "fakeRole",
firstName: "fakeFirstName",
lastName: "fakeLastName",
algoliaSearchApiKey: "fakeAlgoliaSearchApiKey"
},
company: {
hasPurchasing: true
}
})
const mocks = {
User: userResolverStub,
Mutation: () => ({
// This needed to match the name of the mutation in MutationType.js
login: loginMuationMock
})
}
const { getByTestId } = mountApp({ mocks })
await wait(() => {
const emailTextBox = getByTestId("emailTextBox")
emailTextBox.value = "[email protected]"
Simulate.change(emailTextBox)
const passwordTextBox = getByTestId("passwordTextBox")
passwordTextBox.value = "fakePassword"
Simulate.change(passwordTextBox)
})
const loginForm = getByTestId("loginButton")
Simulate.submit(loginForm)
await wait(() => {
loginMuationMock.verify()
expect(getByTestId("search-page")).toBeTruthy()
})
})
})
})
}) In case your interested in my confusion |
I'm glad you got it working. But I still very strongly recommend against doing too much work in your |
…ndation that was given by Kent in issue 78.
@kentcdodds I got my test simplified and I've shared that below for others who might be curious. I also created a documentation pull request that updates PR: #79 import sinon from "sinon"
import { Simulate, wait } from "react-testing-library"
import { client } from "../../../_old/graphql/apolloClient"
import { mountApp } from "../../../test/testBehaviors"
jest.mock("../../../_old/graphql/apolloClient")
afterEach(() => {
client.resetStore()
})
describe("Given we load the App", () => {
describe("When data is returned and there is no user ", () => {
describe("And we enter a valid username and password and click submit", () => {
it("Then we call the login mutation with correct arguments and are redirected to the search page", async () => {
const userResolverStub = sinon.stub()
userResolverStub.onFirstCall().returns(null)
userResolverStub.onSecondCall().returns({
id: "fakeId",
email: "fakeEmail",
role: "fakeRole",
firstName: "fakeFirstName",
lastName: "fakeLastName",
algoliaSearchApiKey: "fakeAlgoliaSearchApiKey"
})
const loginMuationMock = sinon.expectation.create("loginMutationMock")
loginMuationMock
.withArgs(sinon.match.any, {
input: { email: "[email protected]", password: "fakePassword" }
})
.once()
.returns({
user: {
id: "fakeId",
email: "fakeEmail",
role: "fakeRole",
firstName: "fakeFirstName",
lastName: "fakeLastName",
algoliaSearchApiKey: "fakeAlgoliaSearchApiKey"
},
company: {
hasPurchasing: true
}
})
const mocks = {
User: userResolverStub,
Mutation: () => ({
// This needed to match the name of the mutation in MutationType.js
login: loginMuationMock
})
}
const { getByTestId } = mountApp({ mocks })
let emailTextBox
// Wait for tick after userResolverStub's first call
await wait(() => {
emailTextBox = getByTestId("emailTextBox")
})
emailTextBox.value = "[email protected]"
Simulate.change(emailTextBox)
const passwordTextBox = getByTestId("passwordTextBox")
passwordTextBox.value = "fakePassword"
Simulate.change(passwordTextBox)
const loginForm = getByTestId("loginButton")
Simulate.submit(loginForm)
// Wait for tick after loginMuationMock
await wait(() => {
loginMuationMock.verify()
expect(getByTestId("search-page")).toBeTruthy()
})
})
})
})
}) |
…ndation that was given by Kent in issue 78.
🎉 This issue has been resolved in version 3.0.2 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
react-testing-library
version:"react-testing-library": "^2.5.1",
react
version:"react": "^16.3.0",
node
version:v8.9.4
npm
(oryarn
) version:5.6.0
Relevant code or config:
What you did:
I got the above test working and then I wanted to verify that it would break so I changed the password code like this
password: password + 'changed'
so that the test would fail and I could make sure the error was good and all that.What happened:
The test failed eventually but it took a really long time and then didn't provided useful error feedback. We were instead getting a timeout error.
Reproduction:
I think this reproduces the issue. This seems to run for a really long time and never provide useful error feedback.
https://codesandbox.io/s/rjozql4jnm
Problem description:
We need the tests to fail fast and report back a useful error.
Suggested solution:
wait
should run several times quickly and if it's not able to get a clean run with no errors then it shouldPromise.reject(error)
with theerror
was thrown by the callback function.The text was updated successfully, but these errors were encountered: