Skip to content

Commit 86f905a

Browse files
authored
docs(readme): Add missing examples (#113)
1 parent df8a260 commit 86f905a

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ A distinctive feature of Assertive.ts with other assertion libraries is that it
2222

2323
### Features
2424

25-
- Type safety and intelligent matcher completion
25+
- Type safety and intelligent matcher completion
2626
- Rich set of expressive and flexible matchers
2727
- Concise, chainable interface inspired by AssertJ
2828
- Works with any test runner and framework such as [Jest](/docs/jest-tutorial.md), [Mocha](/docs/mocha-tutorial.md), or Ava
@@ -39,7 +39,7 @@ For convenience, this library is split into packages grouped within the same nam
3939
Using you favorite test runner, you just need to import the `expect` and test away! If you don't really agree with `expect` as the name of the assertion function, we provide a couple aliases, such as `assert` and `assertThat`.
4040

4141
```ts
42-
import { expect } from "@assertive-ts/core"
42+
import { expect } from "@assertive-ts/core";
4343

4444
describe("sum", () => {
4545
it("returns the sum of two numbers", () => {
@@ -77,10 +77,65 @@ expect("foobar").toStartWith("foo");
7777
// Number assertion
7878
expect(sum(1, 2)).toBePositive();
7979

80+
// Error assertion
81+
expect(new Error(errorMessage)).toHaveMessage(expectedError);
82+
83+
// Array assertion
84+
const data = [1, 2, 3, 4]
85+
expect(data).toMatchAll(x => x < 5);
86+
expect(data).toBeEmpty()
87+
88+
// Date assertion
89+
const date = new Date(2023, 12, 31);
90+
expect(date).toBeAfter(new Date(2023, 12, 1));
91+
expect(date).toBeBefore(new Date(2024, 1, 1));
92+
93+
// Object assertion
94+
const objectData = {
95+
key1: "test1",
96+
key2: "test2",
97+
};
98+
expect(objectData).toContainKey("key1");
99+
expect(objectData).toContainEntry(["key1", "test1"]);
100+
80101
expect(14).toEndWith("4");
81102
^ ? type error: `toEndWith` does not exist in `NumberAssertion`
82103
```
83104

105+
You can also assert over functions and asynchronous code, for example:
106+
107+
```ts
108+
function verifyEnvVar(): void {
109+
const { MY_ENV_VAR } = process.env;
110+
111+
if (!MY_ENV_VAR) {
112+
throw new Error("Missing MY_ENV_VAR environment variable");
113+
}
114+
};
115+
116+
// assertion
117+
expect(() => verifyEnvVar())
118+
.toThrowError(Error)
119+
.toHaveMessage("Missing MY_ENV_VAR environment variable");
120+
121+
expect(() => verifyEnvVar()).not.toThrow();
122+
123+
async function getData(): Promise<DataType> {
124+
const data = await requestApi();
125+
126+
if (!data) {
127+
throw new Error("Data was not found");
128+
}
129+
130+
return data;
131+
}
132+
133+
// assertion
134+
await expect(getData()).toBeRejected();
135+
136+
await expect(getData()).toBeResolved();
137+
```
138+
84139
For a list of all [Core](https://github.com/stackbuilders/assertive-ts/blob/main/packages/core/README.md) matchers and extended documentation, you can refer to the [Core API documentation](https://stackbuilders.github.io/assertive-ts/docs/core/build/).
85140

86141
## Test Runner Integration
@@ -132,6 +187,7 @@ MIT, see [the LICENSE file](LICENSE).
132187
Do you want to contribute to this project? Please take a look at our [contributing guideline](/docs/CONTRIBUTING.md) to know how you can help us build it.
133188

134189
---
190+
135191
<img src="https://www.stackbuilders.com/media/images/Sb-supports.original.png" alt="Stack Builders" width="50%" />
136192

137193
[Check out our libraries](https://github.com/stackbuilders/) | [Join our team](https://www.stackbuilders.com/join-us/)

0 commit comments

Comments
 (0)