Skip to content

Commit 0fd1471

Browse files
committed
use miniflare for better local testing env
1 parent 6a1012d commit 0fd1471

File tree

5 files changed

+197
-15
lines changed

5 files changed

+197
-15
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"eslint-plugin-no-async-without-await": "^1.2.0",
1919
"eslint-plugin-prettier": "^4.2.1",
2020
"jest": "^29.3.1",
21+
"jest-environment-miniflare": "^2.11.0",
2122
"openai": "^3.1.0",
2223
"prettier": "^2.8.1",
2324
"ts-jest": "^29.0.3",

src/cache.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import { getCacheKey } from './cache';
2-
import { objectHash } from './utils';
3-
4-
jest.mock('./utils.ts');
2+
import * as utils from './utils';
53

64
describe('cache', () => {
75
describe('getCacheKey', () => {
86
beforeEach(() => {
9-
(objectHash as jest.Mock).mockReturnValue('unique-sha-hash');
7+
jest.spyOn(utils, 'objectHash');
108
});
119

1210
it('returns a hash uniquely representing the params', async () => {
1311
const params = {
1412
method: 'POST',
1513
path: '/v1/completions',
16-
authHeader: 'api-key',
14+
authHeader: 'api-key-!',
1715
body: '{ "ok": true }',
1816
};
1917
const result = await getCacheKey(params);
20-
expect(objectHash).toHaveBeenCalledWith(params);
21-
expect(result).toEqual('unique-sha-hash');
18+
expect(utils.objectHash).toHaveBeenCalledWith(params);
19+
expect(result).toEqual('1a97143b720b3d82cf93a34a5a02c61de1492b18d813f4f1859251eef1a738be');
2220
});
2321

2422
it('removes null or empty values', async () => {
@@ -29,11 +27,11 @@ describe('cache', () => {
2927
body: '',
3028
};
3129
const result = await getCacheKey(params);
32-
expect(objectHash).toHaveBeenCalledWith({
30+
expect(utils.objectHash).toHaveBeenCalledWith({
3331
method: 'POST',
3432
path: '/v1/completions',
3533
});
36-
expect(result).toEqual('unique-sha-hash');
34+
expect(result).toEqual('771798e93f4fbad36b4c89cd88d3752383224caf8e585661dc174feb25939f75');
3735
});
3836
});
3937
});

src/utils.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getHeadersAsObject } from './utils';
2+
3+
describe('utils', () => {
4+
describe('getHeadersAsObject', () => {
5+
it('returns Headers instance in Object format', () => {
6+
const sampleHeaders = new Headers({
7+
'content-type': 'application/json',
8+
Authorization: 'pedro martinez',
9+
});
10+
expect(getHeadersAsObject(sampleHeaders)).toEqual({
11+
authorization: 'pedro martinez',
12+
'content-type': 'application/json',
13+
});
14+
});
15+
});
16+
});

test/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = {
44
'\\.ts$': 'ts-jest',
55
},
66
clearMocks: true,
7-
testEnvironment: 'node',
7+
testEnvironment: 'miniflare',
88
setupFilesAfterEnv: ['./test/jest.setup.ts'],
99
};

0 commit comments

Comments
 (0)