-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patherrors.spec.js
47 lines (45 loc) · 1.58 KB
/
errors.spec.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
const assert = require('assert');
const { Rools, Rule } = require('..');
const { frank } = require('./facts/users')();
const { good } = require('./facts/weather')();
const {
ruleMoodGreat, ruleMoodSad, ruleGoWalking, ruleStayAtHome,
} = require('./rules/mood');
require('./setup');
describe('Rools.evaluate() / errors', () => {
it('should not fail if `when` throws error', async () => {
const brokenRule = new Rule({
name: 'broken rule #1',
when: (facts) => facts.bla.blub === 'blub', // TypeError: Cannot read property 'blub' of undefined
then: () => {},
});
const rools = new Rools({ logging: { error: false } });
const facts = { user: frank, weather: good };
await rools.register([brokenRule, ruleMoodGreat, ruleMoodSad, ruleGoWalking, ruleStayAtHome]);
try {
await rools.evaluate(facts);
} catch (error) {
assert.fail();
}
expect(facts.user.mood).to.be.equal('great');
expect(facts.goWalking).to.be.equal(true);
});
it('should fail if `then` throws error', async () => {
const brokenRule = new Rule({
name: 'broken rule #2',
when: () => true, // fire immediately
then: (facts) => {
facts.bla.blub = 'blub'; // TypeError: Cannot read property 'blub' of undefined
},
});
const rools = new Rools({ logging: { error: false } });
const facts = { user: frank, weather: good };
await rools.register([brokenRule, ruleMoodGreat, ruleMoodSad, ruleGoWalking, ruleStayAtHome]);
try {
await rools.evaluate(facts);
assert.fail();
} catch (error) {
// ignore
}
});
});