Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add std/expect #3814

Merged
merged 17 commits into from
Nov 21, 2023
Prev Previous commit
Next Next commit
fix toBeFalsy
  • Loading branch information
kt3k committed Nov 16, 2023
commit f1e4685af5c7ad412a8a776f1ae41b4354b99b9e
6 changes: 6 additions & 0 deletions expect/_to_be_close_to_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ import { AssertionError, assertThrows } from "../assert/mod.ts";
Deno.test("expect().toBeCloseTo()", () => {
expect(0.2 + 0.1).toBeCloseTo(0.3);
expect(0.2 + 0.1).toBeCloseTo(0.3, 5);

expect(0.2 + 0.11).not.toBeCloseTo(0.3);

assertThrows(() => {
expect(0.2 + 0.11).toBeCloseTo(0.3);
}, AssertionError);
});
5 changes: 3 additions & 2 deletions expect/_to_be_defined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { assertStrictEquals } from "../assert/assert_strict_equals.ts";

export function toBeDefined(context: MatcherContext): MatchResult {
if (context.isNot) {
return assertStrictEquals(context.value, undefined, context.customMessage);
assertStrictEquals(context.value, undefined, context.customMessage);
} else {
assertNotStrictEquals(context.value, undefined, context.customMessage);
}
return assertNotStrictEquals(context.value, undefined, context.customMessage);
}
5 changes: 4 additions & 1 deletion expect/_to_be_defined_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-explicit-any

import { expect } from "./expect.ts";
import { AssertionError, assertThrows } from "../assert/mod.ts";
Expand All @@ -7,11 +8,13 @@ Deno.test("expect().toBeDefined()", () => {
expect(1).toBeDefined();
expect("a").toBeDefined();

expect(undefined).not.toBeDefined();
expect(({} as any).foo).not.toBeDefined();

assertThrows(() => {
expect(undefined).toBeDefined();
}, AssertionError);
assertThrows(() => {
// deno-lint-ignore no-explicit-any
expect(({} as any).foo).toBeDefined();
}, AssertionError);
});
12 changes: 7 additions & 5 deletions expect/_to_be_falsy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
const isFalsy = !(context.value);
if (context.isNot) {
if (isFalsy) {
throw new AssertionError(
`Expected ${context.value} to NOT be falsy`,

Check warning on line 13 in expect/_to_be_falsy.ts

View check run for this annotation

Codecov / codecov/patch

expect/_to_be_falsy.ts#L12-L13

Added lines #L12 - L13 were not covered by tests
);
}

Check warning on line 15 in expect/_to_be_falsy.ts

View check run for this annotation

Codecov / codecov/patch

expect/_to_be_falsy.ts#L15

Added line #L15 was not covered by tests
}
if (!isFalsy) {
throw new AssertionError(
`Expected ${context.value} to be falsy`,
);
return;
} else {
if (!isFalsy) {
throw new AssertionError(
`Expected ${context.value} to be falsy`,
);
}
}
}
20 changes: 19 additions & 1 deletion expect/_to_be_falsy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,22 @@
import { expect } from "./expect.ts";
import { AssertionError, assertThrows } from "../assert/mod.ts";

Deno.test("expect().toBeFalsy()", () => {});
Deno.test("expect().toBeFalsy()", () => {
expect(false).toBeFalsy();
expect(0).toBeFalsy();
expect("").toBeFalsy();

expect(true).not.toBeFalsy();
expect(1).not.toBeFalsy();
expect("hello").not.toBeFalsy();

assertThrows(() => {
expect(true).toBeFalsy();
}, AssertionError);
assertThrows(() => {
expect(1).toBeFalsy();
}, AssertionError);
assertThrows(() => {
expect("hello").toBeFalsy();
}, AssertionError);
});
2 changes: 2 additions & 0 deletions expect/_to_throw_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Deno.test("expect().toThrow()", () => {
throw new Error("hello world");
}).toThrow();

expect(() => {}).not.toThrow();

assertThrows(() => {
expect(() => {}).toThrow();
}, AssertionError);
Expand Down
Loading