-
Notifications
You must be signed in to change notification settings - Fork 137
Update jest config and add more unit test coverage #10652
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { getFunctionBody, replaceMultipleStrings } from "./evaluation-utils"; | ||
|
|
||
| describe("evaluation-utils", () => { | ||
| describe("getFunctionBody", () => { | ||
| it("returns inner statements without the wrapping function braces", () => { | ||
| const fn = function example() { | ||
| const n = 1; | ||
| return n + 1; | ||
| }; | ||
| const body = getFunctionBody(fn); | ||
| expect(body).toContain("const n = 1"); | ||
| expect(body).toContain("return n + 1"); | ||
| expect(body.trim().startsWith("function")).toBe(false); | ||
| }); | ||
|
|
||
| it("strips line comments from the serialized function", () => { | ||
| const fn = function withComment() { | ||
| // should disappear | ||
| return 42; | ||
| }; | ||
| const body = getFunctionBody(fn); | ||
| expect(body).not.toMatch(/\/\/ should disappear/); | ||
| expect(body).toContain("return 42"); | ||
| }); | ||
|
|
||
| it("strips block comments from the serialized function", () => { | ||
| const fn = function withBlock() { | ||
| /* block */ | ||
| return 7; | ||
| }; | ||
| const body = getFunctionBody(fn); | ||
| expect(body).not.toMatch(/\/\* block \*\//); | ||
| expect(body).toContain("return 7"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("replaceMultipleStrings", () => { | ||
| it("applies each replacement in order", () => { | ||
| expect( | ||
| replaceMultipleStrings("alpha beta", { | ||
| alpha: "A", | ||
| beta: "B", | ||
| }) | ||
| ).toBe("A B"); | ||
| }); | ||
|
|
||
| it("only replaces the first occurrence per search string (non-global)", () => { | ||
| expect(replaceMultipleStrings("x x", { x: "y" })).toBe("y x"); | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { comparePoints, pointEquals, pointPrecedes } from "./execution-point-utils"; | ||
|
|
||
| describe("execution-point-utils", () => { | ||
| describe("pointEquals", () => { | ||
| it("returns true for identical string points", () => { | ||
| expect(pointEquals("100", "100")).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false for different points", () => { | ||
| expect(pointEquals("100", "101")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("pointPrecedes / comparePoints", () => { | ||
| it("orders by length first (shorter numeric string is smaller)", () => { | ||
| expect(pointPrecedes("9", "10")).toBe(true); | ||
| expect(pointPrecedes("10", "9")).toBe(false); | ||
| expect(comparePoints("9", "10")).toBe(-1); | ||
| expect(comparePoints("10", "9")).toBe(1); | ||
| }); | ||
|
|
||
| it("uses lexicographic order when lengths match", () => { | ||
| expect(comparePoints("12", "34")).toBe(-1); | ||
| expect(comparePoints("34", "12")).toBe(1); | ||
| expect(comparePoints("99", "99")).toBe(0); | ||
| expect(pointPrecedes("12", "34")).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { getRelativePath, getRelativePathWithoutFile, parseUrlMemoized } from "./url"; | ||
|
|
||
| describe("replay-next url utils", () => { | ||
| beforeEach(() => { | ||
| parseUrlMemoized.cache?.clear?.(); | ||
| }); | ||
|
|
||
| describe("parseUrlMemoized", () => { | ||
| it("rewrites webpack://_N_E prefix for parsing", () => { | ||
| const parsed = parseUrlMemoized("webpack://_N_E/./src/page.tsx"); | ||
| expect(parsed.protocol).toBe("webpack:"); | ||
| expect(parsed.pathname).toContain("src"); | ||
| }); | ||
|
|
||
| it("rewrites webpack-internal prefix for parsing", () => { | ||
| const parsed = parseUrlMemoized("webpack-internal:///./foo.ts"); | ||
| expect(parsed.protocol).toBe("webpack-internal:"); | ||
| }); | ||
|
|
||
| it("falls back to treating a bare filename as pathname when URL parsing throws", () => { | ||
| const parsed = parseUrlMemoized("not-a-valid-url"); | ||
| expect(parsed.pathname).toBe("not-a-valid-url"); | ||
| expect(parsed.path).toBe("not-a-valid-url"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getRelativePath", () => { | ||
| it("returns path after the first slash in pathname", () => { | ||
| expect(getRelativePath("https://example.com/a/b/c")).toBe("a/b/c"); | ||
| }); | ||
|
|
||
| it("returns the input url when parsed pathname is empty", () => { | ||
| expect(getRelativePath("")).toBe(""); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getRelativePathWithoutFile", () => { | ||
| it("strips the final path segment (file name)", () => { | ||
| expect(getRelativePathWithoutFile("https://example.com/pkg/src/file.ts")).toBe("pkg/src"); | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { arrayShallowEqual, shallowEqual, strictEqual } from "./compare"; | ||
|
|
||
| describe("compare", () => { | ||
| describe("strictEqual", () => { | ||
| it("uses === semantics", () => { | ||
| expect(strictEqual(1, 1)).toBe(true); | ||
| expect(strictEqual(1, "1" as unknown as number)).toBe(false); | ||
| expect(strictEqual(NaN, NaN)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("shallowEqual", () => { | ||
| it("compares primitives with ===", () => { | ||
| expect(shallowEqual(3, 3)).toBe(true); | ||
| expect(shallowEqual(3, 4)).toBe(false); | ||
| }); | ||
|
|
||
| it("compares arrays element-wise with ===", () => { | ||
| expect(shallowEqual([1, "a"], [1, "a"])).toBe(true); | ||
| expect(shallowEqual([1, {}], [1, {}])).toBe(false); | ||
| expect(shallowEqual([1], [1, 2])).toBe(false); | ||
| }); | ||
|
|
||
| it("compares plain objects by same keys in insertion order and values with ===", () => { | ||
| expect(shallowEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true); | ||
| expect(shallowEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false); | ||
| expect(shallowEqual({ a: {} }, { a: {} })).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("arrayShallowEqual", () => { | ||
| it("requires same length and strict element equality", () => { | ||
| expect(arrayShallowEqual([], [])).toBe(true); | ||
| expect(arrayShallowEqual([0, 1], [0, 1])).toBe(true); | ||
| expect(arrayShallowEqual([0], [0, 1])).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { getOS, isMacOS, isWindowsOS } from "./os"; | ||
|
|
||
| describe("os", () => { | ||
| const originalUA = window.navigator.userAgent; | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(window.navigator, "userAgent", { | ||
| value: originalUA, | ||
| configurable: true, | ||
| }); | ||
| }); | ||
|
|
||
| function setUserAgent(ua: string) { | ||
| Object.defineProperty(window.navigator, "userAgent", { | ||
| value: ua, | ||
| configurable: true, | ||
| }); | ||
| } | ||
|
|
||
| it("maps Linux user agents", () => { | ||
| setUserAgent("Mozilla/5.0 (X11; Linux x86_64)"); | ||
| expect(getOS()).toBe("Linux"); | ||
| expect(isMacOS()).toBe(false); | ||
| expect(isWindowsOS()).toBe(false); | ||
| }); | ||
|
|
||
| it("maps Windows user agents", () => { | ||
| setUserAgent("Mozilla/5.0 (Windows NT 10.0)"); | ||
| expect(getOS()).toBe("WINNT"); | ||
| expect(isWindowsOS()).toBe(true); | ||
| expect(isMacOS()).toBe(false); | ||
| }); | ||
|
|
||
| it("maps macOS user agents", () => { | ||
| setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"); | ||
| expect(getOS()).toBe("Darwin"); | ||
| expect(isMacOS()).toBe(true); | ||
| expect(isWindowsOS()).toBe(false); | ||
| }); | ||
|
|
||
| it("returns Unknown for unrecognized agents", () => { | ||
| setUserAgent("SomeOtherClient/1.0"); | ||
| expect(getOS()).toBe("Unknown"); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { isValidPoint } from "./points"; | ||
|
|
||
| describe("isValidPoint", () => { | ||
| it("accepts objects with required own properties", () => { | ||
| expect( | ||
| isValidPoint({ | ||
| badge: null, | ||
| condition: "", | ||
| content: "", | ||
| }) | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("rejects primitives and null", () => { | ||
| expect(isValidPoint(null)).toBe(false); | ||
| expect(isValidPoint(undefined)).toBe(false); | ||
| expect(isValidPoint("x")).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects objects missing a required key", () => { | ||
| expect(isValidPoint({ badge: 1, condition: "" })).toBe(false); | ||
| expect(isValidPoint({ badge: 1, content: "" })).toBe(false); | ||
| expect(isValidPoint({ condition: "", content: "" })).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects objects that only inherit required keys from the prototype", () => { | ||
| const proto = { badge: 1, condition: "", content: "" }; | ||
| const o = Object.create(proto); | ||
| expect(isValidPoint(o)).toBe(false); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| jest.mock("slugify", () => ({ | ||
| __esModule: true, | ||
| default: (s: string) => s, | ||
| })); | ||
|
|
||
| jest.mock("replay-next/src/suspense/BuildIdCache", () => ({ | ||
| RecordingTarget: { | ||
| gecko: "gecko", | ||
| chromium: "chromium", | ||
| node: "node", | ||
| unknown: "unknown", | ||
| }, | ||
| getRecordingTarget: jest.fn(), | ||
| })); | ||
|
|
||
| import { RecordingTarget, getRecordingTarget } from "replay-next/src/suspense/BuildIdCache"; | ||
| import type { Recording } from "shared/graphql/types"; | ||
| import { SLUG_SEPARATOR } from "shared/utils/slug"; | ||
|
|
||
| import { extractRecordingIdFromPathname, getRecordingURL, showDurationWarning } from "./recording"; | ||
|
|
||
| function minimalRecording(overrides: Partial<Recording> = {}): Recording { | ||
| return { | ||
| date: "2020-01-01", | ||
| duration: null, | ||
| id: "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee", | ||
| testRunId: null, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("recording", () => { | ||
| const getRecordingTargetMock = getRecordingTarget as jest.MockedFunction< | ||
| typeof getRecordingTarget | ||
| >; | ||
|
|
||
| beforeEach(() => { | ||
| getRecordingTargetMock.mockReset(); | ||
| }); | ||
|
|
||
| describe("extractRecordingIdFromPathname", () => { | ||
| it("extracts UUID from /recording/:id", () => { | ||
| const id = "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee"; | ||
| expect(extractRecordingIdFromPathname(`/recording/${id}`)).toBe(id); | ||
| }); | ||
|
|
||
| it("extracts id from slug--uuid under /recording/", () => { | ||
| const id = "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee"; | ||
| const path = `/recording/title${SLUG_SEPARATOR}${id}`; | ||
| expect(extractRecordingIdFromPathname(path)).toBe(id); | ||
| }); | ||
|
|
||
| it("returns undefined when not under /recording/", () => { | ||
| expect( | ||
| extractRecordingIdFromPathname(`/${"aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee"}`) | ||
| ).toBeUndefined(); | ||
| expect(extractRecordingIdFromPathname("/")).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("showDurationWarning", () => { | ||
| it("is true only when duration exceeds two minutes in ms", () => { | ||
| expect(showDurationWarning(minimalRecording({ duration: 120_001 }))).toBe(true); | ||
| expect(showDurationWarning(minimalRecording({ duration: 120_000 }))).toBe(false); | ||
| expect(showDurationWarning(minimalRecording({ duration: null }))).toBe(false); | ||
| expect(showDurationWarning(minimalRecording({ duration: 0 }))).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getRecordingURL", () => { | ||
| it("uses legacy host for gecko targets", () => { | ||
| getRecordingTargetMock.mockReturnValue(RecordingTarget.gecko); | ||
| const rec = minimalRecording({ buildId: "firefox-gecko", title: null }); | ||
| expect(getRecordingURL(rec, true)).toBe(`https://legacy.replay.io/recording/${rec.id}`); | ||
| }); | ||
|
|
||
| it("includes /recording/ path when includeBasePath is true for non-gecko", () => { | ||
| getRecordingTargetMock.mockReturnValue(RecordingTarget.chromium); | ||
| const rec = minimalRecording({ buildId: "chromium", title: null }); | ||
| expect(getRecordingURL(rec, true)).toBe(`/recording/${rec.id}`); | ||
| }); | ||
|
|
||
| it("omits recording segment when includeBasePath is false", () => { | ||
| getRecordingTargetMock.mockReturnValue(RecordingTarget.chromium); | ||
| const rec = minimalRecording({ buildId: "chromium", title: null }); | ||
| expect(getRecordingURL(rec, false)).toBe(`/${rec.id}`); | ||
| }); | ||
|
|
||
| it("prefixes id with slugified title and separator when title is set", () => { | ||
| getRecordingTargetMock.mockReturnValue(RecordingTarget.chromium); | ||
| const rec = minimalRecording({ | ||
| buildId: "chromium", | ||
| title: "MyTitle", | ||
| }); | ||
| expect(getRecordingURL(rec, true)).toBe(`/recording/mytitle${SLUG_SEPARATOR}${rec.id}`); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always returned undefined. Fixed to return boolean