Skip to content

Commit 69ac2d8

Browse files
committed
use vitest instead of jest
1 parent 04e4550 commit 69ac2d8

15 files changed

+874
-2099
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
dist
1+
dist
2+
vitest.config.ts

jest.config.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"README.md"
1414
],
1515
"scripts": {
16-
"test": "jest",
16+
"test": "vitest run",
1717
"build": "run-p build:*",
1818
"build:main": "tsc -p tsconfig.main.json",
1919
"build:module": "tsc -p tsconfig.module.json",
@@ -27,8 +27,7 @@
2727
},
2828
"dependencies": {},
2929
"devDependencies": {
30-
"@cloudflare/workers-types": "^3.14.0",
31-
"@types/jest": "^28.1.3",
30+
"@cloudflare/workers-types": "^4.20231025.0",
3231
"@typescript-eslint/eslint-plugin": "^5.30.5",
3332
"@typescript-eslint/parser": "^5.30.5",
3433
"eslint": "^8.19.0",
@@ -38,13 +37,12 @@
3837
"eslint-plugin-eslint-comments": "^3.2.0",
3938
"eslint-plugin-import": "^2.26.0",
4039
"firebase-tools": "^11.2.0",
41-
"jest": "^28.1.2",
42-
"jest-environment-miniflare": "^2.5.1",
40+
"miniflare": "^3.20231025.1",
4341
"npm-run-all": "^4.1.5",
4442
"prettier": "^2.7.1",
45-
"ts-jest": "^28.0.5",
46-
"typescript": "^4.7.4",
47-
"wrangler": "^2.0.16"
43+
"typescript": "^5.2.2",
44+
"vitest": "^0.34.6",
45+
"wrangler": "^3.15.0"
4846
},
4947
"keywords": [
5048
"web",

tests/base64.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, it, expect } from 'vitest';
12
import { decodeBase64Url, encodeBase64Url } from '../src/base64';
23
import { utf8Encoder } from '../src/utf8';
34

tests/global.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/jwk-fetcher.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Miniflare } from 'miniflare';
2+
import { describe, it, expect, vi } from 'vitest';
13
import type { Fetcher } from '../src/jwk-fetcher';
24
import { parseMaxAge, UrlKeyFetcher } from '../src/jwk-fetcher';
35
import { WorkersKVStore } from '../src/key-store';
@@ -10,7 +12,12 @@ class HTTPMockFetcher implements Fetcher {
1012
}
1113
}
1214

13-
const { TEST_NAMESPACE } = getMiniflareBindings();
15+
const nullScript = 'export default { fetch: () => new Response(null, { status: 404 }) };';
16+
const mf = new Miniflare({
17+
modules: true,
18+
script: nullScript,
19+
kvNamespaces: ['TEST_NAMESPACE'],
20+
});
1421

1522
const validResponseJSON = `{
1623
"keys": [
@@ -62,9 +69,10 @@ describe('UrlKeyFetcher', () => {
6269
},
6370
})
6471
);
72+
const TEST_NAMESPACE = await mf.getKVNamespace('TEST_NAMESPACE');
6573
const urlKeyFetcher = new UrlKeyFetcher(mockedFetcher, new WorkersKVStore(cacheKey, TEST_NAMESPACE));
6674

67-
const httpFetcherSpy = jest.spyOn(mockedFetcher, 'fetch');
75+
const httpFetcherSpy = vi.spyOn(mockedFetcher, 'fetch');
6876

6977
// first call (no-cache in KV)
7078
const firstKeys = await urlKeyFetcher.fetchPublicKeys();
@@ -92,9 +100,10 @@ describe('UrlKeyFetcher', () => {
92100
headers: {},
93101
})
94102
);
103+
const TEST_NAMESPACE = await mf.getKVNamespace('TEST_NAMESPACE');
95104
const urlKeyFetcher = new UrlKeyFetcher(mockedFetcher, new WorkersKVStore(cacheKey, TEST_NAMESPACE));
96105

97-
const httpFetcherSpy = jest.spyOn(mockedFetcher, 'fetch');
106+
const httpFetcherSpy = vi.spyOn(mockedFetcher, 'fetch');
98107

99108
// first call (no-cache in KV)
100109
const firstKeys = await urlKeyFetcher.fetchPublicKeys();
@@ -107,36 +116,38 @@ describe('UrlKeyFetcher', () => {
107116
expect(httpFetcherSpy).toBeCalledTimes(2);
108117
});
109118

110-
it('internal server error fetch', () => {
119+
it('internal server error fetch', async () => {
111120
const cacheKey = 'failed-fetch-flow-key';
112121
const internalServerMsg = 'Internal Server Error';
113122
const mockedFetcher = new HTTPMockFetcher(
114123
new Response(internalServerMsg, {
115124
status: 500,
116125
})
117126
);
127+
const TEST_NAMESPACE = await mf.getKVNamespace('TEST_NAMESPACE');
118128
const urlKeyFetcher = new UrlKeyFetcher(mockedFetcher, new WorkersKVStore(cacheKey, TEST_NAMESPACE));
119129

120130
expect(() => urlKeyFetcher.fetchPublicKeys()).rejects.toThrowError(
121131
'Error fetching public keys for Google certs: ' + internalServerMsg
122132
);
123133
});
124134

125-
it('ok fetch but got text response', () => {
135+
it('ok fetch but got text response', async () => {
126136
const cacheKey = 'ok-fetch-non-json-flow-key';
127137
const mockedFetcher = new HTTPMockFetcher(
128138
new Response('{}', {
129139
status: 200,
130140
})
131141
);
142+
const TEST_NAMESPACE = await mf.getKVNamespace('TEST_NAMESPACE');
132143
const urlKeyFetcher = new UrlKeyFetcher(mockedFetcher, new WorkersKVStore(cacheKey, TEST_NAMESPACE));
133144

134145
expect(() => urlKeyFetcher.fetchPublicKeys()).rejects.toThrowError('The public keys are not an object or null:');
135146
});
136147
});
137148

138149
describe('parseMaxAge', () => {
139-
test.each([
150+
it.each([
140151
['valid simple', 'max-age=604800', 604800],
141152
['valid with other directives', 'public, max-age=18793, must-revalidate, no-transform', 18793],
142153
['invalid cache-control header is null', null, NaN],

tests/jws-verifier.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, it, expect } from 'vitest';
12
import { JwtError, JwtErrorCode } from '../src/errors';
23
import { PublicKeySignatureVerifier, rs256alg } from '../src/jws-verifier';
34
import type { DecodedPayload } from '../src/jwt-decoder';

tests/jwt-decoder.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, it, expect } from 'vitest';
12
import { JwtError, JwtErrorCode } from '../src/errors';
23
import type { DecodedHeader, DecodedPayload } from '../src/jwt-decoder';
34
import { RS256Token } from '../src/jwt-decoder';
@@ -51,7 +52,7 @@ describe('TokenDecoder', () => {
5152
});
5253

5354
describe('payload', () => {
54-
test.each([
55+
it.each([
5556
[
5657
'aud',
5758
{

tests/setup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import crypto from 'node:crypto';
2+
import { vi } from 'vitest';
3+
4+
vi.stubGlobal('crypto', crypto);

tests/token-verifier.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, it, expect } from 'vitest';
12
import { PublicKeySignatureVerifier, rs256alg } from '../src/jws-verifier';
23
import type { FirebaseIdToken } from '../src/token-verifier';
34
import { createFirebaseTokenVerifier, FIREBASE_AUDIENCE } from '../src/token-verifier';
@@ -22,7 +23,7 @@ describe('FirebaseTokenVerifier', () => {
2223
},
2324
};
2425

25-
test.each([
26+
it.each([
2627
['valid without firebase emulator', payload],
2728
[
2829
'valid custom token without firebase emulator',
@@ -60,7 +61,7 @@ describe('FirebaseTokenVerifier', () => {
6061
expect(token).toStrictEqual(payload);
6162
});
6263

63-
test.each([
64+
it.each([
6465
[
6566
'aud',
6667
{

0 commit comments

Comments
 (0)