Skip to content

Conversation

johnsimons
Copy link
Member

@johnsimons johnsimons commented Sep 15, 2025

We found an issue with the cleaning-up routines that can cause certain instances to be cleaned up before others.
We have started a discussion in vitest to figure out if the behaviour is a bug or if we are just using it incorrectly.

In the meantime, we have addressed the issue by refactoring the code to do the cleanup in a more consistent way.

This is to fix the flaky test failures that cause:
Error: ReferenceError: window is not defined
 ❯ stopTimer src/composables/autoRefresh.ts:12:7
 ❯ startTimer src/composables/autoRefresh.ts:20:5
 ❯ executeAndResetTimer src/composables/autoRefresh.ts:31:7
 ❯ Timeout._onTimeout src/composables/autoRefresh.ts:22:7
 ❯ listOnTimeout node:internal/timers:588:17
 ❯ processTimers node:internal/timers:523:7
Added run to vitest so by default we not running in watch mode
Otherwise the cleanup happens in a non deterministic order
@johnsimons johnsimons changed the title Replace window.setTimeout with just setTimeout Addressing issues with failing flaky tests Oct 7, 2025
@johnsimons johnsimons requested a review from PhilBastian October 7, 2025 05:37
"lint": "eslint .",
"preview": "vite preview",
"test:component": "vitest ./src",
"test:component": "vitest run ./src",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default vitest runs in watch mode, but in our CI we always want it to perform a single run without watch mode.

"@testing-library/vue": "8.1.0",
"@tsconfig/node18": "18.2.4",
"@types/bootstrap": "5.2.10",
"@types/jsdom": "27.0.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were missing this typescript type

Comment on lines -36 to -55
function deleteAllCookies() {
const cookies = document.cookie.split(";");

for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
const eqPos = cookie.indexOf("=");
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}

afterEach(() => {
//Intentionally not calling mockServer.resetHandlers here to prevent ServicePulse in flight messages after app unmount to fail.
//mockServer.resetHandlers is being called instead in driver.ts

//Make JSDOM create a fresh document per each test run
jsdom.reconfigure({ url: "http://localhost:3000/" });
localStorage.clear();
sessionStorage.clear();
deleteAllCookies();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this code was moved into the driver.ts

deleteAllCookies();
afterAll(() => {
console.log("Shutting down mock server");
mockServer.close();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are cleaning up the mock server as per the documentation

},
test: {
pool: "forks", //https://github.com/vitest-dev/vitest/issues/2008#issuecomment-187106690
pool: "forks",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forks is now the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant