-
Notifications
You must be signed in to change notification settings - Fork 0
/
jasmineCheatSheet.js
119 lines (101 loc) · 3.56 KB
/
jasmineCheatSheet.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
describe('built-in matchers', function() {
describe('toBeTruthy', function() {
it('passes if subject is true', function() {
expect(true).toBeTruthy();
expect(false).not.toBeTruthy();
});
});
describe('toBeFalsy', function() {
it('passes if subject is false', function() {
expect(false).toBeFalsy();
expect(true).not.toBeFalsy();
});
});
describe('toBeDefined', function() {
it('passes if subject is not undefined', function() {
expect({}).toBeDefined();
expect(undefined).not.toBeDefined();
});
});
describe('toBeNull', function() {
it('passes if subject is null', function() {
expect(null).toBeNull();
expect(undefined).not.toBeNull();
expect({}).not.toBeNull();
});
});
describe('toEqual', function() {
it('passes if subject and expectation are equivalent', function() {
expect('Hello World!').toEqual('Hello World!');
expect('Hello World!').not.toEqual('Goodbye!');
expect('Hello World!').toNotEqual('Hi!');
expect([1, 2, 3]).toEqual([1, 2, 3]);
expect(1).toEqual(1);
expect({
foo: 1
}).toEqual({
foo: 1
});
});
});
describe('toBeCloseTo', function() {
it('checks that the expected item is equal to the actual item up to a given level of decimal precision ', function() {
expect(1.223).toBeCloseTo(1.22);
expect(1.233).not.toBeCloseTo(1.22);
expect(1.23326).toBeCloseTo(1.23324, 3);
});
});
describe('toContain', function() {
it('passes if the expected item is an element in the actual array', function() {
expect([1, 2, 3]).toContain(2);
expect([1, 2, 3]).not.toContain(4);
});
});
describe('toMatch', function() {
it('compares the actual to the expected using a regular expression', function() {
expect('Hello Jasmine').toMatch(/jasmine/i);
expect('phone: 123-45-67').toMatch(/\d{3}-\d{2}-\d{2}/);
});
});
describe('toBeGreaterThan', function() {
it('passes if the actual value is greater than the expected value', function() {
expect(2).toBeGreaterThan(1);
});
});
describe('toBeLessThan', function() {
it('passes if the actual value is less than the expected value', function() {
expect(2).toBeLessThan(3);
});
});
describe('toThrow', function() {
it('checks that the expected exception was thrown by the actua', function() {
var object = {
doSomething: function() {
throw new Error("Unexpected error!")
}
};
expect(object.doSomething).toThrow(new Error("Unexpected error!"));
});
});
});
// Jasmine spec runner
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var trivialReporter = new jasmine.TrivialReporter();
jasmineEnv.addReporter(trivialReporter);
jasmineEnv.specFilter = function(spec) {
return trivialReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
trivialReporter.outerDiv.className += ' show-passed';
}
})();