Skip to content

Commit a31248b

Browse files
committed
Added tests
1 parent 0df86d8 commit a31248b

File tree

6 files changed

+9175
-1302
lines changed

6 files changed

+9175
-1302
lines changed

__tests__/ratelimit.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import nock from "nock";
2+
import { Ratelimit } from "../src/index";
3+
import { describe, it, expect, beforeAll } from "@jest/globals";
4+
5+
describe("Ratelimit", () => {
6+
let ratelimit: Ratelimit;
7+
8+
beforeAll(() => {
9+
ratelimit = new Ratelimit({
10+
url: "https://fake-redis-url",
11+
token: "fake-token",
12+
time: "10 s",
13+
maxRequests: 1,
14+
logging: true,
15+
whitelist: ["127.0.0.1"],
16+
blacklist: ["203.0.113.1"],
17+
blacklistConfig: {
18+
blockDuration: 7200 * 1000,
19+
message: "Your IP has been blacklisted.",
20+
},
21+
});
22+
});
23+
24+
it("should allow whitelisted IP", async () => {
25+
const result = await ratelimit.limit("127.0.0.1");
26+
expect(result.success).toBe(true);
27+
});
28+
29+
it("should block blacklisted IP", async () => {
30+
const result = await ratelimit.limit("203.0.113.1");
31+
expect(result.success).toBe(false);
32+
});
33+
34+
it("should rate limit IP", async () => {
35+
nock("https://fake-redis-url")
36+
.post("/")
37+
.reply(200, { success: true, reset: Date.now() + 10000 });
38+
39+
const result = await ratelimit.limit("192.168.1.1");
40+
expect(result.success).toBe(true);
41+
}, 10000);
42+
});

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
transform: {
4+
'^.+\\.tsx?$': 'ts-jest',
5+
},
6+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
7+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
8+
};

0 commit comments

Comments
 (0)