Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions types/chai/chai-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,8 @@ class CrashyObject {
}
}

declare function foobar<T>(): T;

suite("assert", () => {
test("assert", () => {
const foo = "bar" as string;
Expand Down Expand Up @@ -1537,6 +1539,10 @@ suite("assert", () => {
assert.instanceOf(new Foo(), Foo);
assert.instanceOf(5, Foo);
assert.instanceOf(new CrashyObject(), CrashyObject);

const value = foobar<Foo | null>();
assert.instanceOf(value, Foo);
const fooValue: Foo = value;
});

test("notInstanceOf", () => {
Expand Down Expand Up @@ -1656,21 +1662,37 @@ suite("assert", () => {
test("isNull", () => {
assert.isNull(null);
assert.isNull(undefined);

const value = foobar<string | null>();
assert.isNull(value);
const nullValue: null = value;
});

test("isNotNull", () => {
assert.isNotNull(undefined);
assert.isNotNull(null);

const value = foobar<number | null>();
assert.isNotNull(value);
const numberValue: number = value;
});

test("isUndefined", () => {
assert.isUndefined(undefined);
assert.isUndefined(null);

const value = foobar<undefined | number>();
assert.isUndefined(value);
const undefinedValue: undefined = value;
});

test("isDefined", () => {
assert.isDefined(null);
assert.isDefined(undefined);

const value = foobar<undefined | number>();
assert.isDefined(value);
const definedValue: number = value;
});

test("isNaN", () => {
Expand Down Expand Up @@ -1737,12 +1759,20 @@ suite("assert", () => {
assert.isBoolean(true);
assert.isBoolean(false);
assert.isBoolean("1");

const value = foobar<boolean | string>();
assert.isBoolean(value);
const booleanValue: boolean = value;
});

test("isNotBoolean", () => {
assert.isNotBoolean("true");
assert.isNotBoolean(true);
assert.isNotBoolean(false);

const value = foobar<boolean | string>();
assert.isNotBoolean(value);
const stringValue: string = value;
});

test("include", () => {
Expand Down
22 changes: 12 additions & 10 deletions types/chai/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ declare namespace Chai {
exists: boolean;
}

interface Constructor<T> {
new(...args: any[]): T;
}

interface ErrorConstructor {
new(...args: any[]): Error;
}
Expand Down Expand Up @@ -595,11 +599,10 @@ declare namespace Chai {
/**
* Asserts that value is null.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
isNull<T>(value: T, message?: string): void;
isNull(value: unknown, message?: string): asserts value is null;

/**
* Asserts that value is not null.
Expand All @@ -608,7 +611,7 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
isNotNull<T>(value: T, message?: string): void;
isNotNull<T>(value: T, message?: string): asserts value is Exclude<T, null>;

/**
* Asserts that value is NaN.
Expand Down Expand Up @@ -653,7 +656,7 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
isUndefined<T>(value: T, message?: string): void;
isUndefined<T>(value: T | undefined, message?: string): asserts value is undefined;

/**
* Asserts that value is not undefined.
Expand All @@ -662,7 +665,7 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
isDefined<T>(value: T, message?: string): void;
isDefined<T>(value: T, message?: string): asserts value is Exclude<T, undefined>;

/**
* Asserts that value is a function.
Expand Down Expand Up @@ -770,11 +773,10 @@ declare namespace Chai {
/**
* Asserts that value is a boolean.
*
* T Type of value.
* @param value Actual value.
* @param message Message to display on error.
*/
isBoolean<T>(value: T, message?: string): void;
isBoolean(value: unknown, message?: string): asserts value is boolean;

/**
* Asserts that value is not a boolean.
Expand All @@ -783,7 +785,7 @@ declare namespace Chai {
* @param value Actual value.
* @param message Message to display on error.
*/
isNotBoolean<T>(value: T, message?: string): void;
isNotBoolean<T>(value: T, message?: string): asserts value is Exclude<T, boolean>;

/**
* Asserts that value's type is name, as determined by Object.prototype.toString.
Expand All @@ -808,12 +810,12 @@ declare namespace Chai {
/**
* Asserts that value is an instance of constructor.
*
* T Type of value.
* T Expected type of value.
* @param value Actual value.
* @param constructor Potential expected contructor of value.
* @param message Message to display on error.
*/
instanceOf<T>(value: T, constructor: Function, message?: string): void;
instanceOf<T>(value: unknown, constructor: Constructor<T>, message?: string): asserts value is T;

/**
* Asserts that value is not an instance of constructor.
Expand Down