-
Notifications
You must be signed in to change notification settings - Fork 0
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
✨ Add: custom assertion class for robust and type-safe testing ✨ #64
Open
vidup
wants to merge
38
commits into
master
Choose a base branch
from
experiment/assertions-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
…/threw check), or any argument (compare with actual rejection/caught errors)
…nto experiment/assertions-2
…e contains a matcher deeply even if dev passed a plain structure at root level
feature: automatic data generation by leveraging matchers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces a new, matcher-based, expect-style API assertion library within our @vdstack/mockit package. The goal is to provide developers with a more powerful, flexible, and type-safe way to write assertions, reducing test brittleness and focusing on the behavior that matters rather than requiring exact value matches.
Motivation:
Traditional assertions often force tests to assert on every detail of an object or value. This approach can lead to brittle tests that break due to non-critical changes. By using matcher-based assertions, tests can:
Decouple from implementation details: Only assert on the properties that truly matter.
Improve readability: The intent of tests becomes clear with matchers like
m.anyString()
orm.stringMatching(/@example\.com$/)
.Enhance maintainability: Tests remain stable when the data structure evolves, unless the specific properties under test are changed.
Leverage type-safety: Our TypeScript integration ensures that assertions are checked at compile time, reducing runtime errors. It also helps developers at development time to write assertions faster.
Key Changes
Documentation Update
A new README is added that comprehensively documents the assertion library.
The README explains:
Asserting on direct values
Partial Object Matching
When you only care about specific properties:
Deep Object Matching
The most concise way to match nested objects:
Assertion Class Implementation
I added a new Assertion class that implements the following methods:
toEqual(expected: T)
: Supports both direct and function-based evaluations, enabling lazy assertions.toThrow(expectedError?: unknown)
: Asserts that a function throws an error, optionally matching the error against a provided value.toContain(expected)
: Checks if an array contains an element or if an object contains a subset of properties.toResolve(expected?)
: Handles assertions for promises or functions returning promises.toReject(expectedRejection?)
: Validates that a promise or async function rejects, with optional matching for the rejection value.Exposed API
A new helper function
expect<T>(actual: T)
is exposed, which creates an instance of the Assertion class.This API mirrors familiar patterns (similar to Jest's expect), making it easy for users to adopt: