forked from bradtraversy/jest_testing_basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.test.js
122 lines (101 loc) · 3.22 KB
/
functions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const functions = require('./functions');
// dummy functions
// const initDatabase = () => console.log('Database initialized....');
// const closeDatabase = () => console.log('Database closed....');
// Life-cycle methods - functions that run before or after each test
// beforeEach(() => initDatabase());
// afterEach(() => closeDatabase());
// beforeAll(() => initDatabase());
// afterAll(() => closeDatabase());
const nameCheck = () => console.log('Checking names...');
describe('Checking names', () => {
beforeEach(() => nameCheck());
test('User is Jeff', () => {
const user = 'Jeff';
expect(user).toBe('Jeff');
});
test('User is Karen', () => {
const user = 'Karen';
expect(user).toBe('Karen');
});
});
// toBe
test('Adds 2 + 2 to equal 4', () => {
expect(functions.add(2, 2)).toBe(4);
});
test('Adds 2 + 2 to not equal 5', () => {
expect(functions.add(2, 2)).not.toBe(5);
});
// toBeNull
test('Should be null', () => {
expect(functions.isNull()).toBeNull();
});
// toBeUndefined
test('Should be undefined', () => {
expect(functions.isUndefined()).toBeUndefined();
});
// toBeTruthy
test('Should be truthy', () => {
expect(functions.isTruthy()).toBeTruthy();
});
// toBeFalsy
test('Should be falsy', () => {
expect(functions.isFalsy()).toBeFalsy();
expect(functions.checkValue(null)).toBeFalsy();
expect(functions.checkValue(undefined)).toBeFalsy();
expect(functions.checkValue(0)).toBeFalsy();
expect(functions.checkValue(1)).not.toBeFalsy();
expect(functions.checkValue('')).toBeFalsy();
});
// Testing objects for equality
// toBe is for primitive types
test('User should be Jst Polencek object', () => {
expect(functions.createUser()).toEqual({ firstName: 'Jst', lastName: 'Polencek' });
});
// less than and greater than
test('Should be under 1600', () => {
const load1 = 800;
const load2 = 700;
const load3 = 800;
expect(load1 + load2).toBeLessThan(1600);
expect(load1 + load3).toBeLessThanOrEqual(1600);
});
// regex
test('There is no I in team', () => {
expect('team').not.toMatch(/I/);
// playing around
expect('teamI').toMatch(/I/);
expect('teami').not.toMatch(/I/);
expect('teami').toMatch(/I/i);
});
// arrays
test('admin should be in username', () => {
const usernames = ['john', 'karen', 'admin'];
expect(usernames).toContain('admin');
expect(usernames).not.toContain('chris');
});
// Promise !
// working with async data
test('user fetched name should be Leanne Graham', () => {
expect.assertions(1); // number of assertions we expect
return functions.fetchUser().then((data) => {
expect(data.name).toEqual('Leanne Graham');
});
});
// WRONG BEHAVIOUR -> this sometimes passes, and sometimes it doesn't
// no matter what we put in toEqual() !!!!
// we need both assertion and return, for it to work correctly
/*
test('user fetched name should be Leanne Graham', () => {
// expect.assertions(1); // number of assertions we expect
functions.fetchUser().then((data) => {
expect(data.name).toEqual('Leanne Graham1');
});
});
*/
// Async await syntax
test('user fetched name should be Leanne Graham', async () => {
expect.assertions(1); // number of assertions we expect
const data = await functions.fetchUser();
expect(data.name).toEqual('Leanne Graham');
});