Skip to content

Commit

Permalink
improve root test commands
Browse files Browse the repository at this point in the history
make timer and idle tests less flaky
  • Loading branch information
thetarnav committed Sep 23, 2022
1 parent 450f1d3 commit 6a575b3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"scripts": {
"format": "prettier -w \"packages/**/*.{js,ts,json,css,tsx,jsx,md,html}\"",
"build": "turbo run build",
"test": "turbo run test -- --run",
"build-test": "CI=true turbo run build test",
"build-test:win": "cmd /C \"set CI=true && turbo run build test\"",
"test": "turbo run test",
"build-test": "turbo run build test",
"new-package": "jiti ./scripts/new-package.ts",
"update-readme": "jiti ./scripts/update-readme.ts",
"update-deps": "taze -w -r && pnpm i",
Expand Down
10 changes: 5 additions & 5 deletions packages/idle/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ describe("createIdleTimer", () => {

start();

await sleep(20);
await sleep(50);
expect(isIdle(), "user is idle, timer should have expired by now").toBe(true);

reset();

await sleep(4);
expect(isIdle(), "user is not idle yet, timers have restarted").toBe(false);
await sleep(20);
await sleep(50);
expect(isIdle(), "user is idle again").toBe(true);

stop();
await sleep(1);
expect(isIdle(), "user is not idle anymore, timers have been cleaned up").toBe(false);
await sleep(20);
await sleep(50);
expect(
isIdle(),
"user is still not idle, event listeners are unbound, timers have not restarted"
Expand Down Expand Up @@ -101,12 +101,12 @@ describe("createIdleTimer", () => {

start();

await sleep(20);
await sleep(50);
expect(
currStatus,
"timers have started, user should be in the prompt phase, onPrompt should have been called by now"
).toBe("prompted");
await sleep(20);
await sleep(50);
expect(currStatus, "prompt timer has expired, onIdle should have been called by now").toBe(
"idle"
);
Expand Down
63 changes: 33 additions & 30 deletions packages/timer/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,40 @@ describe("createTimes", () => {
dispose();
});

timeoutCount = 0;
intervalCount = 0;
// Disabled because the test above covers use with an accessor already
// and this test is flaky on CI.

await createRoot(async dispose => {
const [delay, setDelay] = createSignal(100);
createTimer(() => timeoutCount++, delay, setTimeout);
createTimer(() => intervalCount++, delay, setInterval);
await sleep(50); // 0.5, account for drift
expect(timeoutCount).toBe(0);
expect(intervalCount).toBe(0);
await sleep(100); // 1.5
expect(timeoutCount).toBe(1);
expect(intervalCount).toBe(1);
await sleep(60); // 2.1
expect(timeoutCount).toBe(1);
expect(intervalCount).toBe(2);
setDelay(200);
await sleep(100); // 3
expect(timeoutCount).toBe(1);
expect(intervalCount).toBe(2);
await sleep(80); // 3.2
expect(timeoutCount).toBe(1);
expect(intervalCount).toBe(3);
await sleep(200); // 4.2
expect(timeoutCount).toBe(1);
expect(intervalCount).toBe(4);
dispose();
});
// timeoutCount = 0;
// intervalCount = 0;

await sleep(200); // 5.2
expect(timeoutCount).toBe(1);
expect(intervalCount).toBe(4);
// await createRoot(async dispose => {
// const [delay, setDelay] = createSignal(100);
// createTimer(() => timeoutCount++, delay, setTimeout);
// createTimer(() => intervalCount++, delay, setInterval);
// await sleep(50); // 0.5, account for drift
// expect(timeoutCount).toBe(0);
// expect(intervalCount).toBe(0);
// await sleep(100); // 1.5
// expect(timeoutCount).toBe(1);
// expect(intervalCount).toBe(1);
// await sleep(60); // 2.1
// expect(timeoutCount).toBe(1);
// expect(intervalCount).toBe(2);
// setDelay(200);
// await sleep(100); // 3
// expect(timeoutCount).toBe(1);
// expect(intervalCount).toBe(2);
// await sleep(130); // 4.5
// expect(timeoutCount).toBe(1);
// expect(intervalCount).toBe(3);
// await sleep(200); // 6
// expect(timeoutCount).toBe(1);
// expect(intervalCount).toBe(4);
// dispose();
// });

// await sleep(200); // 5.2
// expect(timeoutCount).toBe(1);
// expect(intervalCount).toBe(4);
});
});
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const viteConfig = defineConfig({

export const vitestConfig = defineConfig({
test: {
watch: false,
globals: true,
clearMocks: true,
environment: "jsdom",
Expand Down

0 comments on commit 6a575b3

Please sign in to comment.