Skip to content

Commit 52dadcc

Browse files
committed
[CARE-3589] Switch tests to vitest
- it's faster - it's working with ESM+TS better than jest/ts-jest
1 parent 4e96a19 commit 52dadcc

File tree

9 files changed

+6839
-11448
lines changed

9 files changed

+6839
-11448
lines changed

jest.config.cjs

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

package-lock.json

Lines changed: 6706 additions & 11335 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
3737
"@babel/preset-env": "^7.7.1",
3838
"@babel/preset-typescript": "^7.18.6",
39-
"@types/jest": "^29.5.11",
40-
"@types/node": "^12.12.9",
39+
"@types/node": "^18.19.3",
4140
"@typescript-eslint/eslint-plugin": "^6.13.2",
4241
"@typescript-eslint/parser": "^6.13.2",
4342
"babel-plugin-add-import-extension": "^1.6.0",
@@ -46,13 +45,12 @@
4645
"eslint": "^8.25.0",
4746
"eslint-config-prettier": "^8.5.0",
4847
"eslint-plugin-import": "^2.26.0",
49-
"jest": "^29.7.0",
50-
"jest-fetch-mock": "^3.0.3",
5148
"np": "^7.2.0",
5249
"prettier": "^2.7.1",
5350
"rimraf": "^3.0.0",
54-
"ts-jest": "^29.1.1",
55-
"typescript": "^5.3.3"
51+
"typescript": "^5.3.3",
52+
"vitest": "^1.0.4",
53+
"vitest-fetch-mock": "^0.2.2"
5654
},
5755
"scripts": {
5856
"clean": "rimraf dist node_modules",
@@ -66,10 +64,9 @@
6664
"lint": "eslint ./src --ext=.ts",
6765
"lint:fix": "npm run lint -- --fix",
6866
"test": "npm run test:build && npm run test:ts && npm run test:unit",
69-
"test:unit": "NODE_OPTIONS=--experimental-vm-modules jest",
67+
"test:unit": "vitest run",
7068
"test:ts": "find src/ -name '*.test.ts' | xargs -I{} tsc --noEmit --strict {}",
7169
"test:build": "node dist/index.js && tsc --noEmit dist/index.d.ts",
72-
"snapshot": "jest --updateSnapshot",
7370
"prettier:check": "prettier --check './src/**/*.{ts,js}'",
7471
"prettier:fix": "prettier --write './src/**/*.{ts,js}'",
7572
"prerelease": "npm run build",

setupJest.cjs

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Client.test.js renamed to src/Client.test.ts

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { readFileSync } from 'fs';
2-
3-
const { version: packageVersion, repository } = JSON.parse(readFileSync('package.json'));
2+
import { vi, beforeEach, describe, expect, it, afterEach } from 'vitest';
3+
import type { MockResponseInitFunction } from 'vitest-fetch-mock';
4+
import createFetchMock from 'vitest-fetch-mock';
45

56
import { createClient } from './Client';
67
import { Method } from './http';
8+
import type { CoverageEntry } from './types';
9+
10+
const { version: packageVersion, repository } = JSON.parse(readFileSync('package.json').toString());
11+
12+
const fetch = createFetchMock(vi);
713

814
const BASE_URL = 'https://api.prezly.com';
915
const DEFAULT_USER_AGENT = `prezly-javascript-sdk/${packageVersion} (+${repository.url})`;
@@ -18,12 +24,15 @@ const DEFAULT_REQUEST_PROPS = {
1824
},
1925
};
2026

21-
function successJsonResponse(body, status = 200) {
22-
return new Response(JSON.stringify(body), {
23-
status,
24-
statusText: 'OK',
25-
headers: {
26-
'Content-Type': 'application/json',
27+
function successJsonResponse(body: any, status = 200): MockResponseInitFunction {
28+
return () => ({
29+
body: JSON.stringify(body),
30+
init: {
31+
status,
32+
statusText: 'OK',
33+
headers: {
34+
'Content-Type': 'application/json',
35+
},
2736
},
2837
});
2938
}
@@ -34,23 +43,30 @@ describe('Client', () => {
3443

3544
beforeEach(() => {
3645
fetch.resetMocks();
46+
fetch.enableMocks();
47+
});
48+
49+
afterEach(() => {
50+
fetch.disableMocks();
3751
});
3852

3953
describe('Coverage', () => {
40-
const getListPayload = (coverage) => ({
41-
sort: '-published_at',
42-
pagination: {
43-
offset: 0,
44-
limit: 20,
45-
matched_records_number: coverage.length,
46-
total_records_number: coverage.length,
47-
},
48-
coverage,
49-
});
54+
function getListPayload(coverage: Partial<CoverageEntry>[]) {
55+
return {
56+
sort: '-published_at',
57+
pagination: {
58+
offset: 0,
59+
limit: 20,
60+
matched_records_number: coverage.length,
61+
total_records_number: coverage.length,
62+
},
63+
coverage,
64+
};
65+
}
5066

5167
it('should call the list endpoint', async () => {
5268
const expectedPayload = getListPayload([]);
53-
fetch.mockResolvedValueOnce(successJsonResponse(expectedPayload));
69+
fetch.mockResponseOnce(successJsonResponse(expectedPayload));
5470

5571
const prezlySdk = createClient({
5672
accessToken: ACCESS_TOKEN,
@@ -66,7 +82,7 @@ describe('Client', () => {
6682

6783
it('should call the GET :id endpoint', async () => {
6884
const coverage = { id: 123 };
69-
fetch.mockResolvedValueOnce(successJsonResponse({ coverage }));
85+
fetch.mockResponseOnce(successJsonResponse({ coverage }));
7086

7187
const prezlySdk = createClient({
7288
accessToken: ACCESS_TOKEN,
@@ -82,9 +98,10 @@ describe('Client', () => {
8298

8399
it('should allow filtering by externalReferenceId', async () => {
84100
const externalReferenceId = 'external-ref-id';
101+
85102
const coverage = { id: 123 };
86103
const expectedPayload = getListPayload([coverage]);
87-
fetch.mockResolvedValueOnce(successJsonResponse(expectedPayload));
104+
fetch.mockResponseOnce(successJsonResponse(expectedPayload));
88105

89106
const prezlySdk = createClient({
90107
accessToken: ACCESS_TOKEN,
@@ -101,7 +118,7 @@ describe('Client', () => {
101118

102119
it('should return null from externalReferenceId no results found', async () => {
103120
const expectedPayload = getListPayload([]);
104-
fetch.mockResolvedValueOnce(successJsonResponse(expectedPayload));
121+
fetch.mockResponseOnce(successJsonResponse(expectedPayload));
105122

106123
const prezlySdk = createClient({
107124
accessToken: ACCESS_TOKEN,
@@ -115,7 +132,7 @@ describe('Client', () => {
115132
const coverage = {
116133
url: 'https://example.com',
117134
};
118-
fetch.mockResolvedValueOnce(successJsonResponse({ coverage }));
135+
fetch.mockResponseOnce(successJsonResponse({ coverage }));
119136

120137
const prezlySdk = createClient({
121138
accessToken: ACCESS_TOKEN,
@@ -135,7 +152,7 @@ describe('Client', () => {
135152
const coverage = {
136153
url: 'https://prezly.com',
137154
};
138-
fetch.mockResolvedValueOnce(successJsonResponse({ coverage }));
155+
fetch.mockResponseOnce(successJsonResponse({ coverage }));
139156

140157
const prezlySdk = createClient({
141158
accessToken: ACCESS_TOKEN,
@@ -152,7 +169,7 @@ describe('Client', () => {
152169

153170
it('should call delete endpoint', async () => {
154171
const id = 123;
155-
fetch.mockResolvedValueOnce(successJsonResponse(undefined, 204));
172+
fetch.mockResponseOnce(successJsonResponse(undefined, 204));
156173

157174
const prezlySdk = createClient({
158175
accessToken: ACCESS_TOKEN,
@@ -169,7 +186,7 @@ describe('Client', () => {
169186

170187
describe('custom options', () => {
171188
it('should allow overriding the url', async () => {
172-
fetch.mockResolvedValueOnce(successJsonResponse({}));
189+
fetch.mockResponseOnce(successJsonResponse({}));
173190

174191
// Intentionally using trailing `/` to test url sanitizing.
175192
const baseUrl = 'https://api.prezly.test/';
@@ -186,7 +203,7 @@ describe('Client', () => {
186203
});
187204

188205
it('should allow overriding the headers', async () => {
189-
fetch.mockResolvedValueOnce(successJsonResponse({}));
206+
fetch.mockResponseOnce(successJsonResponse({}));
190207

191208
const customHeaders = {
192209
'User-Agent': 'Test',

0 commit comments

Comments
 (0)