feat(api): s/jest/vitest/g (#61863)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
This commit is contained in:
Mrugesh Mohapatra
2025-08-25 22:57:56 +05:30
committed by GitHub
parent fed489f092
commit 45c098d506
58 changed files with 1974 additions and 907 deletions

View File

@@ -40,7 +40,7 @@ function createTestConnectionURL(url: string, dbId?: string) {
assert.ok(
dbId,
`dbId is required for test connection URL. Is this running in a test environment?
If so, ensure that the environment variable JEST_WORKER_ID is set.`
If so, ensure that the environment variable VITEST_WORKER_ID is set.`
);
return url.replace(/(.*)(\?.*)/, `$1${dbId}$2`);
}
@@ -162,7 +162,7 @@ export const MONGOHQ_URL =
process.env.NODE_ENV === 'test'
? createTestConnectionURL(
process.env.MONGOHQ_URL,
process.env.JEST_WORKER_ID
process.env.VITEST_WORKER_ID
)
: process.env.MONGOHQ_URL;

View File

@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { Exam, Question } from '@prisma/client';
import {
examJson,

View File

@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest';
import { getChallenges } from './get-challenges';
import { isObjectID } from './validation';

View File

@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest';
import { base64URLEncode, challenge, verifier } from '.';
describe('utils', () => {

View File

@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest';
import {
normalizeTwitter,
normalizeProfileUI,
@@ -9,17 +10,17 @@ import {
describe('normalize', () => {
describe('normalizeTwitter', () => {
it('returns the input if it is a url', () => {
test('returns the input if it is a url', () => {
const url = 'https://twitter.com/a_generic_user';
expect(normalizeTwitter(url)).toEqual(url);
});
it('adds the handle to twitter.com if it is not a url', () => {
test('adds the handle to twitter.com if it is not a url', () => {
const handle = '@a_generic_user';
expect(normalizeTwitter(handle)).toEqual(
'https://twitter.com/a_generic_user'
);
});
it('returns undefined if that is the input', () => {
test('returns undefined if that is the input', () => {
expect(normalizeTwitter('')).toBeUndefined();
});
});
@@ -51,16 +52,16 @@ describe('normalize', () => {
};
describe('normalizeProfileUI', () => {
it('should return the input if it is not null', () => {
test('should return the input if it is not null', () => {
expect(normalizeProfileUI(profileUIInput)).toEqual(profileUIInput);
});
it('should return the default profileUI if the input is null', () => {
test('should return the default profileUI if the input is null', () => {
const input = null;
expect(normalizeProfileUI(input)).toEqual(defaultProfileUI);
});
it('should convert all "null" values to "undefined"', () => {
test('should convert all "null" values to "undefined"', () => {
const input = {
isLocked: null,
showAbout: false,
@@ -89,7 +90,7 @@ describe('normalize', () => {
});
describe('normalizeChallenges', () => {
it('should remove null values from the input', () => {
test('should remove null values from the input', () => {
const completedChallenges = [
{
id: 'a6b0bb188d873cb2c8729495',
@@ -143,7 +144,7 @@ describe('normalize', () => {
});
describe('normalizeFlags', () => {
it('should replace nulls with false', () => {
test('should replace nulls with false', () => {
const flags = {
isLocked: null,
showAbout: false,
@@ -160,14 +161,14 @@ describe('normalize', () => {
});
describe('normalizeDate', () => {
it('should return the date as a number', () => {
test('should return the date as a number', () => {
expect(normalizeDate(1)).toEqual(1);
expect(normalizeDate({ $date: '2023-10-01T00:00:00Z' })).toEqual(
1696118400000
);
});
it('should throw an error if the date is not in the expected shape', () => {
test('should throw an error if the date is not in the expected shape', () => {
expect(() => normalizeDate('2023-10-01T00:00:00Z')).toThrow(
'Unexpected date value: "2023-10-01T00:00:00Z"'
);
@@ -178,13 +179,13 @@ describe('normalize', () => {
});
describe('normalizeChallengeType', () => {
it('should return the challenge type as a number or null', () => {
test('should return the challenge type as a number or null', () => {
expect(normalizeChallengeType(10)).toEqual(10);
expect(normalizeChallengeType('10')).toEqual(10);
expect(normalizeChallengeType(null)).toEqual(null);
});
it('should throw an error if the challenge type is not in the expected shape', () => {
test('should throw an error if the challenge type is not in the expected shape', () => {
expect(() => normalizeChallengeType('invalid')).toThrow(
'Unexpected challengeType value: "invalid"'
);

View File

@@ -1,12 +1,13 @@
import { describe, test, expect } from 'vitest';
import { getCalendar, getPoints } from './progress';
describe('utils/progress', () => {
describe('getCalendar', () => {
it('should return an empty object if no timestamps are passed', () => {
test('should return an empty object if no timestamps are passed', () => {
expect(getCalendar([])).toEqual({});
expect(getCalendar(null)).toEqual({});
});
it('should take timestamps and return a calendar object', () => {
test('should take timestamps and return a calendar object', () => {
const timestamps = [-1111001, 0, 1111000, 1111500, 1113000, 9999999];
expect(getCalendar(timestamps)).toEqual({
@@ -18,7 +19,7 @@ describe('utils/progress', () => {
});
});
it('should handle null, { timestamp: number } and float entries', () => {
test('should handle null, { timestamp: number } and float entries', () => {
const timestamps = [null, { timestamp: 1113000 }, 1111000.5];
expect(getCalendar(timestamps)).toEqual({
@@ -29,10 +30,10 @@ describe('utils/progress', () => {
});
describe('getPoints', () => {
it('should return 1 if there are no progressTimestamps', () => {
test('should return 1 if there are no progressTimestamps', () => {
expect(getPoints(null)).toEqual(1);
});
it('should return then number of progressTimestamps if there are any', () => {
test('should return then number of progressTimestamps if there are any', () => {
expect(getPoints([0, 1, 2])).toEqual(3);
});
});

View File

@@ -1,3 +1,4 @@
import { describe, test, expect, vi } from 'vitest';
import jwt from 'jsonwebtoken';
import {
@@ -26,7 +27,7 @@ const defaultObject = {
// TODO: tidy this up (the mocking is a bit of a mess)
describe('redirection', () => {
describe('getReturnTo', () => {
it('should extract returnTo from a jwt', () => {
test('should extract returnTo from a jwt', () => {
expect.assertions(1);
const encryptedReturnTo = jwt.sign(
@@ -41,10 +42,10 @@ describe('redirection', () => {
});
});
it('should return a default url if the secrets do not match', () => {
test('should return a default url if the secrets do not match', () => {
const oldLog = console.log;
expect.assertions(2);
console.log = jest.fn();
console.log = vi.fn();
const encryptedReturnTo = jwt.sign(
{ returnTo: validReturnTo },
invalidJWTSecret
@@ -56,7 +57,7 @@ describe('redirection', () => {
console.log = oldLog;
});
it('should return a default url for unknown origins', () => {
test('should return a default url for unknown origins', () => {
expect.assertions(1);
const encryptedReturnTo = jwt.sign(
{ returnTo: invalidReturnTo },
@@ -68,18 +69,18 @@ describe('redirection', () => {
});
});
describe('normalizeParams', () => {
it('should return a {returnTo, origin, pathPrefix} object', () => {
test('should return a {returnTo, origin, pathPrefix} object', () => {
expect.assertions(2);
const keys = Object.keys(normalizeParams({}));
const expectedKeys = ['returnTo', 'origin', 'pathPrefix'];
expect(keys.length).toBe(3);
expect(keys).toEqual(expect.arrayContaining(expectedKeys));
});
it('should default to process.env.HOME_LOCATION', () => {
test('should default to process.env.HOME_LOCATION', () => {
expect.assertions(1);
expect(normalizeParams({}, defaultOrigin)).toEqual(defaultObject);
});
it('should convert an unknown pathPrefix to ""', () => {
test('should convert an unknown pathPrefix to ""', () => {
expect.assertions(1);
const brokenPrefix = {
...defaultObject,
@@ -89,7 +90,7 @@ describe('redirection', () => {
defaultObject
);
});
it('should not change a known pathPrefix', () => {
test('should not change a known pathPrefix', () => {
expect.assertions(1);
const spanishPrefix = {
...defaultObject,
@@ -103,7 +104,7 @@ describe('redirection', () => {
// process.env.HOME_LOCATION/path, but if the origin is wrong something unexpected is
// going on. In that case it's probably best to just send them to
// process.env.HOME_LOCATION/learn.
it('should return default parameters if the origin is unknown', () => {
test('should return default parameters if the origin is unknown', () => {
expect.assertions(1);
const exampleOrigin = {
...defaultObject,
@@ -114,7 +115,7 @@ describe('redirection', () => {
defaultObject
);
});
it('should return default parameters if the returnTo is unknown', () => {
test('should return default parameters if the returnTo is unknown', () => {
expect.assertions(1);
const exampleReturnTo = {
...defaultObject,
@@ -126,7 +127,7 @@ describe('redirection', () => {
);
});
it('should reject returnTo without trailing slashes', () => {
test('should reject returnTo without trailing slashes', () => {
const exampleReturnTo = {
...defaultObject,
returnTo: 'https://www.freecodecamp.dev'
@@ -136,7 +137,7 @@ describe('redirection', () => {
);
});
it('should not modify the returnTo if it is valid', () => {
test('should not modify the returnTo if it is valid', () => {
const exampleReturnTo = {
...defaultObject,
returnTo: 'https://www.freecodecamp.dev/'
@@ -148,7 +149,7 @@ describe('redirection', () => {
});
describe('getRedirectParams', () => {
it('should return origin, pathPrefix and returnTo given valid headers', () => {
test('should return origin, pathPrefix and returnTo given valid headers', () => {
const req = {
headers: {
referer: `https://www.freecodecamp.org/espanol/learn/rosetta-code/`
@@ -165,7 +166,7 @@ describe('redirection', () => {
expect(result).toEqual(expectedReturn);
});
it('should strip off any query parameters from the referer', () => {
test('should strip off any query parameters from the referer', () => {
const req = {
headers: {
referer: `https://www.freecodecamp.org/espanol/learn/rosetta-code/?query=param`
@@ -182,7 +183,7 @@ describe('redirection', () => {
expect(result).toEqual(expectedReturn);
});
it('should returnTo the origin if the referer is missing', () => {
test('should returnTo the origin if the referer is missing', () => {
const req = {
headers: {}
};
@@ -197,7 +198,7 @@ describe('redirection', () => {
expect(result).toEqual(expectedReturn);
});
it('should returnTo the origin if the referrer is invalid', () => {
test('should returnTo the origin if the referrer is invalid', () => {
const req = {
headers: {
referer: 'invalid-url'
@@ -216,7 +217,7 @@ describe('redirection', () => {
});
describe('getLoginRedirectParams', () => {
it('should use the login-returnto cookie if present', () => {
test('should use the login-returnto cookie if present', () => {
const mockReq = {
cookies: {
'login-returnto': 'https://www.freecodecamp.org/espanol/learn'
@@ -236,25 +237,25 @@ describe('redirection', () => {
});
describe('getPrefixedLandingPath', () => {
it('should return the origin when no pathPrefix is provided', () => {
test('should return the origin when no pathPrefix is provided', () => {
const result = getPrefixedLandingPath(defaultOrigin);
expect(result).toEqual(defaultOrigin);
});
it('should append pathPrefix to origin when pathPrefix is provided', () => {
test('should append pathPrefix to origin when pathPrefix is provided', () => {
const expectedPath = `${defaultOrigin}/learn`;
const result = getPrefixedLandingPath(defaultOrigin, 'learn');
expect(result).toEqual(expectedPath);
});
it('should handle empty origin', () => {
test('should handle empty origin', () => {
const pathPrefix = 'learn';
const expectedPath = '/learn';
const result = getPrefixedLandingPath('', pathPrefix);
expect(result).toEqual(expectedPath);
});
it('should handle empty pathPrefix', () => {
test('should handle empty pathPrefix', () => {
const result = getPrefixedLandingPath(defaultOrigin, '');
expect(result).toEqual(defaultOrigin);
});

View File

@@ -1,9 +1,11 @@
jest.useFakeTimers();
import { describe, test, expect, vi } from 'vitest';
vi.useFakeTimers();
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { createAccessToken, createAuthToken, isExpired } from './tokens';
describe('createAccessToken', () => {
it('creates an object with id, ttl, created and userId', () => {
test('creates an object with id, ttl, created and userId', () => {
const userId = 'abc';
const actual = createAccessToken(userId);
@@ -18,7 +20,7 @@ describe('createAccessToken', () => {
});
});
it('sets the ttl, defaulting to 77760000000 ms', () => {
test('sets the ttl, defaulting to 77760000000 ms', () => {
const userId = 'abc';
const ttl = 123;
const actual = createAccessToken(userId, ttl);
@@ -29,7 +31,7 @@ describe('createAccessToken', () => {
});
describe('createAuthToken', () => {
it('creates an object with id, ttl, created and userId', () => {
test('creates an object with id, ttl, created and userId', () => {
const userId = 'abc';
const actual = createAuthToken(userId);
@@ -44,7 +46,7 @@ describe('createAuthToken', () => {
});
});
it('sets the ttl, defaulting to 900000 ms', () => {
test('sets the ttl, defaulting to 900000 ms', () => {
const userId = 'abc';
const ttl = 123;
const actual = createAuthToken(userId, ttl);
@@ -55,23 +57,23 @@ describe('createAuthToken', () => {
});
describe('isExpired', () => {
it('returns true if the token expiry date is in the past', () => {
test('returns true if the token expiry date is in the past', () => {
const token = createAccessToken('abc', 1000);
expect(isExpired(token)).toBe(false);
jest.advanceTimersByTime(500);
vi.advanceTimersByTime(500);
expect(isExpired(token)).toBe(false);
jest.advanceTimersByTime(500);
vi.advanceTimersByTime(500);
expect(isExpired(token)).toBe(false);
jest.advanceTimersByTime(1);
vi.advanceTimersByTime(1);
expect(isExpired(token)).toBe(true);
});
it('handles tokens with Date values for created', () => {
test('handles tokens with Date values for created', () => {
const token = { ...createAccessToken('abc', 2000), created: new Date() };
expect(isExpired(token)).toBe(false);
jest.advanceTimersByTime(2000);
vi.advanceTimersByTime(2000);
expect(isExpired(token)).toBe(false);
jest.advanceTimersByTime(1);
vi.advanceTimersByTime(1);
expect(isExpired(token)).toBe(true);
});
});

View File

@@ -1,12 +1,13 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import { inLastFiveMinutes } from './validate-donation';
describe('inLastFiveMinutes', () => {
beforeAll(() => {
jest.useFakeTimers();
vi.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
vi.useRealTimers();
});
it('should return true if the timestamp is within the last five minutes', () => {

View File

@@ -1,3 +1,4 @@
import { describe, it, expect } from 'vitest';
import { isObjectID } from './validation';
describe('Validation', () => {