Skip to content

Commit f57bc21

Browse files
docs: add jest and mocha tutorial (#39)
* docs: add jest tutorial * docs: move tutorial.md to docs/jest-tutorial.md and add keeping Jests expect section * docs: add install and usage section to README.md and moved Development section to docs/development.md * docs: add mocha tutorial and minor fix to jest tutorial * docs: added links to tutorials in README * address @JoseLion feedback * implement feedback from @JoseLion * Implement feedback * Address feedback from @jpvillaisaza and @flandrade Co-authored-by: Christian <[email protected]>
1 parent f0cbdde commit f57bc21

File tree

4 files changed

+198
-19
lines changed

4 files changed

+198
-19
lines changed

README.md

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,62 @@
55

66
A type-safe fluent assertion library inspired by [Jest](https://jestjs.io/docs/expect) assertions and the popular [AssertJ](https://assertj.github.io/doc/).
77

8-
## Development
8+
## Install
9+
```
10+
npm install --save-dev @stackbuilders/assertive-ts
11+
```
12+
Or:
13+
```
14+
yarn add --dev @stackbuilders/assertive-ts
15+
```
916

10-
To setup your local environment follow the next steps.
17+
## Usage
1118

12-
### Requirements
19+
Import the library in your test script:
20+
```typescript
21+
import { expect } from "@stackbuilders/assertive-ts"
22+
```
1323

14-
* Latest [Yarn Classic](https://classic.yarnpkg.com)
15-
* NodeJS (Use version set on [.nvmrc](https://github.com/stackbuilders/assertive-ts/blob/master/.nvmrc))
24+
Use the `expect` function along with a "matcher" function on the value you want to assert:
25+
```typescript
26+
expect(sum(1, 2)).toBeEqual(3);
27+
```
1628

17-
### Install dependencies
29+
To assert the opposite, just add `.not` before a matcher:
30+
```typescript
31+
expect(sum(1, 2)).not.toBeNull();
32+
```
1833

19-
```console
20-
yarn install
34+
With `assertive-ts` you can use **fluent assertions**, which means you can chain multiple matcher functions to the same value under test:
35+
```typescript
36+
expect("assertive-ts is awesome!")
37+
.toStartWith("assertive-ts")
38+
.not.toContain("unsafe")
39+
.toEndWith("awesome!");
2140
```
2241

23-
### Compile TS files
42+
The matcher functions depend on the type of the value on the `expect`. If you're using TypeScript, the compiler will let you know if something is not available for that assertion:
43+
```typescript
44+
// Boolean assertion
45+
expect(isEven(2)).toBeTrue();
2446

25-
```console
26-
yarn compile
27-
```
47+
// String assertion
48+
expect("foobar").toStartWith("foo");
2849

29-
### Lint code
50+
// Number assertion
51+
expect(sum(1, 2)).toBePositive();
3052

31-
```console
32-
yarn lint
53+
expect(14).toEndWith("4");
54+
^ ? type error: `toEndWith` does not exist in `NumberAssertion`
3355
```
3456

35-
### Run tests
57+
For a list of all matchers and extended documentation, please refer to the API documentation.
58+
59+
## Test Runner Integration
60+
61+
- [Jest Integration](docs/jest-tutorial.md)
62+
- [Mocha Integration](docs/mocha-tutorial.md)
3663

37-
```console
38-
yarn test
39-
```
4064
## Contributors ✨
4165

4266
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

docs/development.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Development
2+
3+
To setup your local environment follow the next steps.
4+
5+
### Requirements
6+
7+
* Latest [Yarn Classic](https://classic.yarnpkg.com)
8+
* NodeJS (Use version set on [.nvmrc](https://github.com/stackbuilders/assertive-ts/blob/master/.nvmrc))
9+
10+
### Install dependencies
11+
12+
```console
13+
yarn install
14+
```
15+
16+
### Compile TS files
17+
18+
```console
19+
yarn compile
20+
```
21+
22+
### Lint code
23+
24+
```console
25+
yarn lint
26+
```
27+
28+
### Run tests
29+
30+
```console
31+
yarn test
32+
```

docs/jest-tutorial.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Usage with Jest
2+
3+
Let's set up a project with Jest and assertive-ts to write our first assertion.
4+
5+
First, let's install the dependencies:
6+
7+
```
8+
npm install --save-dev typescript ts-jest jest @types/jest @stackbuilders/assertive-ts
9+
```
10+
11+
This library is meant to be used with TypeScript, so to have better results we encourage you to use TypeScript in your project.
12+
13+
Let's create a `tsconfig.json` file:
14+
```
15+
npx tsc -init
16+
```
17+
18+
Let's also create a jest configuration file at the root directory by running the following command:
19+
```
20+
npx ts-jest config:init
21+
```
22+
23+
**For more information about Jest and its configuration please refer to the [official documentation](https://jestjs.io/docs/getting-started).**
24+
25+
We are going to test a simple function:
26+
27+
*src/mathUtils.ts*
28+
```typescript
29+
export const sum = (a: number, b: number): number => {
30+
return a + b;
31+
}
32+
```
33+
34+
Now let's write a test for that function. Make sure to import `expect` from the `@stackbuilders/assertive-ts` module:
35+
36+
*tests/mathUtils.test.ts*
37+
```typescript
38+
import { expect } from "@stackbuilders/assertive-ts";
39+
import { sum } from "../src/mathUtils";
40+
41+
describe("sum", () => {
42+
it("adds two numbers", () => {
43+
expect(sum(1, 2)).toBeEqual(3);
44+
})
45+
});
46+
```
47+
48+
And that's it! Now you can run your tests as usual with your test script.
49+
50+
## Keeping Jest's `expect` function
51+
52+
You might want to use the `expect` function from Jest along with assertive-ts assertions. There are a couple ways to achieve this:
53+
54+
- Instead of importing `expect` from the `assertive-ts` module, you can import one of the available aliases: `assert` or `assertThat`:
55+
56+
```typescript
57+
import { expect } from "@jest/globals";
58+
import { assert } from "@stackbuilders/assertive-ts";
59+
```
60+
61+
- Or use an explicit import to rename the `expect` function from Jest to something like `jestExpect`:
62+
63+
```typescript
64+
import { expect as jestExpect } from "@jest/globals";
65+
import { expect } from "@stackbuilders/assertive-ts";
66+
```

docs/mocha-tutorial.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Usage with Mocha
2+
3+
Let's set up a project with Mocha and assertive-ts.
4+
5+
First, let's install the dependencies:
6+
```
7+
npm install --save-dev typescript mocha @types/mocha ts-node @stackbuilders/assertive-ts
8+
```
9+
10+
This library is meant to be used with TypeScript, so to have better results we encourage you to use TypeScript in your project.
11+
12+
Let's create a `tsconfig.json` file:
13+
```
14+
npx tsc -init
15+
```
16+
17+
Let's also create a Mocha configuration file:
18+
19+
*.mocharc.json*
20+
```json
21+
{
22+
"extension": ["ts"],
23+
"spec": "tests/**/*.test.ts",
24+
"require": [
25+
"ts-node/register"
26+
]
27+
}
28+
```
29+
30+
As the config includes the TypeScript transpilation hook `ts-node/register` it does not require pre-compilation before running.
31+
32+
**For more information about Mocha configuration please refer to the [official documentation](https://mochajs.org/#configuring-mocha-nodejs)**
33+
34+
We are going to test a simple function:
35+
36+
*src/mathUtils.ts*
37+
```typescript
38+
export const sum = (a: number, b: number): number => {
39+
return a + b;
40+
}
41+
```
42+
43+
Now let's write a test for that function. Make sure to import `expect` from the `@stackbuilders/assertive-ts` module.
44+
45+
*tests/mathUtils.test.ts*
46+
```typescript
47+
import { expect } from "@stackbuilders/assertive-ts";
48+
import { sum } from "../src/mathUtils";
49+
50+
describe("sum", () => {
51+
it("adds two numbers", () => {
52+
expect(sum(1, 2)).toBeEqual(3);
53+
})
54+
});
55+
```
56+
57+
And that's it! Now you can run your tests as usual with your test script.

0 commit comments

Comments
 (0)