updates usages of jest timers to prepare for jest 27 (#4539)

This commit is contained in:
Dimitri Mitropoulos 2022-03-07 11:19:08 -08:00 committed by GitHub
parent 1f05683fad
commit 85d8290450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 10 deletions

View File

@ -4,12 +4,12 @@ import path from 'path';
import { globalBeforeEach } from '../../__jest__/before-each'; import { globalBeforeEach } from '../../__jest__/before-each';
import LocalStorage from '../../main/local-storage'; import LocalStorage from '../../main/local-storage';
jest.spyOn(global, 'setTimeout');
describe('LocalStorage()', () => { describe('LocalStorage()', () => {
beforeEach(async () => { beforeEach(async () => {
await globalBeforeEach(); await globalBeforeEach();
jest.useFakeTimers(); jest.useFakeTimers();
// There has to be a better way to reset this...
setTimeout.mock.calls = [];
}); });
afterEach(() => { afterEach(() => {
@ -18,8 +18,10 @@ describe('LocalStorage()', () => {
it('create directory', () => { it('create directory', () => {
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`; const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
const ls = new LocalStorage(basePath); const ls = new LocalStorage(basePath);
expect(ls).toBeInstanceOf(LocalStorage); expect(ls).toBeInstanceOf(LocalStorage);
const dir = fs.readdirSync(basePath); const dir = fs.readdirSync(basePath);
expect(dir.length).toEqual(0); expect(dir.length).toEqual(0);
}); });
@ -27,10 +29,12 @@ describe('LocalStorage()', () => {
it('does basic operations', () => { it('does basic operations', () => {
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`; const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
const localStorage = new LocalStorage(basePath); const localStorage = new LocalStorage(basePath);
// Test get and set // Test get and set
localStorage.setItem('foo', 'bar 1'); localStorage.setItem('foo', 'bar 1');
localStorage.setItem('foo', 'bar'); localStorage.setItem('foo', 'bar');
expect(localStorage.getItem('foo', 'BAD')).toBe('bar'); expect(localStorage.getItem('foo', 'BAD')).toBe('bar');
// Test Object storage // Test Object storage
localStorage.setItem('obj', { localStorage.setItem('obj', {
foo: 'bar', foo: 'bar',
@ -40,6 +44,7 @@ describe('LocalStorage()', () => {
foo: 'bar', foo: 'bar',
arr: [1, 2, 3], arr: [1, 2, 3],
}); });
// Test default values // Test default values
expect(localStorage.getItem('dne', 'default')).toEqual('default'); expect(localStorage.getItem('dne', 'default')).toEqual('default');
expect(localStorage.getItem('dne')).toEqual('default'); expect(localStorage.getItem('dne')).toEqual('default');
@ -49,9 +54,11 @@ describe('LocalStorage()', () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`; const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
const localStorage = new LocalStorage(basePath); const localStorage = new LocalStorage(basePath);
// Assert default is returned on bad JSON // Assert default is returned on bad JSON
fs.writeFileSync(path.join(basePath, 'key'), '{bad JSON'); fs.writeFileSync(path.join(basePath, 'key'), '{bad JSON');
expect(localStorage.getItem('key', 'default')).toBe('default'); expect(localStorage.getItem('key', 'default')).toBe('default');
// Assert that writing our file actually works // Assert that writing our file actually works
fs.writeFileSync(path.join(basePath, 'key'), '{"good": "JSON"}'); fs.writeFileSync(path.join(basePath, 'key'), '{"good": "JSON"}');
expect(localStorage.getItem('key', 'default')).toEqual({ expect(localStorage.getItem('key', 'default')).toEqual({
@ -66,7 +73,9 @@ describe('LocalStorage()', () => {
const localStorage = new LocalStorage(basePath); const localStorage = new LocalStorage(basePath);
fs.rmdirSync(basePath); fs.rmdirSync(basePath);
localStorage.setItem('key', 'value'); localStorage.setItem('key', 'value');
jest.runAllTimers(); jest.runAllTimers();
// Since the above operation failed to write, we should now get back // Since the above operation failed to write, we should now get back
// the default value // the default value
expect(localStorage.getItem('key', 'different')).toBe('different'); expect(localStorage.getItem('key', 'different')).toBe('different');
@ -77,13 +86,17 @@ describe('LocalStorage()', () => {
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`; const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
const localStorage = new LocalStorage(basePath); const localStorage = new LocalStorage(basePath);
localStorage.setItem('foo', 'bar'); localStorage.setItem('foo', 'bar');
// Assert timeouts are called // Assert timeouts are called
expect(setTimeout.mock.calls.length).toBe(1); expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout.mock.calls[0][1]).toBe(100); expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 100);
// Force debouncer to flush // Force debouncer to flush
jest.runAllTimers(); jest.runAllTimers();
// Assert there is one item stored // Assert there is one item stored
expect(fs.readdirSync(basePath).length).toEqual(1); expect(fs.readdirSync(basePath).length).toEqual(1);
// Assert the contents are correct // Assert the contents are correct
const contents = fs.readFileSync(path.join(basePath, 'foo'), 'utf8'); const contents = fs.readFileSync(path.join(basePath, 'foo'), 'utf8');
expect(contents).toEqual('"bar"'); expect(contents).toEqual('"bar"');
@ -95,14 +108,17 @@ describe('LocalStorage()', () => {
localStorage.setItem('foo', 'bar1'); localStorage.setItem('foo', 'bar1');
localStorage.setItem('another', 10); localStorage.setItem('another', 10);
localStorage.setItem('foo', 'bar3'); localStorage.setItem('foo', 'bar3');
// Assert timeouts are called // Assert timeouts are called
expect(setTimeout.mock.calls.length).toBe(3); expect(setTimeout).toHaveBeenCalledTimes(3);
expect(setTimeout.mock.calls[0][1]).toBe(100); expect(setTimeout).nthCalledWith(1, expect.anything(), 100);
expect(setTimeout.mock.calls[1][1]).toBe(100); expect(setTimeout).nthCalledWith(2, expect.anything(), 100);
expect(setTimeout.mock.calls[2][1]).toBe(100); expect(setTimeout).nthCalledWith(3, expect.anything(), 100);
expect(fs.readdirSync(basePath).length).toEqual(0); expect(fs.readdirSync(basePath).length).toEqual(0);
// Force flush // Force flush
jest.runAllTimers(); jest.runAllTimers();
// Make sure only one item exists // Make sure only one item exists
expect(fs.readdirSync(basePath).length).toEqual(2); expect(fs.readdirSync(basePath).length).toEqual(2);
expect(fs.readFileSync(path.join(basePath, 'foo'), 'utf8')).toEqual('"bar3"'); expect(fs.readFileSync(path.join(basePath, 'foo'), 'utf8')).toEqual('"bar3"');

View File

@ -16,6 +16,8 @@ import {
xmlDecode, xmlDecode,
} from '../misc'; } from '../misc';
jest.spyOn(global, 'setTimeout');
describe('hasAuthHeader()', () => { describe('hasAuthHeader()', () => {
beforeEach(globalBeforeEach); beforeEach(globalBeforeEach);
@ -150,7 +152,7 @@ describe('keyedDebounce()', () => {
fn('foo', 'bar2'); fn('foo', 'bar2');
fn('foo', 'bar3'); fn('foo', 'bar3');
fn('multi', 'foo', 'bar', 'baz'); fn('multi', 'foo', 'bar', 'baz');
expect(setTimeout.mock.calls.length).toBe(5); expect(setTimeout).toHaveBeenCalledTimes(5);
expect(resultsList).toEqual([]); expect(resultsList).toEqual([]);
jest.runAllTimers(); jest.runAllTimers();
expect(resultsList).toEqual([ expect(resultsList).toEqual([
@ -179,7 +181,7 @@ describe('debounce()', () => {
fn('multi', 'foo', 'bar', 'baz'); fn('multi', 'foo', 'bar', 'baz');
fn('baz', 'bar'); fn('baz', 'bar');
fn('foo', 'bar3'); fn('foo', 'bar3');
expect(setTimeout.mock.calls.length).toBe(5); expect(setTimeout).toHaveBeenCalledTimes(5);
expect(resultList).toEqual([]); expect(resultList).toEqual([]);
jest.runAllTimers(); jest.runAllTimers();
expect(resultList).toEqual([['foo', 'bar3']]); expect(resultList).toEqual([['foo', 'bar3']]);