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

View File

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