Skip to content

Commit

Permalink
test: use time mock to achieve time acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
a20688392 committed Aug 9, 2023
1 parent 6ac5c60 commit 9fa9130
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/auth/jwt/jwt-access.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("JwtAccessGuard", () => {
let configService: ConfigService;

beforeEach(async () => {
jest.useFakeTimers();
const moduleRef = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
Expand All @@ -35,7 +36,7 @@ describe("JwtAccessGuard", () => {
});

it("should return true for a valid JWT", async () => {
const payload = { email: "testuser", id: 1 };
const payload = { id: 1 };
const secret: string | undefined = configService.get("jwtSecret.access");
const token = jwtService.sign(payload, {
expiresIn: "1h",
Expand All @@ -59,9 +60,11 @@ describe("JwtAccessGuard", () => {
});

it("should throw an error for an expired JWT", async () => {
const payload = { email: "testuser", id: 1 };
const payload = { id: 1 };
const secret: string | undefined = configService.get("jwtSecret.access");
const token = jwtService.sign(payload, { expiresIn: 0, secret });
const token = jwtService.sign(payload, { expiresIn: "1h", secret });

jest.advanceTimersByTime(60 * 60 * 1000);

const response = {};
const context: ExecutionContext = {
Expand All @@ -80,4 +83,8 @@ describe("JwtAccessGuard", () => {
expect(error).toBeInstanceOf(UnauthorizedException);
}
});

afterEach(async () => {
jest.clearAllTimers();
});
});

0 comments on commit 9fa9130

Please sign in to comment.