You’re debugging a test for the formatDate function in the blogging app. The test fails because it’s trying to fetch data from an API to create a Date object. Based on the lecture, what’s wrong with this test?
formatDate
Date
It’s too slow for a unit test since it involves network calls.
There’s nothing wrong; unit tests should fetch real data.
It’s mocking the API, which makes it an integration test.
It’s testing formatDate in isolation, which is incorrect for a function.
It’s using a real browser, so it’s an E2E test.
Here’s a test for the PostPreview component:
PostPreview
import { render, screen } from "@testing-library/react"; import { describe, it, expect } from "vitest"; import { PostPreview } from "../components/PostPreview"; describe("PostPreview", () => { it("renders correctly", () => { render(<PostPreview title="Test Post" date={new Date("2025-04-07")} />); expect(screen.getByText("Test Post")).toBeDefined(); expect(screen.getByText("April 7, 2025")).toBeDefined(); }); });
What type of test is this, and why?
Integration test, because it uses React Testing Library.
E2E test, because it verifies text displayed to the user.
Unit test, because it tests one component in isolation with direct props.
Integration test, because it renders the component and checks interactions.
Your team writes this test for the PostList component:
PostList
import { render, screen } from "@testing-library/react"; import { describe, it, expect, vi } from "vitest"; import { PostList } from "../components/PostList"; describe("PostList", () => { it("displays fetched posts", async () => { vi.spyOn(global, "fetch").mockResolvedValue({ json: () => Promise.resolve([{ title: "Demo", date: "2025-04-07" }]), } as any); render(<PostList />); expect(await screen.findByText("Demo")).toBeDefined(); }); });
A bug causes the PostPreview component to render the wrong date format. Will this test catch it?
Yes, because it renders the full UI like an E2E test.
No, because the fetch is mocked, so it’s not a real test.
Yes, because it tests the integration between PostList and PostPreview.
No, because it doesn’t check the date output, only the title.
You’re reviewing an E2E test written with Playwright:
import { test, expect } from "@playwright/test"; test("user adds a post", async ({ page }) => { await page.goto("http://localhost:3000/admin"); await page.fill("#title", "New Post"); await page.fill("#date", "2025-04-08"); await page.click("button[type='submit']"); await page.goto("http://localhost:3000"); await expect(page.getByText("New Post")).toBeVisible(); });
What makes this an E2E test? Select all that apply.
It only tests one component at a time.
It’s slow because it involves real network requests.
It tests the full flow from UI to server to database.
It mocks the API to speed up execution.
It uses a real browser to interact with the app.
In the blogging app’s CI/CD pipeline, a test fails during the pull request stage, showing that PostList doesn’t render posts correctly. Based on the lecture, what type of test is most likely running at this stage?
Integration test, because it checks how components work together.
Unit test, because CI/CD always runs unit tests first.
Unit test, because it’s the fastest and catches logic errors.
E2E test, because it verifies the full app before deployment.
A student writes this test, thinking it’s a unit test:
import { render, screen } from "@testing-library/react"; import { describe, it, expect } from "vitest"; import { PostPreview } from "../components/PostPreview"; describe("PostPreview", () => { it("renders with real data", async () => { const response = await fetch("/api/posts"); const post = await response.json(); render(<PostPreview title={post[0].title} date={new Date(post[0].date)} />); expect(screen.getByText(post[0].title)).toBeDefined(); }); });
What’s wrong with calling this a unit test? Select all that apply.
It’s an E2E test because it uses real data in a component.
It’s fine as a unit test because it tests PostPreview.
It’s not isolated since it depends on an external system.
It’s an integration test because it fetches real data from an API.
You’re testing a feature where users filter posts by date. Which approach best verifies it works across the entire app, from UI button to database query?
Unit test the database query separately.
Integration test the filter component with a mocked API response.
Unit test the filter function in isolation.
E2E test the full flow of clicking the filter and checking results.
Here’s a test snippet:
import { describe, it, expect } from "vitest"; import { formatDate } from "../utils/date"; describe("formatDate", () => { it("handles leap year dates", () => { const date = new Date("2024-02-29"); expect(formatDate(date)).toBe("February 29, 2024"); }); });
A teammate argues this should be an integration test because dates are tricky. Why is it still a unit test? Select all that apply.
It doesn’t involve any external systems like APIs or browsers.
It uses toLocaleDateString, which depends on the environment.
toLocaleDateString
It checks system-level date settings, which makes it broader.
It’s fast and doesn’t require complex setup.
It tests formatDate in complete isolation.
This content is only available to registered users.Please register at the the home page.