Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing

The general guideline for integration testing is to cover the longest happy path and any edge cases that can't be exercised by unit tests.


From The Art of Unit Testing book.

Criteria of good test:

  1. Readable
  2. Maintainable
  3. Trustworthy

Technically, one the biggest benefits of TDD is that by seeing a test fail, and then seeing it pass without changing the test, you are basically testing the test itself.

Format:

describe("unit name under test", () => {
    it("expectation, scenario", () => {
        // Arrange.

        // Act.

        // Assert.
    });
})

Use parameterized test or test.each() (p.54).

describe('one uppercase rule, with vanilla JS for', () => {
    const tests = {
        'Abc': true,
        'aBc': true,
        'abc': false,
    };
    for (const [input, expected] of Object.entries(tests)) {
        test('given ${input}, ${expected}', () => {
            const result = oneUpperCaseRule(input);
            expect(result.passed).toEqual(expected);
        });
    }
});
  • Don't use beforeEach and afterEach. Reason: scroll fatigue (p.47).

  • Avoid magic values.

  • Avoid logic in unit test.

  • Avoid dynamically creating the expected value in your asserts; use hardcoded values when possible.

  • Separate asserts from actions. Below is bad.

    expect(verifier.verify("any value")[0]).toContain("fake reason");
    
  • Don't test multiple exit points (p.158). If you’re testing more than one thing, giving the test a good name that indicates what’s being tested is difficult

  • Sometimes it’s perfectly okay to assert multiple things in the same test, as long as they are not multiple concerns.

  • BAD: Looping inside test().

  • GOOD: Looping the test().

Resources

Book

  1. The Art of Unit Testing. Third Edition. Roy Osherove.
  2. Unit Testing Principles, Practices, and Patterns. Vladimir Khorikov.
  3. Build Your Own Test Framework. Daniel Irvine.
  1. Node test runner doc
  2. Node assert doc
  3. Node.js native test runner is great
  4. Migrating 500+ tests from Mocha to Node.js
  5. Complete Guide to the Node.js Test Runner
  6. Unit testing best practices for .NET