-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathassertThatSpec.js
More file actions
131 lines (101 loc) · 3.85 KB
/
assertThatSpec.js
File metadata and controls
131 lines (101 loc) · 3.85 KB
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
123
124
125
126
127
128
129
130
131
'use strict';
const assert = require('assert');
const AssertionError = require('assertion-error');
const __ = require('../..');
const TestMatcher = require('./TestMatcher');
describe('assertThat', () => {
it('should do nothing on success', () => {
__.assertThat('truth', new TestMatcher());
});
it('should pass value to matcher', () => {
const input = 'assertion value';
let passedValue;
__.assertThat(input, new TestMatcher((value) => {
passedValue = value;
return true;
}));
assert.ok(passedValue === input, 'Received: ' + passedValue);
});
it('should wrap non-matcher values in `equalTo()` (without message)', () => {
assert.doesNotThrow(() => __.assertThat(true, true));
assert.doesNotThrow(() => __.assertThat('some string', 'some string'));
assert.doesNotThrow(() => __.assertThat({a: 'value'}, {a: 'value'}));
__.assertThat(() => __.assertThat('some value', 'other value'), __.throws(__.instanceOf(AssertionError)));
});
it('should wrap non-matcher values in `equalTo()` (with a message)', () => {
assert.doesNotThrow(() => __.assertThat('Boolean', true, true));
assert.doesNotThrow(() => __.assertThat('String', 'some string', 'some string'));
assert.doesNotThrow(() => __.assertThat('Objects', {a: 'value'}, {a: 'value'}));
__.assertThat(() => __.assertThat('Mismatching strings', 'some value', 'other value'), __.throws(__.instanceOf(AssertionError)));
});
it('should format assertion message if matcher fails', () => {
let thrown;
try {
__.assertThat('real value', new TestMatcher(() => false));
} catch (e) {
thrown = e;
}
assert.ok(thrown instanceof AssertionError, 'Should throw AssertionError. Threw ' + thrown);
assert.equal(thrown.message, '\nExpected: Matcher description\n but: was "real value"');
});
it('should prepend message, if available', () => {
let thrown;
try {
__.assertThat('Assertion message', 'real value', new TestMatcher(() => false));
} catch (e) {
thrown = e;
}
assert.ok(thrown instanceof AssertionError, 'Should throw AssertionError. Threw ' + thrown);
assert.equal(thrown.message, 'Assertion message\nExpected: Matcher description\n but: was "real value"');
});
it('should pass diff representations to AssertionError', () => {
let thrown;
const testMatcher = new TestMatcher(() => false);
testMatcher.getExpectedForDiff = () => {
return 'expected for diff';
};
testMatcher.formatActualForDiff = function (actual) {
return 'actual for diff: ' + actual;
};
try {
__.assertThat('actual value', testMatcher);
} catch (e) {
thrown = e;
}
assert.equal(thrown.expected, 'expected for diff');
assert.equal(thrown.actual, 'actual for diff: actual value');
});
it('should throw if matcher returns a promise', () => {
let thrown;
try {
__.assertThat('a value', new TestMatcher(() => Promise.resolve(true)));
} catch (e) {
thrown = e;
}
assert.ok(thrown instanceof AssertionError, 'Should throw AssertionError. Threw ' + thrown);
assert.equal(thrown.message, 'Matcher returned a promise instead of a boolean - use promiseThat for promising matchers!');
});
it('should call expect().nothing() function if it’s available (to suppress jasmine’s "no expectations" error)', () => {
let expectNothingWasCalled = false;
global.expect = () => {
return {
nothing() {
expectNothingWasCalled = true;
}
};
};
__.assertThat('truth', __.is(__.anything()));
assert.equal(expectNothingWasCalled, true);
});
it('should not throw when nothing is not available on expect (fix "Invalid Chai property" in vitest)', () => {
const chaiFake = {irrelevant: 'property'};
global.expect = () => new Proxy(chaiFake, {
get(target, property) {
if (!Object.keys(target).includes(property)) {
throw Error(`Invalid Chai property: ${property}`);
}
}
});
__.assertThat(true, __.is(__.equalTo(true)));
});
});