31
loading...
This website collects cookies to deliver better user experience
writing tests is less about the syntax but more on the TDD philosophy,
There are two types of messages : query and command. Queries return something or changes nothing. Command types return nothing but changes something.
Message Type | Query | Command |
---|---|---|
Incoming |
Assert result Test incoming query messages by making assertions about what they send back. Test the interface and not the implementation. |
Test incoming command messages by making assertions about direct public side effects. DRY it out. Receiver of incoming message has sole responsibility for asserting the result of direct public side effects. |
Sent to Self | Ignore: Do not test private methods. | Ignore: Do not test private methods. |
Outgoing | Ignore. The receiver of an incoming query is solely responsible for assertions that involve state. If a message has no visible side effects, the sender should not test it |
Expect to send outgoing command messages using mocks |
yarn
or npm
.test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
toBe(3)
portion is the matcher. When Jest runs, it tracks all the failing matchers so it can print nice error messages. The toBe
matcher uses Object.is
to test the equality. undefined
, null
, false
might be needed to be checked also. Jest contains helpers that lets developers be explicit with what to expect. It is then good to use a matcher that most precisely corresponds to what the code is doing.toBeNull
matches only null
toBeUndefined
matches only undefined
toBeDefined
is the opposite of toBeUndefined
toBeTruthy
matches anything that an if
statement treats as truetoBeFalsy
matches anything that an if
statement treats as falsetoBeGreaterThan
, toBeGreaterThanOrEqual
, toBeLessThan
, toBeLessThanOrEqual
. For floating point numbers, there are equality matcher like toBeCloseTo
.toMatch
.toContain
can be used to check if a particular item can be found in an array or iterable.toThrow
can be used to check if a particular function throws a specific error. It is to be noted that the function being checked needs to be invoked within a wrapping function for the toThrow
exception to work.There are also more advanced Jest matchers used for testing asynchronous code, i.e for callbacks and promises.
capitalize(string)
takes a string and returns that string with the first character capitalized.const capitalize = require('../capitalize');
test('should capitalize lowercase string correctly', () => {
expect(capitalize("capitalize")).toBe("Capitalize");
});
test("should return '' for strings with length 0", () => {
expect(capitalize("")).toBe("");
});
// other tests here
reverseString(string)
takes a string and returns it reversed. Below is a snippet of the test I wrote for a normal scenario.const reverseString = require('../reverse-string');
test('should reverse normal strings', () => {
expect(reverseString("reverse")).toBe("esrever");
});
//other tests here
calculator
object that contains the basic operations: add
, subtract
, divide
, and multiply
. The following test snippet below shows that the method will throw an error message if the divisor is zero.const calculator = require("../calculator");
//other tests here
test("should throw an error if divisor is 0", () => {
expect(() => calculator.divide(20, 0)).toThrow("cannot divide by 0");
});
caesar cipher
. A caesar cipher is a substitution cipher where each letter in the text is shifted a certain places number down the alphabet. More info can be read here. const caesar = require("../caesar-cipher");
//other tests here
test('wraps', function() {
expect(caesar('Z', 1)).toEqual('A');
});
test('works with large shift factors', function() {
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
});
test('works with large negative shift factors', function() {
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
});
average
, min
, max
, and length
.const analyze = require("../analyze");
const object = analyze([1,8,3,4,2,6]);
test("should return correct average", () => {
expect(object.average).toEqual(4);
});
test("should return correct min", () => {
expect(object.min).toEqual(1);
});
// other tests here