Test works in isolation but fails alongside other RTL-based tests
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- javascript, react
Hướng nghiên cứu
Bắt đầu bằng cách chạy toàn bộ suite chứa UserApp, render-utils, React Testing Library, userEvent và thiết lập nock persistent, sau đó so sánh với từng lần chạy test riêng lẻ. Kiểm tra cleanup, trạng thái userEvent dùng chung và các mock beforeAll; hoàn thành khi các test điều hướng cùng pass mà không có lỗi phụ thuộc vào thứ tự.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
@testing-library/reactversion:
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.0.0",- Testing Framework and version:
Jest, maybe 29.5.12? Unknown version within Create React App (package-lock says "@types/jest": "^29.5.12",) - DOM Environment:
jsdom from jest-environment-jsdom 27.5.1
Relevant code or config:
// This test works on its own but not alongside the other tests shown below
it('should navigate to customers page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
await user.click(await screen.findByText("Customers"));
expect(await screen.findByText('[email protected]')).toBeInTheDocument();
});
// It works if I skip the test before it (**not** the failing test itself):
xit('should not navigate to create-transaction page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
expect(screen.queryByText("Create Transaction")).toBeNull();
});
it('should navigate to customers page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
await user.click(await screen.findByText("Customers"));
expect(await screen.findByText('[email protected]')).toBeInTheDocument();
});
// I can get the test after it to fail if I move the test block up to be after `it('should not navigate to create-transaction page', ...)`
it('should not navigate to create-transaction page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
expect(screen.queryByText("Create Transaction")).toBeNull();
});
it('should navigate to agents page', async () => { // <== the 5th test tends to fail
renderWithProviders(<UserApp/>, {path: '/login'});
await act(()=> user.click(screen.getByDisplayValue("Log In")));
await user.click(await screen.findByText("Agents"));
expect(await screen.findByText('TestAgent OnAdminPage')).toBeInTheDocument();
});
// The full test suite, with mock API using `nock` (I moved from `msw` because I thought it was the problem, but I'm experiencing the exact same problems so I'm pretty sure the problem is with React Testing Library):
import {renderWithProviders} from "./render-utils";
import {act, cleanup, screen} from "@testing-library/react";
import {UserApp} from "../UserApp";
import userEvent from "@testing-library/user-event";
import nock from "nock";
const user = userEvent.setup();
describe('navigation and default pages for Tryula Admin', () => {
beforeAll(() => {
const scope = nock('http://localhost:3000')
.defaultReplyHeaders({
'access-control-allow-origin': '*',
'access-control-allow-credentials': 'true'
});
scope
.persist()
.post('/login/')
.reply(200, {
user: {
user_type: 'TryulaAdmin',
}
}
);
scope
.persist()
.get('/privacy_check/invalid/')
.reply(200, {privacy_policy_consent_required: true});
scope
.persist()
.get('/any_admin_stats/')
.reply(200, {});
scope
.persist()
.get('/service_areas/')
.reply(200, [{name: 'Fakerton'}]);
scope
.persist()
.get('/languages/')
.reply(200, [{name: 'Engl-ish'}]);
scope
.persist()
.get('/agents/')
.reply(200, [
{first_name: 'TestAgent', last_name: 'OnAdminPage', service_areas: []}
]);
scope
.persist()
.get('/agents/1/')
.reply(200, {
bio: 'I am the real fake agent',
first_name: 'TestFirstName',
last_name: 'TestLastName',
languages: [],
service_areas: []
});
scope
.persist()
.get('/transactions/')
.reply(200, [{
agent_id: 1,
agent_name: 'Test Agent',
customer_name: 'Test Customer'
}]);
scope
.persist()
.get('/customers/')
.reply(200, [{
email: '[email protected]'
}]);
});
afterEach(cleanup);
it('should go to /dashboard and see stats after login from agent search', async () => {
renderWithProviders(<UserApp/>, {path: '/login?return_url=/'});
await act(()=> user.click(screen.getByDisplayValue("Log In")));
expect(await screen.findByTestId('num-gt45d-transactions')).toBeInTheDocument();
});
it('should go to /dashboard and see stats after login from agent profile', async () => {
renderWithProviders(<UserApp/>, {path: '/login?return_url=/agent/1'});
await act(()=> user.click(screen.getByDisplayValue("Log In")));
expect(await screen.findByTestId('num-gt45d-transactions')).toBeInTheDocument();
});
it('should navigate to stats after seeing transactions', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(()=> user.click(screen.getByDisplayValue("Log In")));
await user.click(await screen.findByText("Transactions"));
expect(await screen.findByText("Test Agent")).toBeInTheDocument();
await user.click(await screen.findByText("Dashboard"));
expect(await screen.findByTestId('num-active-transactions')).toBeInTheDocument();
expect(await screen.findByTestId('num-pending-transactions')).toBeInTheDocument();
expect(await screen.findByTestId('num-closed-transactions')).toBeInTheDocument();
expect(await screen.findByTestId('num-gt45d-transactions')).toBeInTheDocument();
});
it('should not navigate to create-transaction page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
expect(screen.queryByText("Create Transaction")).toBeNull();
});
it('should navigate to agents page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(()=> user.click(screen.getByDisplayValue("Log In")));
await user.click(await screen.findByText("Agents"));
expect(await screen.findByText('TestAgent OnAdminPage')).toBeInTheDocument();
});
it('should navigate to customers page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
await user.click(await screen.findByText("Customers"));
expect(await screen.findByText('[email protected]')).toBeInTheDocument();
});
it('should navigate to create-agent page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(()=> user.click(screen.getByDisplayValue("Log In")));
await user.click(await screen.findByText("Create Agent"));
expect(await screen.findByText('First Name')).toBeInTheDocument();
expect(await screen.findByText('Last Name')).toBeInTheDocument();
expect(await screen.findByText('Email')).toBeInTheDocument();
expect(await screen.findByText('Phone')).toBeInTheDocument();
expect(await screen.findByText('Powerform URL')).toBeInTheDocument();
expect(await screen.findByText('Referral Agreement')).toBeInTheDocument();
expect(await screen.findByText('Powerform ID')).toBeInTheDocument();
});
it('should not navigate to tryula-admins page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
expect(screen.queryByText("Tryula Admins")).toBeNull();
});
it('should not navigate to super-admins page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
expect(screen.queryByText("Super Admins")).toBeNull();
});
it('should not navigate to Site Settings page', async () => {
renderWithProviders(<UserApp/>, {path: '/login'});
await act(() => user.click(screen.getByDisplayValue("Log In")));
expect(screen.queryByText("Site Settings")).toBeNull();
});
});
What you did:
I have 10 unit tests that simulate a login with some fake API data (all data is contained in beforeAll), and then assert the default page for that type of user and what else they can navigate to. They each work individually, and they ought to, because I don't think I'm trying to do anything crazy--I'm not even trying to change the endpoints for each test, which I was doing before and which proved to be extremely finicky. I've isolated a stable subset of the fake API for each set of tests because I banged my head against the wall for a day with nonsensical failures when attempting to define different responses to the same endpoints within each it(...) block (for example, in one run there's one transaction, and in the next that endpoint returns [] and the app should display No transactions found.) I have some inkling to watch out for Jest test parallelization, so I've tried --runInBand to no avail. I have been berated by warnings about how I have to wrap state changes in act(), so I've tried doing that; at first it seemed like it helped, but then it just went back to failing. I figured somewhere I should be using waitFor, but RTL documentation says that using the find... queries has that built in automatically. Interestingly, I can get the "problem test" to succeed by skipping the test before it, and I can change which test fails by moving them around, as if just the 5th or 6th test is doomed to fail.
What happened:
The 5th test usually fails if run alongside the other tests. When it's run by itself, it works fine.
Reproduction:
Problem description:
It's seemingly so arbitrary. I've meticulously scoured the documentation and many, many Stack Overflow posts and I just can't reason about why it's behaving so erratically.
Suggested solution:
Document how to do this type of testing because the current learning curve is immensely steep.
- Ngôn ngữ chính
- JavaScript
- Star
- 19.7k
- Fork
- 1.2k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của testing-library/react-testing-library
-
fireEvent.select does not wrap its automatic native focus in actCó thể đã có người làm @sergioperezcheco đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict modeCó thể làm lại được @suhailopensource đã nhận 72 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 35/100
testing-library/react-testing-library#1466 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
testing-library/react-testing-library#1459 · 2 bình luận ·
-
perf: optimize container lookup with early exitCó thể làm lại được @Ch-Abhinav-Chowdary đã nhận 298 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 35/100
testing-library/react-testing-library#1430 · 1 bình luận ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)Có thể đã có người làm @swarnim02 đã nhận 315 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
Tất cả issue của testing-library/react-testing-library
Issue tương tự
-
[BUG] Multi-day events show "Ended" while still in progressCó thể đã có người làm @tarunagnihotri534 đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
data-umbrella/du-event-board#231 · 2 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
NaturalIntelligence/fast-xml-parser#888 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 80/100
USACE/chart-docs#766 ·
-
bug callouts regression revealjs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
quarto-dev/quarto-cli#15014 ·
Maintainer thường phản hồi trong vòng 1 ngày