chore: Update (ts-)jest and use modern fake timers where possible

* chore(deps): update dependency jest to v27

* chore: Update (ts-)jest and use modern fake timers where possible

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com>
This commit is contained in:
renovate[bot]
2021-05-31 15:29:14 +02:00
committed by GitHub
parent 9172f2ae59
commit afc662ca9a
8 changed files with 1549 additions and 2425 deletions

3917
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -155,7 +155,7 @@
"eslint-plugin-tsdoc": "^0.2.7",
"eslint-plugin-unused-imports": "^1.0.0",
"husky": "^4.2.5",
"jest": "^26.6.3",
"jest": "^27.0.3",
"jest-rdf": "^1.5.0",
"manual-git-changelog": "^1.0.1",
"node-mocks-http": "^1.8.1",
@@ -164,7 +164,7 @@
"set-cookie-parser": "^2.4.8",
"stream-to-string": "^1.1.0",
"supertest": "^6.0.0",
"ts-jest": "^26.3.0",
"ts-jest": "^27.0.2",
"typedoc": "^0.20.36",
"typescript": "^4.0.2"
}

View File

@@ -17,7 +17,8 @@ import { WrappedExpiringReadWriteLocker } from '../../src/util/locking/WrappedEx
import { guardedStreamFrom } from '../../src/util/StreamUtil';
import { BASE } from './Config';
jest.useFakeTimers();
// The modern fake timers implementation blocks certain parts with the current setup
jest.useFakeTimers('legacy');
describe('A LockingResourceStore', (): void => {
let path: string;

View File

@@ -3,6 +3,9 @@ import { InteractionCompleter } from '../../../../../src/identity/interaction/ut
import type { HttpRequest } from '../../../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../../../src/server/HttpResponse';
// Use fixed dates
jest.useFakeTimers();
describe('An InteractionCompleter', (): void => {
const request: HttpRequest = {} as any;
const response: HttpResponse = {} as any;
@@ -11,8 +14,6 @@ describe('An InteractionCompleter', (): void => {
const completer = new InteractionCompleter();
beforeEach(async(): Promise<void> => {
const now = Date.now();
Date.now = jest.fn().mockReturnValue(now);
provider = {
interactionFinished: jest.fn(),
} as any;

View File

@@ -3,6 +3,9 @@ import type { ExpiringAdapter } from '../../../../src/identity/storage/ExpiringA
import { ExpiringAdapterFactory } from '../../../../src/identity/storage/ExpiringAdapterFactory';
import type { ExpiringStorage } from '../../../../src/storage/keyvalue/ExpiringStorage';
// Use fixed dates
jest.useFakeTimers();
describe('An ExpiringAdapterFactory', (): void => {
const storageName = '/storage';
const name = 'nnaammee';
@@ -12,14 +15,10 @@ describe('An ExpiringAdapterFactory', (): void => {
let storage: ExpiringStorage<string, unknown>;
let adapter: ExpiringAdapter;
let factory: ExpiringAdapterFactory;
// Make sure this stays consistent in tests
const now = Date.now();
const expiresIn = 333;
const expireDate = new Date(now + (expiresIn * 1000));
const expireDate = new Date(Date.now() + (expiresIn * 1000));
beforeEach(async(): Promise<void> => {
Date.now = jest.fn().mockReturnValue(now);
payload = { data: 'data!' };
const map = new Map<string, any>();

View File

@@ -112,6 +112,10 @@ describe('A WrappedExpiringStorage', (): void => {
});
it('removes expired entries after a given time.', async(): Promise<void> => {
// Disable interval function and simply check it was called with the correct parameters
// Otherwise it gets quite difficult to verify the async interval function gets executed
const mockInterval = jest.spyOn(global, 'setInterval');
mockInterval.mockImplementation(jest.fn());
// Timeout of 1 minute
storage = new WrappedExpiringStorage(source, 1);
const data = [
@@ -123,16 +127,22 @@ describe('A WrappedExpiringStorage', (): void => {
yield* data;
});
jest.advanceTimersByTime(60 * 1000);
// Make sure interval is created correctly
expect(mockInterval.mock.calls).toHaveLength(1);
expect(mockInterval.mock.calls[0]).toHaveLength(2);
expect(mockInterval.mock.calls[0][1]).toBe(60 * 1000);
// Allow timer promise callback to resolve
await new Promise(setImmediate);
// Await the function that should have been executed by the interval
await (mockInterval.mock.calls[0][0] as () => Promise<void>)();
expect(source.delete).toHaveBeenCalledTimes(1);
expect(source.delete).toHaveBeenLastCalledWith('key2');
mockInterval.mockRestore();
});
it('can stop the timer.', async(): Promise<void> => {
const mockInterval = jest.spyOn(global, 'setInterval');
const mockClear = jest.spyOn(global, 'clearInterval');
// Timeout of 1 minute
storage = new WrappedExpiringStorage(source, 1);
const data = [
@@ -145,11 +155,13 @@ describe('A WrappedExpiringStorage', (): void => {
});
expect(storage.finalize()).toBeUndefined();
jest.advanceTimersByTime(60 * 1000);
// Allow timer promise callback to resolve
await new Promise(setImmediate);
// Make sure clearInterval was called with the interval timer
expect(mockClear.mock.calls).toHaveLength(1);
expect(mockClear.mock.calls[0]).toHaveLength(1);
expect(mockClear.mock.calls[0][0]).toBe(mockInterval.mock.results[0].value);
expect(source.delete).toHaveBeenCalledTimes(0);
mockInterval.mockRestore();
mockClear.mockRestore();
});
});

View File

@@ -75,7 +75,7 @@ describe('GuardedStream', (): void => {
expect(errorListeners[2]).toHaveBeenCalledTimes(0);
expect(endListener).toHaveBeenCalledTimes(0);
await new Promise((resolve): any => setImmediate(resolve));
jest.runAllTimers();
expect(errorListeners[0]).toHaveBeenCalledTimes(2);
expect(errorListeners[0]).toHaveBeenNthCalledWith(1, errors[0]);
@@ -137,7 +137,7 @@ describe('GuardedStream', (): void => {
const errorCb3 = jest.fn();
stream.on('error', errorCb3);
await new Promise((resolve): any => setImmediate(resolve));
jest.runAllTimers();
expect(errorCb).toHaveBeenCalledTimes(0);
expect(errorCb2).toHaveBeenCalledTimes(0);
@@ -167,7 +167,7 @@ describe('GuardedStream', (): void => {
const errorCb = jest.fn();
stream.on('error', errorCb);
await new Promise((resolve): any => setImmediate(resolve));
jest.runAllTimers();
expect(errorCb).toHaveBeenCalledTimes(3);
expect(errorCb).toHaveBeenNthCalledWith(1, errors[0]);

View File

@@ -46,6 +46,10 @@ describe('A WrappedExpiringReadWriteLocker', (): void => {
expect(syncCb).toHaveBeenCalledTimes(1);
prom = locker.withReadLock(identifier, asyncCb);
// Execute promise (without triggering timeout)
jest.advanceTimersByTime(100);
await expect(prom).resolves.toBe('async');
expect(asyncCb).toHaveBeenCalledTimes(1);
});