Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions __tests__/confidentialClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {ConfidentialClient} from '../src';
import {OpenIDClientFactory} from '../src/openIDClientFactory';
import {HttpsProxyAgent} from 'https-proxy-agent';

jest.mock('../src/openIDClientFactory');
vi.mock('../src/openIDClientFactory');

describe('test ConfidentialClient class', () => {
beforeEach(() => {
jest.resetAllMocks();
vi.resetAllMocks();
});

describe('test instanciating', () => {
Expand All @@ -27,9 +27,9 @@ describe('test ConfidentialClient class', () => {

describe('test getAccessToken function', () => {
test('check access token logic', async () => {
jest.useFakeTimers();
vi.useFakeTimers();

const mockGrant = jest.fn().mockImplementation(() => {
const mockGrant = vi.fn().mockImplementation(() => {
const date = Math.floor(Date.now() / 1000);

return Promise.resolve({
Expand All @@ -38,31 +38,31 @@ describe('test ConfidentialClient class', () => {
});
});

jest.mocked(OpenIDClientFactory.getClient).mockResolvedValue({
vi.mocked(OpenIDClientFactory.getClient).mockResolvedValue({
grant: mockGrant,
} as unknown as Client);

const cf = new ConfidentialClient('./__tests__/fixtures/validConfig.json');

jest.setSystemTime(new Date('2020-01-01T12:00:00+00:00'));
vi.setSystemTime(new Date('2020-01-01T12:00:00+00:00'));
const token1 = await cf.getAccessToken();
expect(token1).toBe('test_token 1577880900');
expect(mockGrant).toHaveBeenCalledTimes(1);

jest.setSystemTime(new Date('2020-01-01T12:10:00+00:00'));
vi.setSystemTime(new Date('2020-01-01T12:10:00+00:00'));
const token2 = await cf.getAccessToken();
expect(token2).toBe('test_token 1577880900');
expect(mockGrant).toHaveBeenCalledTimes(1);

jest.setSystemTime(new Date('2020-01-01T12:30:00+00:00'));
vi.setSystemTime(new Date('2020-01-01T12:30:00+00:00'));
const token3 = await cf.getAccessToken();
expect(token3).toBe('test_token 1577882700');
expect(mockGrant).toHaveBeenCalledTimes(2);
});

test('should throw an invalid token error', async () => {
jest.mocked(OpenIDClientFactory.getClient).mockResolvedValue({
grant: jest.fn().mockResolvedValue({
vi.mocked(OpenIDClientFactory.getClient).mockResolvedValue({
grant: vi.fn().mockResolvedValue({
access_token: 'test_token',
}),
} as unknown as Client);
Expand All @@ -73,8 +73,8 @@ describe('test ConfidentialClient class', () => {
});

test('should throw an get access token error', async () => {
jest.mocked(OpenIDClientFactory.getClient).mockResolvedValue({
grant: jest.fn().mockRejectedValue('error'),
vi.mocked(OpenIDClientFactory.getClient).mockResolvedValue({
grant: vi.fn().mockRejectedValue('error'),
} as unknown as Client);

const confidentialClient = new ConfidentialClient('./__tests__/fixtures/validConfig.json');
Expand All @@ -84,9 +84,9 @@ describe('test ConfidentialClient class', () => {

test('should use the proxy agent if provided', async () => {
const proxyUrl = 'http://proxy.example.com:8080';
const getClientMock = jest.mocked(OpenIDClientFactory.getClient);
const getClientMock = vi.mocked(OpenIDClientFactory.getClient);
getClientMock.mockResolvedValue({
grant: jest.fn().mockResolvedValue({
grant: vi.fn().mockResolvedValue({
access_token: 'test_token',
expires_at: Math.floor(Date.now() / 1000) + 900,
}),
Expand Down
1 change: 0 additions & 1 deletion __tests__/configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Configuration} from '../src/configuration';
import {describe} from 'node:test';

const validTestConfig = {
name: 'name',
Expand Down
16 changes: 8 additions & 8 deletions __tests__/openIDClientFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {OpenIDClientFactory} from '../src/openIDClientFactory';
import {Client, custom, Issuer} from 'openid-client';
import {HttpsProxyAgent} from 'https-proxy-agent';

jest.mock('openid-client');
vi.mock('openid-client');

const config = {
name: 'name',
Expand All @@ -29,13 +29,13 @@ const config = {
describe('Test OpenIDClientFactory class', () => {
describe('Test getClient function', () => {
test('should not throw an error', async () => {
jest.mocked(Issuer.discover).mockResolvedValue({
vi.mocked(Issuer.discover).mockResolvedValue({
metadata: {
issuer: 'test',
token_endpoint: 'token_endpint',
},
Client: jest.fn().mockReturnValue({
grant: jest.fn().mockResolvedValue('testgrant'),
Client: vi.fn().mockImplementation(function () {
return {grant: vi.fn().mockResolvedValue('testgrant')};
}),
} as unknown as Issuer<Client>);

Expand All @@ -50,13 +50,13 @@ describe('Test OpenIDClientFactory class', () => {
const proxyUrl = 'http://proxy.example.com:8080';
const userAgent = `fds-sdk/javascript/utils/2.1.4 (${process.platform}; node ${process.version})`;

jest.mocked(Issuer.discover).mockResolvedValue({
vi.mocked(Issuer.discover).mockResolvedValue({
metadata: {
issuer: 'test',
token_endpoint: 'token_endpoint',
},
Client: jest.fn().mockReturnValue({
grant: jest.fn().mockResolvedValue('testgrant'),
Client: vi.fn().mockImplementation(function () {
return {grant: vi.fn().mockResolvedValue('testgrant')};
}),
} as unknown as Issuer<Client>);

Expand All @@ -69,7 +69,7 @@ describe('Test OpenIDClientFactory class', () => {
});

test('should throw an error while retrieving contents from well known uri', async () => {
jest.mocked(Issuer.discover).mockRejectedValue('test_error');
vi.mocked(Issuer.discover).mockRejectedValue('test_error');

await expect(OpenIDClientFactory.getClient(config)).rejects.toThrow(
'Error retrieving contents from the well_known_uri: testWellKnownUri'
Expand Down
6 changes: 3 additions & 3 deletions __tests__/token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ describe('test Token Class', () => {
});

test('checks the token whether it is expired or not', () => {
jest.useFakeTimers();
vi.useFakeTimers();
const expiresAt = Math.floor(new Date('2020-01-02').getTime() / 1000);
const token = new Token('testtoken', expiresAt);

jest.setSystemTime(new Date('2020-01-01'));
vi.setSystemTime(new Date('2020-01-01'));
expect(token.isExpired()).toBe(false);

jest.setSystemTime(new Date('2020-01-03'));
vi.setSystemTime(new Date('2020-01-03'));
expect(token.isExpired()).toBe(true);
});
});
6 changes: 3 additions & 3 deletions __tests__/unixTimestamp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import {unixTimestamp} from '../src/unixTimestamp';

describe('test unixTimestamp function', () => {
test('should generate proper dates', () => {
jest.useFakeTimers();
jest.setSystemTime(new Date('2020-01-01T12:00:00+00:00'));
vi.useFakeTimers();
vi.setSystemTime(new Date('2020-01-01T12:00:00+00:00'));
expect(unixTimestamp()).toBe(1577880000);

jest.setSystemTime(new Date('2021-01-01T12:00:00+00:00'));
vi.setSystemTime(new Date('2021-01-01T12:00:00+00:00'));
expect(unixTimestamp()).toBe(1609502400);
});
});
1 change: 1 addition & 0 deletions __tests__/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vitest/globals" />
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const compat = new FlatCompat({
});

export default defineConfig([
globalIgnores(['**/lib', '**/lib/', '**/node_modules/', '**/jest.config.js', '__tests__/fixtures', '**/.github']),
globalIgnores(['**/lib', '**/lib/', '**/node_modules/', '**/vitest.config.ts', '__tests__/fixtures', '**/.github']),
{
extends: compat.extends('plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'),

Expand Down
7 changes: 0 additions & 7 deletions jest.config.js

This file was deleted.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"prettier:fix": "prettier --write .",
"eslint:check": "eslint src",
"eslint:fix": "eslint --fix src",
"test": "jest --silent",
"test": "vitest run --coverage",
"prepublishOnly": "yarn build"
},
"files": [
Expand All @@ -42,19 +42,17 @@
"@eslint/eslintrc": "^3.3.5",
"@eslint/js": "^10.0.1",
"@types/debug": "^4.1.13",
"@types/jest": "^30.0.0",
"@types/node": "^22.20.0",
"@typescript-eslint/eslint-plugin": "^8.62.0",
"@typescript-eslint/parser": "^8.62.0",
"@vitest/coverage-v8": "^4.1.9",
"concurrently": "^10.0.3",
"eslint": "^10.5.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.6",
"jest": "^30.4.2",
"prettier": "^3.8.4",
"ts-jest": "^29.4.11",
"ts-node": "^10.9.2",
"typescript": "^5.9.3"
"typescript": "^5.9.3",
"vitest": "^4.1.9"
},
"packageManager": "yarn@4.17.0"
}
13 changes: 13 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {defineConfig} from 'vitest/config';

export default defineConfig({
test: {
globals: true,
environment: 'node',
coverage: {
provider: 'v8',
include: ['src/**/*.ts'],
},
exclude: ['__tests__/fixtures/**', 'node_modules/**', 'lib/**'],
},
});
Loading
Loading