Path: blob/main/src/vs/base/test/common/assert.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { ok, assert as commonAssert } from '../../common/assert.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';8import { CancellationError, ReadonlyError } from '../../common/errors.js';910suite('Assert', () => {11test('ok', () => {12assert.throws(function () {13ok(false);14});1516assert.throws(function () {17ok(null);18});1920assert.throws(function () {21ok();22});2324assert.throws(function () {25ok(null, 'Foo Bar');26}, function (e: Error) {27return e.message.indexOf('Foo Bar') >= 0;28});2930ok(true);31ok('foo');32ok({});33ok(5);34});3536suite('throws a provided error object', () => {37test('generic error', () => {38const originalError = new Error('Oh no!');3940try {41commonAssert(42false,43originalError,44);45} catch (thrownError) {46assert.strictEqual(47thrownError,48originalError,49'Must throw the provided error instance.',50);5152assert.strictEqual(53thrownError.message,54'Oh no!',55'Must throw the provided error instance.',56);57}58});5960test('cancellation error', () => {61const originalError = new CancellationError();6263try {64commonAssert(65false,66originalError,67);68} catch (thrownError) {69assert.strictEqual(70thrownError,71originalError,72'Must throw the provided error instance.',73);74}75});7677test('readonly error', () => {78const originalError = new ReadonlyError('World');7980try {81commonAssert(82false,83originalError,84);85} catch (thrownError) {86assert.strictEqual(87thrownError,88originalError,89'Must throw the provided error instance.',90);9192assert.strictEqual(93thrownError.message,94'World is read-only and cannot be changed',95'Must throw the provided error instance.',96);97}98});99});100101ensureNoDisposablesAreLeakedInTestSuite();102});103104105