Skip to content

Commit

Permalink
fix(backend): Use Use fakeTimers() in tests to resolve test flakiness
Browse files Browse the repository at this point in the history
The tests were failing from time to time due to difference in the dates
in the error messages asserted in the assertions test files.
The difference was 1 second.
  • Loading branch information
dimkl committed Sep 21, 2023
1 parent 378a903 commit 27b611e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-meals-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Fix 1 second flakiness in assertions tests
35 changes: 31 additions & 4 deletions packages/backend/src/tokens/jwt/assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from './assertions';

export default (QUnit: QUnit) => {
const { module, test, hooks } = QUnit;
const { module, test } = QUnit;

function formatToUTCString(ts: number) {
const tsDate = new Date(0);
Expand Down Expand Up @@ -259,7 +259,16 @@ export default (QUnit: QUnit) => {
});
});

module('assertExpirationClaim(exp, clockSkewInMs)', () => {
module('assertExpirationClaim(exp, clockSkewInMs)', hooks => {
let fakeClock;
hooks.beforeEach(() => {
fakeClock = sinon.useFakeTimers();
});
hooks.afterEach(() => {
fakeClock.restore();
sinon.restore();
});

test('throws err if exp is in the past', assert => {
const nowInSeconds = Date.now() / 1000;
const exp = nowInSeconds - 5;
Expand Down Expand Up @@ -300,7 +309,16 @@ export default (QUnit: QUnit) => {
});
});

module('assertActivationClaim(nbf, clockSkewInMs)', () => {
module('assertActivationClaim(nbf, clockSkewInMs)', hooks => {
let fakeClock;
hooks.beforeEach(() => {
fakeClock = sinon.useFakeTimers();
});
hooks.afterEach(() => {
fakeClock.restore();
sinon.restore();
});

test('does not throw error if nbf is undefined', assert => {
assert.equal(undefined, assertActivationClaim(undefined, 0));
});
Expand Down Expand Up @@ -343,7 +361,16 @@ export default (QUnit: QUnit) => {
});
});

module('assertIssuedAtClaim(iat, clockSkewInMs)', () => {
module('assertIssuedAtClaim(iat, clockSkewInMs)', hooks => {
let fakeClock;
hooks.beforeEach(() => {
fakeClock = sinon.useFakeTimers();
});
hooks.afterEach(() => {
fakeClock.restore();
sinon.restore();
});

test('does not throw error if iat is undefined', assert => {
assert.equal(undefined, assertIssuedAtClaim(undefined, 0));
});
Expand Down

0 comments on commit 27b611e

Please sign in to comment.