Skip to content

Commit

Permalink
replace logger devdep with mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
blaine-arcjet committed Jun 11, 2024
1 parent e92bf53 commit fa5fed8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
30 changes: 27 additions & 3 deletions arcjet/test/index.edge.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/**
* @jest-environment @edge-runtime/jest-environment
*/
import { describe, expect, test, jest } from "@jest/globals";
import {
describe,
expect,
test,
jest,
beforeEach,
afterEach,
} from "@jest/globals";

import arcjet, {
rateLimit,
Expand All @@ -11,11 +18,28 @@ import arcjet, {
ArcjetReason,
ArcjetAllowDecision,
} from "../index";
import { Logger } from "@arcjet/logger";

beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
jest.clearAllTimers();
jest.clearAllMocks();
jest.restoreAllMocks();
});

class ArcjetTestReason extends ArcjetReason {}

const log = new Logger({ level: "info" });
const log = {
time: jest.fn(),
timeEnd: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

describe("Arcjet: Env = Edge runtime", () => {
test("should create a new instance", async () => {
Expand Down
22 changes: 12 additions & 10 deletions arcjet/test/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
RuleResult,
RuleState,
} from "@arcjet/protocol/proto";
import { Logger } from "@arcjet/logger";

import arcjet, {
ArcjetDecision,
Expand Down Expand Up @@ -164,7 +163,14 @@ class ArcjetInvalidDecision extends ArcjetDecision {
}
}

const log = new Logger({ level: "info" });
const log = {
time: jest.fn(),
timeEnd: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

describe("createRemoteClient", () => {
const defaultRemoteClientOptions = {
Expand Down Expand Up @@ -4123,8 +4129,6 @@ describe("SDK", () => {
"extra-test": "extra-test-value",
};

const errorLogSpy = jest.spyOn(log, "error");

function testRuleLocalThrowString(): ArcjetLocalRule {
return {
mode: ArcjetMode.LIVE,
Expand All @@ -4146,8 +4150,8 @@ describe("SDK", () => {

const _ = await aj.protect({}, request);

expect(errorLogSpy).toHaveBeenCalledTimes(1);
expect(errorLogSpy).toHaveBeenCalledWith(
expect(log.error).toHaveBeenCalledTimes(1);
expect(log.error).toHaveBeenCalledWith(
"Failure running rule: %s due to %s",
"TEST_RULE_LOCAL_THROW_STRING",
"Local rule protect failed",
Expand Down Expand Up @@ -4176,8 +4180,6 @@ describe("SDK", () => {
"extra-test": "extra-test-value",
};

const errorLogSpy = jest.spyOn(log, "error");

function testRuleLocalThrowNull(): ArcjetLocalRule {
return {
mode: ArcjetMode.LIVE,
Expand All @@ -4199,8 +4201,8 @@ describe("SDK", () => {

const _ = await aj.protect({}, request);

expect(errorLogSpy).toHaveBeenCalledTimes(1);
expect(errorLogSpy).toHaveBeenCalledWith(
expect(log.error).toHaveBeenCalledTimes(1);
expect(log.error).toHaveBeenCalledWith(
"Failure running rule: %s due to %s",
"TEST_RULE_LOCAL_THROW_NULL",
"Unknown problem",
Expand Down

0 comments on commit fa5fed8

Please sign in to comment.