blob: 52a08a81eb7c44025cb3e3a51bdd2535352c45ab [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Asserts that the given promise will rejected.
*
* @param promise The Promise that is expected to reject.
* @param expectedReason Expected rejection reason.
* If RegExp or string, then rejection reason is expected to have message
* property (like Error) that matches the expected reason.
*/
export async function assertRejects(promise: any, expectedReason: any = null) {
try {
await promise;
assert.fail('exception', 'no exception');
} catch (actualReason: unknown) {
if (expectedReason) {
if (expectedReason instanceof RegExp) {
assert.match((actualReason as Error).message, expectedReason);
} else if (typeof expectedReason === 'string') {
assert.equal((actualReason as Error).message, expectedReason);
} else {
assert.deepEqual(actualReason, expectedReason);
}
}
}
}
export function deepFreeze(obj: any) {
for (const p of Object.keys(obj)) {
const v = obj[p];
if (typeof v === 'object' && v !== null) {
deepFreeze(v);
}
}
return Object.freeze(obj);
}