Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions test/js/third_party/jsonwebtoken/async_sign.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "bun:test";
import { afterEach, beforeEach, describe, expect, it, setSystemTime } from "bun:test";
import { generateKeyPairSync } from "crypto";
import jwt from "jsonwebtoken";
import jws from "jws";
Expand All @@ -8,13 +8,30 @@ describe("signing a token asynchronously", function () {
describe("when signing a token", function () {
var secret = "shhhhhh";

// Each sign() reads Date.now() for `iat`. Freeze the clock so the async
// and the sync token cannot differ when a second boundary falls between them.
beforeEach(function () {
setSystemTime(60000);
});

afterEach(function () {
setSystemTime();
});

it("should return the same result as singing synchronously", function (done) {
jwt.sign({ foo: "bar" }, secret, { algorithm: "HS256" }, function (err, asyncToken) {
if (err) return done(err);
var syncToken = jwt.sign({ foo: "bar" }, secret, { algorithm: "HS256" });
expect(typeof asyncToken).toBe("string");
expect(asyncToken.split(".")).toHaveLength(3);
expect(asyncToken).toEqual(syncToken);
// jws runs this callback inside a try/catch and routes a throw to the
// 'error' listener, which jsonwebtoken wrapped in once(). A failed
// expect() here is swallowed and the test only times out. Report it.
try {
var syncToken = jwt.sign({ foo: "bar" }, secret, { algorithm: "HS256" });
expect(typeof asyncToken).toBe("string");
expect(asyncToken.split(".")).toHaveLength(3);
expect(asyncToken).toEqual(syncToken);
} catch (e) {
return done(e);
}
done();
});
});
Expand Down
Loading