diff --git a/src/components/TrackPicker.test.js b/src/components/TrackPicker.test.js index 839ee1da..bb3b657c 100644 --- a/src/components/TrackPicker.test.js +++ b/src/components/TrackPicker.test.js @@ -5,6 +5,7 @@ import "@testing-library/jest-dom"; import "../config-client.js"; import { config } from "../config-global.mjs"; import { defaultTrackColors } from "../common.mjs"; +import { timeoutAtLeast } from "../testHelpers"; describe("TrackPicker", () => { const tracks = { @@ -184,7 +185,7 @@ describe("TrackPicker", () => { expect(fakeOnChange).toHaveBeenCalledTimes(2); expect(fakeOnChange).toHaveBeenCalledWith(newTracks); - }, 50000); + }, timeoutAtLeast(50000)); it("should close when the exit button is pressed", async () => { const fakeOnChange = jest.fn(); diff --git a/src/end-to-end.test.js b/src/end-to-end.test.js index 5aee54ad..95ddd4d2 100644 --- a/src/end-to-end.test.js +++ b/src/end-to-end.test.js @@ -23,6 +23,7 @@ import "@testing-library/jest-dom/extend-expect"; import userEvent from "@testing-library/user-event"; import selectEvent from "react-select-event"; import App from "./App"; +import { timeoutAtLeast } from "./testHelpers"; const getRegionInput = () => { // Helper function to select the Region input box @@ -376,7 +377,7 @@ it("produces correct link for view before & after go is pressed", async () => { "http://localhost/?tracks[0][trackFile]=exampleData%2Fcactus.vg.xg&tracks[0][trackType]=graph&tracks[1][trackFile]=exampleData%2Fcactus-NA12879.sorted.gam&tracks[1][trackType]=read&bedFile=exampleData%2Fcactus.bed&name=cactus®ion=ref%3A1-100&dataType=built-in&simplify=false&removeSequences=false"; // Make sure link has changed after pressing go expect(fakeClipboard).toEqual(expectedLinkCactus); -}, 20000); +}, timeoutAtLeast(20000)); it("can retrieve the list of mounted graph files", async () => { // Wait for everything to settle so we don't stop the server while it is thinking @@ -527,5 +528,5 @@ it("can accept uploaded files", async () => { expect(svg.getElementsByTagName("title").length).toEqual(5); console.log("Test over"); -}, 50000); // We need to allow a long time for the slow vg test machines. +}, timeoutAtLeast(50000)); // We need to allow a long time for the slow vg test machines. // TODO: Is this slow because of unnecessary re-renders caused by the new color schemes taking effect and being rendered with the old data, before the new data downloads? diff --git a/src/scripts.test.js b/src/scripts.test.js index b9fd97eb..60aea99a 100644 --- a/src/scripts.test.js +++ b/src/scripts.test.js @@ -3,6 +3,7 @@ import "./config-server.mjs"; import { find_vg } from "./vg.mjs"; +import { timeoutAtLeast } from "./testHelpers"; import { mkdtemp, rm, cp, open, access } from 'node:fs/promises'; import { join } from 'node:path'; @@ -57,6 +58,6 @@ it("can run prepare_vg.sh", async () => { console.log("stderr:", stderr); await access(join(workDir, "x.vg.xg")); await access(join(workDir, "x.vg.gbwt")); -}, 30000); +}, timeoutAtLeast(30000)); diff --git a/src/testHelpers.js b/src/testHelpers.js new file mode 100644 index 00000000..7f8c1a59 --- /dev/null +++ b/src/testHelpers.js @@ -0,0 +1,33 @@ +// Helpers shared by test files. + +// Jest's built-in default per-test timeout. +const JEST_BUILTIN_TIMEOUT = 5000; + +// Find jest-circus's internal state object, or undefined if it can't be found. +export function findJestState() { + // jest-circus stores this on the test file's global under an unregistered + // symbol named JEST_STATE_SYMBOL, and applies the Jest configuration to it + // before loading setup and test files. See Jest v27.5.1: + // https://github.com/jestjs/jest/blob/67c1aa20c5fec31366d733e901fee2b981cb1850/packages/jest-circus/src/types.ts#L12-L14 + // https://github.com/jestjs/jest/blob/67c1aa20c5fec31366d733e901fee2b981cb1850/packages/jest-circus/src/state.ts#L33-L46 + // https://github.com/jestjs/jest/blob/67c1aa20c5fec31366d733e901fee2b981cb1850/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts#L67-L69 + // https://github.com/jestjs/jest/blob/67c1aa20c5fec31366d733e901fee2b981cb1850/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts#L26-L85 + // TODO: This is not a public Jest API and may break on a Jest upgrade. + // + // Jest gives the code under test its own isolated module registry, so we + // can't get at the real Jest state by importing jest-circus. See: + // https://github.com/jestjs/jest/blob/67c1aa20c5fec31366d733e901fee2b981cb1850/packages/jest-runtime/src/index.ts#L803-L811 + // https://github.com/jestjs/jest/blob/67c1aa20c5fec31366d733e901fee2b981cb1850/packages/jest-runtime/src/index.ts#L849-L870 + const stateSymbol = Object.getOwnPropertySymbols(globalThis).find( + (s) => s.description === "JEST_STATE_SYMBOL" + ); + return stateSymbol && globalThis[stateSymbol]; +} + +// Get a per-test timeout of at least ms, or longer if Jest's default is +// longer. +export function timeoutAtLeast(ms) { + // Use the configured Jest test timeout, or a default value if it is + // unavailable. + return Math.max(ms, findJestState()?.testTimeout || JEST_BUILTIN_TIMEOUT); +}