Use assert.isRejected instead of custom assertRejects

This commit is contained in:
Evan Hahn 2021-12-13 11:38:40 -06:00 committed by GitHub
parent 683823a114
commit 43f843f9fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 26 deletions

View file

@ -1,17 +0,0 @@
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
export async function assertRejects(fn: () => Promise<unknown>): Promise<void> {
let err: unknown;
try {
await fn();
} catch (e) {
err = e;
}
assert(
err instanceof Error,
'Expected promise to reject with an Error, but it resolved'
);
}

View file

@ -13,7 +13,6 @@ import PQueue from 'p-queue';
import { JobError } from '../../jobs/JobError'; import { JobError } from '../../jobs/JobError';
import { TestJobQueueStore } from './TestJobQueueStore'; import { TestJobQueueStore } from './TestJobQueueStore';
import { missingCaseError } from '../../util/missingCaseError'; import { missingCaseError } from '../../util/missingCaseError';
import { assertRejects } from '../helpers';
import type { LoggerType } from '../../types/Logging'; import type { LoggerType } from '../../types/Logging';
import { JobQueue } from '../../jobs/JobQueue'; import { JobQueue } from '../../jobs/JobQueue';
@ -803,8 +802,8 @@ describe('JobQueue', () => {
noopQueue.streamJobs(); noopQueue.streamJobs();
await assertRejects(() => noopQueue.streamJobs()); await assert.isRejected(noopQueue.streamJobs());
await assertRejects(() => noopQueue.streamJobs()); await assert.isRejected(noopQueue.streamJobs());
sinon.assert.calledOnce(fakeStore.stream as sinon.SinonStub); sinon.assert.calledOnce(fakeStore.stream as sinon.SinonStub);
}); });
@ -834,7 +833,7 @@ describe('JobQueue', () => {
maxAttempts: 99, maxAttempts: 99,
}); });
await assertRejects(() => noopQueue.add(undefined)); await assert.isRejected(noopQueue.add(undefined));
sinon.assert.notCalled(fakeStore.stream as sinon.SinonStub); sinon.assert.notCalled(fakeStore.stream as sinon.SinonStub);
}); });

View file

@ -1,4 +1,4 @@
// Copyright 2020 Signal Messenger, LLC // Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai'; import { assert } from 'chai';
@ -7,7 +7,6 @@ import * as os from 'os';
import * as fs from 'fs'; import * as fs from 'fs';
import * as fse from 'fs-extra'; import * as fse from 'fs-extra';
import * as Sinon from 'sinon'; import * as Sinon from 'sinon';
import { assertRejects } from '../helpers';
import { writeWindowsZoneIdentifier } from '../../util/windowsZoneIdentifier'; import { writeWindowsZoneIdentifier } from '../../util/windowsZoneIdentifier';
@ -47,13 +46,13 @@ describe('writeWindowsZoneIdentifier', () => {
await fse.outputFile(file, 'hello'); await fse.outputFile(file, 'hello');
await fs.promises.writeFile(`${file}:Zone.Identifier`, '# already here'); await fs.promises.writeFile(`${file}:Zone.Identifier`, '# already here');
await assertRejects(() => writeWindowsZoneIdentifier(file)); await assert.isRejected(writeWindowsZoneIdentifier(file));
}); });
it('fails if the original file does not exist', async function thisNeeded() { it('fails if the original file does not exist', async function thisNeeded() {
const file = path.join(this.tmpdir, 'file-never-created.txt'); const file = path.join(this.tmpdir, 'file-never-created.txt');
await assertRejects(() => writeWindowsZoneIdentifier(file)); await assert.isRejected(writeWindowsZoneIdentifier(file));
}); });
it('fails if not on Windows', async function thisNeeded() { it('fails if not on Windows', async function thisNeeded() {
@ -62,6 +61,6 @@ describe('writeWindowsZoneIdentifier', () => {
const file = path.join(this.tmpdir, 'file.txt'); const file = path.join(this.tmpdir, 'file.txt');
await fse.outputFile(file, 'hello'); await fse.outputFile(file, 'hello');
await assertRejects(() => writeWindowsZoneIdentifier(file)); await assert.isRejected(writeWindowsZoneIdentifier(file));
}); });
}); });