Skip to content

Update first_rule.md #3

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion testing/first_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,64 @@ Clean tests should follow the rules:
- **Self-Validating** a test should answer with either _Passed_ or _Failed_. You don't need to compare log files to answer if a test passed.

- **Timely** unit tests should be written before the production code. If you write tests after the production code, you might find writing tests too hard.


** Good

```typescript
// add.ts

export const add = (a: number, b: number): number => {
return a + b;
};

// add.test.ts

import { add } from './add';

describe('add function', () => {
// Fast: This test is very quick to run.
// Independent: This test does not depend on any other tests.
// Repeatable: This test is repeatable, as it will always produce the same result given the same inputs.
// Self-Validating: The test itself will report if it passes or fails.
// Timely: The test is written before the actual function (in a TDD manner).

it('correctly adds two numbers', () => {
const result = add(1, 2);
expect(result).toBe(3);
});

// We can also add more tests to cover more cases
it('correctly adds two negative numbers', () => {
const result = add(-1, -2);
expect(result).toBe(-3);
});

it('correctly adds a positive and a negative number', () => {
const result = add(-1, 2);
expect(result).toBe(1);
});
});
```

** Bad

```typescript
// add.test.ts

import { add } from './add';

let previousResult = 0;

describe('add function', () => {
it('correctly adds two numbers', () => {
const result = add(1, 2);
previousResult = result;
expect(result).toBe(3);
});

it('correctly adds two other numbers', () => {
const result = add(previousResult, 5);
expect(result).toBe(8);
});
});
```