Skip to content

Commit

Permalink
npm i @sinonjs/fake-timers@latest
Browse files Browse the repository at this point in the history
As noted in 7de53a5, this causes one of the tests to break; the
runMicrotasks() works around this. It makes the countTimers() assertion
partially pointless, but I can live with that (and I think it still
asserts something, just not microtasks anymore). Note that the
runMicrotasks() can’t be part of the test function itself for two
reasons: the job is (somehow) only added after the test returns (it
doesn’t exist at the end of the test function yet – you can see that by
console.log()ing clock.jobs in both places), and the failure isn’t
specific to any one test function (if you .skip() one, the failure
shifts to the next function).

I think the missing `await`s on `tickAsync()` were always wrong, but
didn’t cause problems (the problem here also turned out to be
unrelated). Let’s add them anyways.
  • Loading branch information
lucaswerkmeister committed Sep 22, 2024
1 parent 0a1e913 commit 7772f8f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ but this file may sometimes contain later improvements (e.g. typo fixes).

## next (not yet released)

No changes yet.
- Updated dependencies.

## v0.8.4 (2024-09-22)

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"undici": "^6.12.0"
},
"devDependencies": {
"@sinonjs/fake-timers": "^12.0.0",
"@sinonjs/fake-timers": "^13.0.2",
"chai": "^5.1.0",
"chai-as-promised": "^8.0.0",
"eslint": "^8.57.0",
Expand Down
28 changes: 19 additions & 9 deletions test/unit/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ describe( 'Session', () => {

it( 'uses retry options for token request', async () => {
const clock = FakeTimers.install();
clock.tickAsync( 1000 ); // just so it doesn’t start at 0
await clock.tickAsync( 1000 ); // just so it doesn’t start at 0

try {
const expectedParams = {
Expand Down Expand Up @@ -406,7 +406,7 @@ describe( 'Session', () => {
retryAfterReadonlySeconds: 3,
tokenType: 'csrf',
} );
clock.tickAsync( 5000 );
await clock.tickAsync( 5000 );
await expect( promise )
.to.be.rejectedWith( ApiErrors );
} finally {
Expand Down Expand Up @@ -638,6 +638,16 @@ describe( 'Session', () => {
clock = FakeTimers.install();
} );
afterEach( () => {
/*
* For some reason, since @sinonjs/fake-timers v13,
* the first test in this block which runs (no matter which specific test it is)
* creates a Node-internal afterWriteTick microtask / job
* in between the test returning and the afterEach hook running,
* which fails the countTimers expectation below unless we run it first.
* It’s weird, but at least this keeps the tests working 🤷
*/
clock.runMicrotasks();

clock.uninstall();
expect( clock.countTimers() ).to.equal( 0 );
} );
Expand All @@ -651,7 +661,7 @@ describe( 'Session', () => {
{ response: { response: true } },
] );
const promise = session.request( {} );
clock.tickAsync( 60000 );
await clock.tickAsync( 60000 );
const response = await promise;
expect( response ).to.eql( { response: true } );
} );
Expand Down Expand Up @@ -682,7 +692,7 @@ describe( 'Session', () => {
{ response: { response: true } },
] );
const promise = session.request( {} );
clock.tickAsync( 65000 );
await clock.tickAsync( 65000 );
const response = await promise;
expect( response ).to.eql( { response: true } );
} );
Expand All @@ -699,7 +709,7 @@ describe( 'Session', () => {
} },
] );
const promise = session.request( {}, { maxRetriesSeconds: 5 } );
clock.tickAsync( 5000 );
await clock.tickAsync( 5000 );
await expect( promise )
.to.be.rejectedWith( ApiErrors );
} );
Expand Down Expand Up @@ -730,7 +740,7 @@ describe( 'Session', () => {
{ response: { response: true } },
] );
const promise = session.request( {} );
clock.tickAsync( 5000 );
await clock.tickAsync( 5000 );
const response = await promise;
expect( response ).to.eql( { response: true } );
} );
Expand All @@ -741,7 +751,7 @@ describe( 'Session', () => {
{ response: { response: true } },
] );
const promise = session.request( {} );
clock.tickAsync( 30000 );
await clock.tickAsync( 30000 );
const response = await promise;
expect( response ).to.eql( { response: true } );
} );
Expand All @@ -752,7 +762,7 @@ describe( 'Session', () => {
{ response: { response: true } },
] );
const promise = session.request( {}, { retryAfterMaxlagSeconds: 2 } );
clock.tickAsync( 2000 );
await clock.tickAsync( 2000 );
const response = await promise;
expect( response ).to.eql( { response: true } );
} );
Expand All @@ -763,7 +773,7 @@ describe( 'Session', () => {
{ response: { response: true } },
] );
const promise = session.request( {}, { retryAfterReadonlySeconds: 10 } );
clock.tickAsync( 10000 );
await clock.tickAsync( 10000 );
const response = await promise;
expect( response ).to.eql( { response: true } );
} );
Expand Down

0 comments on commit 7772f8f

Please sign in to comment.