generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add jest custom matcher toEqualMoment() (#1980)
* test: Add toBeSameMoment() custom matcher * test: Rework tests to use toBeSameMoment() custom matcher To give meaningful output upon test failure * test: Rework tests to use toBeSameMoment() custom matcher To give meaningful output upon test failure * test: .not.toBeSameMoment() now gives meaningful message * test: Extract variables in toBeSameMoment() before adding support for received being null * test: toBeSameMoment() is now useful if received "date" is null * test: Simplify moment test, now that toBeSameMoment() handles nulls * test: Rename toBeSameMoment() to toEqualMoment()
- Loading branch information
1 parent
f3e4760
commit 3156647
Showing
5 changed files
with
62 additions
and
42 deletions.
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,26 @@ | ||
import type moment from 'moment'; | ||
import { diff } from 'jest-diff'; | ||
|
||
declare global { | ||
namespace jest { | ||
interface Matchers<R> { | ||
toEqualMoment(expected: moment.Moment): CustomMatcherResult; | ||
} | ||
} | ||
} | ||
|
||
// Based on https://stackoverflow.com/a/60229956/104370 | ||
export function toEqualMoment(received: moment.Moment | null, expected: moment.Moment): jest.CustomMatcherResult { | ||
const pass: boolean = expected.isSame(received); | ||
const expectedAsText = expected.toISOString(); | ||
const receivedAsText = received ? received.toISOString() : 'null'; | ||
const message: () => string = () => | ||
pass | ||
? `Received moment should not be ${expectedAsText}` | ||
: `Received moment is not the same as expected: ${diff(expectedAsText, receivedAsText)}`; | ||
|
||
return { | ||
message, | ||
pass, | ||
}; | ||
} |
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
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