-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.js
69 lines (51 loc) · 1.9 KB
/
index_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// for node based assertion
const assert = require('assert');
const fs = require('fs');
describe('Math', () => {
describe('.max', () => {
// A hook is a piece of code that is executed when a certain event happens.
afterEach(() => {
// teardown goes here
});
/* Setup, Exercise, and Verify
The phases are defined as follows:
1. Setup - create objects, variables, and set conditions that your test depends on
2. Exercise - execute the functionality you are testing
3. Verify - check your expectations against the result of the exercise phase.
You can use the assert library here
Clearly separating each phase makes a test easier to read, change, and validate.
*/
it('returns the argument with the highest value', () => {
// Setup
const minimum = 1;
const median = 2;
const maximum = 3;
// Exercise
const result = Math.max(median, minimum, maximum);
// Verify
assert.ok(result === maximum);
// Teardown
});
it('returns -Infinity when no arguments are provided', () => {
const negInfinity = -Infinity;
const result = Math.max();
assert.ok(result === negInfinity);
});
});
describe('.fs example', () => {
/* Setup, Exercise, and Verify
*/
it('compare file contain Hello string', () => {
// Setup
const path = './message.txt';
const str = 'Hello Node.js';
// Exercise
fs.appendFileSync(path, str);
// Verify
const contents = fs.readFileSync(path);
assert.ok(contents.toString() === str);
// Teardown
fs.unlinkSync(path);
});
});
});