Skip to content

Commit 9e89cd0

Browse files
committed
jest integration tests configs
1 parent bebd6fa commit 9e89cd0

File tree

5 files changed

+112
-21
lines changed

5 files changed

+112
-21
lines changed

jest.integration.config.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"testMatch": [
3+
"**/test/integration/**/*.spec.js"
4+
],
5+
"transform": {
6+
"^.+\\.js$": "babel-jest"
7+
},
8+
"transformIgnorePatterns": [
9+
"/node_modules/(?!chai|sinon)"
10+
],
11+
"moduleFileExtensions": [
12+
"js"
13+
],
14+
"globals": {
15+
"ts-jest": {
16+
"useESM": true
17+
}
18+
}
19+
}

jest.unit.config.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"testMatch": [
3+
"**/test/unit/**/*.spec.js"
4+
],
5+
"transform": {
6+
"^.+\\.js$": "babel-jest"
7+
},
8+
"transformIgnorePatterns": [
9+
"/node_modules/(?!chai|sinon)"
10+
],
11+
"moduleFileExtensions": [
12+
"js"
13+
],
14+
"globals": {
15+
"ts-jest": {
16+
"useESM": true
17+
}
18+
}
19+
}

package.json

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"build:debug": "export $(npm run -s set-version) && esbuild --bundle src/index.js --format=esm --outfile=dist/worker.mjs",
2121
"build:tsc": "tsc --build",
2222
"test": "npm run build:debug && node --test --experimental-vm-modules",
23-
"test:unit": "export $(npm run -s set-version) && node --experimental-vm-modules ./node_modules/.bin/jest",
23+
"test:unit": "export $(npm run -s set-version) && node --experimental-vm-modules ./node_modules/.bin/jest --config jest.unit.config.json",
24+
"test:integration": "export $(npm run -s set-version) && node --experimental-vm-modules ./node_modules/.bin/jest --config jest.integration.config.json",
2425
"lint": "standard"
2526
},
2627
"keywords": [
@@ -66,24 +67,5 @@
6667
"ignore": [
6768
"*.ts"
6869
]
69-
},
70-
"jest": {
71-
"testMatch": [
72-
"**/test/middlewares/**/*.spec.js"
73-
],
74-
"transform": {
75-
"^.+\\.js$": "babel-jest"
76-
},
77-
"transformIgnorePatterns": [
78-
"/node_modules/(?!chai|sinon)"
79-
],
80-
"moduleFileExtensions": [
81-
"js"
82-
],
83-
"globals": {
84-
"ts-jest": {
85-
"useESM": true
86-
}
87-
}
8870
}
8971
}

test/integration/index.it.spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fetchHandler from '../../src/index.js'
2+
import { jest } from '@jest/globals'
3+
import { RATE_LIMIT_EXCEEDED } from '../../src/constants.js'
4+
import { HttpError } from '@web3-storage/gateway-lib/util'
5+
6+
describe('Integration Test for fetch handler', () => {
7+
let env
8+
let rateLimiter
9+
let ctx
10+
11+
beforeEach(() => {
12+
rateLimiter = {
13+
limit: jest.fn()
14+
}
15+
env = {
16+
RATE_LIMITER: rateLimiter,
17+
ACCOUNTING_SERVICE_URL: 'http://example.com',
18+
AUTH_TOKEN_METADATA: {
19+
get: jest.fn(),
20+
put: jest.fn()
21+
}
22+
}
23+
ctx = {
24+
dataCid: 'test-cid'
25+
}
26+
})
27+
28+
afterEach(() => {
29+
jest.restoreAllMocks()
30+
})
31+
32+
it('should call the handler if rate limit is not exceeded', async () => {
33+
rateLimiter.limit.mockResolvedValue({ success: true })
34+
35+
const request = new Request('https://example.com', {
36+
method: 'GET',
37+
headers: {
38+
'Authorization': 'test-token'
39+
}
40+
})
41+
42+
const response = await fetchHandler.fetch(request, env, ctx)
43+
44+
expect(rateLimiter.limit).toHaveBeenCalledTimes(1)
45+
expect(rateLimiter.limit).toHaveBeenCalledWith({ key: ctx.dataCid.toString() })
46+
expect(response).toBeDefined()
47+
expect(response.status).toBe(200)
48+
})
49+
50+
it('should throw an error if rate limit is exceeded', async () => {
51+
rateLimiter.limit.mockResolvedValue({ success: false })
52+
53+
const request = new Request('https://example.com', {
54+
method: 'GET',
55+
headers: {
56+
'Authorization': 'test-token'
57+
}
58+
})
59+
60+
try {
61+
await fetchHandler.fetch(request, env, ctx)
62+
throw new Error('Expected error was not thrown')
63+
} catch (err) {
64+
expect(rateLimiter.limit).toHaveBeenCalledTimes(1)
65+
expect(rateLimiter.limit).toHaveBeenCalledWith({ key: ctx.dataCid.toString() })
66+
expect(err).toBeInstanceOf(HttpError)
67+
expect(err.message).toBe('Too Many Requests')
68+
}
69+
})
70+
71+
})

test/middlewares/rate-limiter.spec.js renamed to test/unit/middlewares/rate-limiter.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { jest } from '@jest/globals'
2-
import { withRateLimits } from '../../src/middleware.js'
2+
import { withRateLimits } from '../../../src/middleware.js'
33
import { HttpError } from '@web3-storage/gateway-lib/util'
44

55
describe('withRateLimits', () => {

0 commit comments

Comments
 (0)