-
Notifications
You must be signed in to change notification settings - Fork 633
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(expect): support expect.extend()
api
#4412
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
90db189
wip: expect.extend support
eryue0220 1fa43ce
wip: expect.extend
eryue0220 15eb546
chore: remove untest code
eryue0220 6d1bb84
chore: remove untest code
eryue0220 8a2ae06
Merge branch 'main' into feat/expect-extend
eryue0220 e920bfa
fix: conflict
eryue0220 45d74a7
fix: ci
eryue0220 8e30d4b
Update expect/_extend_test.ts
kt3k 1b73417
use `function`
kt3k f7a652d
clean up test case
kt3k f983df3
add random test cases
kt3k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { Matchers } from "./_types.ts"; | ||
|
||
let extendMatchers = {}; | ||
|
||
export function getExtendMatchers() { | ||
return extendMatchers; | ||
} | ||
|
||
export function setExtendMatchers(newExtendMatchers: Matchers) { | ||
extendMatchers = { | ||
...extendMatchers, | ||
...newExtendMatchers, | ||
}; | ||
} |
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,119 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { expect } from "./expect.ts"; | ||
import { MatcherContext, Tester } from "./_types.ts"; | ||
|
||
type DbConnection = number; | ||
|
||
declare module "./_types.ts" { | ||
interface Expected { | ||
toEqualBook: (expected: unknown) => ExtendMatchResult; | ||
} | ||
} | ||
|
||
const CONNECTION_PROP = "__connection"; | ||
let DbConnectionId = 0; | ||
|
||
class Author { | ||
public name: string; | ||
public [CONNECTION_PROP]: DbConnection; | ||
|
||
constructor(name: string) { | ||
this.name = name; | ||
this[CONNECTION_PROP] = DbConnectionId++; | ||
} | ||
} | ||
|
||
class Book { | ||
public name: string; | ||
public authors: Array<Author>; | ||
public [CONNECTION_PROP]: DbConnection; | ||
|
||
constructor(name: string, authors: Array<Author>) { | ||
this.name = name; | ||
this.authors = authors; | ||
this[CONNECTION_PROP] = DbConnectionId++; | ||
} | ||
} | ||
|
||
const areAuthorsEqual: Tester = (a: unknown, b: unknown) => { | ||
const isAAuthor = a instanceof Author; | ||
const isBAuthor = b instanceof Author; | ||
|
||
if (isAAuthor && isBAuthor) { | ||
return a.name === b.name; | ||
} else if (isAAuthor === isBAuthor) { | ||
return undefined; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
const areBooksEqual: Tester = function ( | ||
this: MatcherContext, | ||
a: unknown, | ||
b: unknown, | ||
customTesters: Tester[], | ||
) { | ||
const isABook = a instanceof Book; | ||
const isBBook = b instanceof Book; | ||
|
||
if (isABook && isBBook) { | ||
return (a.name === b.name && | ||
this.equal(a.authors, b.authors, { customTesters: customTesters })); | ||
} else if (isABook === isBBook) { | ||
return undefined; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
const book1 = new Book("Book 1", [ | ||
new Author("Author 1"), | ||
new Author("Author 2"), | ||
]); | ||
const book1b = new Book("Book 1", [ | ||
new Author("Author 1"), | ||
new Author("Author 2"), | ||
]); | ||
|
||
expect.addEqualityTesters([ | ||
areAuthorsEqual, | ||
areBooksEqual, | ||
]); | ||
|
||
expect.extend({ | ||
toEqualBook(context, expected) { | ||
const actual = context.value as Book; | ||
const result = context.equal(expected, actual, { | ||
customTesters: context.customTesters, | ||
}); | ||
|
||
return { | ||
message: () => | ||
`Expected Book object: ${expected.name}. Actual Book object: ${actual.name}`, | ||
pass: result, | ||
}; | ||
}, | ||
toBeWithinRange(context, floor: number, ceiling: number) { | ||
const actual = context.value as number; | ||
const pass = actual >= floor && actual <= ceiling; | ||
if (pass) { | ||
return { | ||
message: () => | ||
`expected ${actual} not to be within range ${floor} - ${ceiling}`, | ||
pass: true, | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected ${actual} to be within range ${floor} - ${ceiling}`, | ||
pass: false, | ||
}; | ||
} | ||
}, | ||
}); | ||
|
||
Deno.test("expect.extend() api test case", () => { | ||
expect(book1).toEqualBook(book1b); | ||
}); |
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
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
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.
We have a style guide about this. We prefer
function
over arrow syntax https://docs.deno.com/runtime/manual/references/contributing/style_guide#top-level-functions-should-not-use-arrow-syntaxThere 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.
Oh, thank you. I missed it.