-
Notifications
You must be signed in to change notification settings - Fork 569
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
feat: add std/expect #3814
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6de4de6
feat: add expect syntax by wrapping assert syntax
kt3k b872b6d
move to std/expect
kt3k e32a465
add docs, add placeholders, add supported APIs
kt3k e3e116e
add test cases wip
kt3k f1e4685
fix toBeFalsy
kt3k 8305b7a
add tests, wip
kt3k 46b27ac
wip
kt3k 0b43060
mock utils
kt3k 4a40f42
add toHaveBeenCalledWith
kt3k 771af12
add alisases
kt3k 1cd90fe
implement toHaveBeenLastCalledWith, toHaveBeenNthCalledWith
kt3k 2663f62
toHaveProperty
kt3k 86111ea
add toHaveReturned and toHaveReturnedTimes
kt3k 357ecb3
add toHaveReturnedWith, fix toBeCloseTo
kt3k b1d19a4
fix browser typing
kt3k 1af6b63
remove resolved todo
kt3k ef3fce6
move matchers to expect/_matchers.ts
kt3k File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// deno-lint-ignore-file | ||
|
||
export function inspectArgs(args: unknown[]): string { | ||
return args.map(inspectArg).join(", "); | ||
} | ||
|
||
export function inspectArg(arg: unknown): string { | ||
const { Deno } = globalThis as any; | ||
return typeof Deno !== "undefined" && Deno.inspect | ||
? Deno.inspect(arg) | ||
: String(arg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// deno-lint-ignore-file no-explicit-any | ||
|
||
export const MOCK_SYMBOL = Symbol.for("@MOCK"); | ||
|
||
export type MockCall = { | ||
args: any[]; | ||
returned?: any; | ||
thrown?: any; | ||
timestamp: number; | ||
returns: boolean; | ||
throws: boolean; | ||
}; | ||
|
||
export function getMockCalls(f: any): MockCall[] { | ||
const mockInfo = f[MOCK_SYMBOL]; | ||
if (!mockInfo) { | ||
throw new Error("Received function must be a mock or spy function"); | ||
} | ||
|
||
return [...mockInfo.calls]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { assertNotStrictEquals } from "../assert/assert_not_strict_equals.ts"; | ||
import { assertStrictEquals } from "../assert/assert_strict_equals.ts"; | ||
|
||
export function toBe(context: MatcherContext, expect: unknown): MatchResult { | ||
if (context.isNot) { | ||
assertNotStrictEquals(context.value, expect, context.customMessage); | ||
} else { | ||
assertStrictEquals(context.value, expect, context.customMessage); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { AssertionError } from "../assert/assertion_error.ts"; | ||
|
||
// TODO(kt3k): tolerance handling is wrong | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test that covers this tolerance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, this has been already resolved in 357ecb3 |
||
export function toBeCloseTo( | ||
context: MatcherContext, | ||
expected: number, | ||
numDigits = 2, | ||
): MatchResult { | ||
if (numDigits < 0) { | ||
throw new Error( | ||
"toBeCloseTo second argument must be a non-negative integer. Got " + | ||
numDigits, | ||
); | ||
} | ||
const tolerance = 0.5 * Math.pow(10, -numDigits); | ||
const value = Number(context.value); | ||
const pass = Math.abs(expected - value) < tolerance; | ||
|
||
if (context.isNot) { | ||
if (pass) { | ||
throw new AssertionError( | ||
`Expected the value not to be close to ${expected} (using ${numDigits} digits), but it is`, | ||
); | ||
} | ||
} else { | ||
if (!pass) { | ||
throw new AssertionError( | ||
`Expected the value (${value} to be close to ${expected} (using ${numDigits} digits), but it is not`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
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.1).toBeCloseTo(0.3, 15); | ||
|
||
expect(0.2 + 0.11).not.toBeCloseTo(0.3); | ||
expect(0.2 + 0.1).not.toBeCloseTo(0.3, 16); | ||
|
||
assertThrows(() => { | ||
expect(0.2 + 0.11).toBeCloseTo(0.3); | ||
}, AssertionError); | ||
|
||
assertThrows(() => { | ||
expect(0.2 + 0.1).not.toBeCloseTo(0.3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { assertNotStrictEquals } from "../assert/assert_not_strict_equals.ts"; | ||
import { assertStrictEquals } from "../assert/assert_strict_equals.ts"; | ||
|
||
export function toBeDefined(context: MatcherContext): MatchResult { | ||
if (context.isNot) { | ||
assertStrictEquals(context.value, undefined, context.customMessage); | ||
} else { | ||
assertNotStrictEquals(context.value, undefined, context.customMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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"; | ||
|
||
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(() => { | ||
expect(({} as any).foo).toBeDefined(); | ||
}, AssertionError); | ||
|
||
assertThrows(() => { | ||
expect(1).not.toBeDefined(); | ||
}, AssertionError); | ||
assertThrows(() => { | ||
expect("a").not.toBeDefined(); | ||
}, AssertionError); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { AssertionError } from "../assert/assertion_error.ts"; | ||
|
||
export function toBeFalsy( | ||
context: MatcherContext, | ||
): MatchResult { | ||
const isFalsy = !(context.value); | ||
if (context.isNot) { | ||
if (isFalsy) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to NOT be falsy`, | ||
); | ||
} | ||
} else { | ||
if (!isFalsy) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to be falsy`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
import { AssertionError, assertThrows } from "../assert/mod.ts"; | ||
|
||
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); | ||
|
||
assertThrows(() => { | ||
expect(false).not.toBeFalsy(); | ||
}, AssertionError); | ||
assertThrows(() => { | ||
expect(0).not.toBeFalsy(); | ||
}, AssertionError); | ||
assertThrows(() => { | ||
expect("").not.toBeFalsy(); | ||
}, AssertionError); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { AssertionError } from "../assert/assertion_error.ts"; | ||
|
||
export function toBeGreaterThan( | ||
context: MatcherContext, | ||
expected: number, | ||
): MatchResult { | ||
const isGreater = Number(context.value) > Number(expected); | ||
if (context.isNot) { | ||
if (isGreater) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to NOT be greater than ${expected}`, | ||
); | ||
} | ||
} else { | ||
if (!isGreater) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to be greater than ${expected}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { AssertionError } from "../assert/assertion_error.ts"; | ||
|
||
export function toBeGreaterThanOrEqual( | ||
context: MatcherContext, | ||
expected: number, | ||
): MatchResult { | ||
const isGreaterOrEqual = Number(context.value) >= Number(expected); | ||
if (context.isNot) { | ||
if (isGreaterOrEqual) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to NOT be greater than or equal ${expected}`, | ||
); | ||
} | ||
} else { | ||
if (!isGreaterOrEqual) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to be greater than or equal ${expected}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
import { AssertionError, assertThrows } from "../assert/mod.ts"; | ||
|
||
Deno.test("expect().toBeGreaterThanOrEqual()", () => { | ||
expect(10).toBeGreaterThanOrEqual(10); | ||
expect(11).toBeGreaterThanOrEqual(10); | ||
|
||
expect(9).not.toBeGreaterThanOrEqual(10); | ||
|
||
assertThrows(() => { | ||
expect(9).toBeGreaterThanOrEqual(10); | ||
}, AssertionError); | ||
|
||
assertThrows(() => { | ||
expect(11).not.toBeGreaterThanOrEqual(10); | ||
}, AssertionError); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
import { AssertionError, assertThrows } from "../assert/mod.ts"; | ||
|
||
Deno.test("expect().toBeGreaterThan()", () => { | ||
expect(11).toBeGreaterThan(10); | ||
|
||
expect(10).not.toBeGreaterThan(10); | ||
expect(9).not.toBeGreaterThan(10); | ||
|
||
assertThrows(() => { | ||
expect(10).toBeGreaterThan(10); | ||
}, AssertionError); | ||
assertThrows(() => { | ||
expect(9).toBeGreaterThan(10); | ||
}); | ||
|
||
assertThrows(() => { | ||
expect(11).not.toBeGreaterThan(10); | ||
}, AssertionError); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { AnyConstructor, MatcherContext, MatchResult } from "./_types.ts"; | ||
import { assertInstanceOf } from "../assert/assert_instance_of.ts"; | ||
import { assertNotInstanceOf } from "../assert/assert_not_instance_of.ts"; | ||
|
||
export function toBeInstanceOf<T extends AnyConstructor>( | ||
context: MatcherContext, | ||
expected: T, | ||
): MatchResult { | ||
if (context.isNot) { | ||
assertNotInstanceOf(context.value, expected); | ||
} else { | ||
assertInstanceOf(context.value, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
import { AssertionError, assertThrows } from "../assert/mod.ts"; | ||
|
||
Deno.test("expect().toBeInstanceOf", () => { | ||
expect(new Error()).toBeInstanceOf(Error); | ||
expect(new Error()).toBeInstanceOf(Object); | ||
|
||
expect(new Error()).not.toBeInstanceOf(String); | ||
|
||
assertThrows(() => { | ||
expect(new Error()).toBeInstanceOf(String); | ||
}, AssertionError); | ||
|
||
assertThrows(() => { | ||
expect(new Error()).not.toBeInstanceOf(Error); | ||
}, AssertionError); | ||
assertThrows(() => { | ||
expect(new Error()).not.toBeInstanceOf(Object); | ||
}, AssertionError); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { AssertionError } from "../assert/assertion_error.ts"; | ||
|
||
export function toBeLessThan( | ||
context: MatcherContext, | ||
expected: number, | ||
): MatchResult { | ||
const isLower = Number(context.value) < Number(expected); | ||
if (context.isNot) { | ||
if (isLower) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to NOT be lower than ${expected}`, | ||
); | ||
} | ||
} else { | ||
if (!isLower) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to be lower than ${expected}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { MatcherContext, MatchResult } from "./_types.ts"; | ||
import { AssertionError } from "../assert/assertion_error.ts"; | ||
|
||
export function toBeLessThanOrEqual( | ||
context: MatcherContext, | ||
expected: number, | ||
): MatchResult { | ||
const isLower = Number(context.value) <= Number(expected); | ||
if (context.isNot) { | ||
if (isLower) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to NOT be lower than or equal ${expected}`, | ||
); | ||
} | ||
} else { | ||
if (!isLower) { | ||
throw new AssertionError( | ||
`Expected ${context.value} to be lower than or equal ${expected}`, | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for splitting into so many small modules?