|
| 1 | +// this test is run by Vows (as all files matching *test.js) |
| 2 | + |
| 3 | +var vows = require('vows'), |
| 4 | + should = require('should'); |
| 5 | + |
| 6 | +var createSchema = require('..').createSchema, |
| 7 | + config = require('./config'); |
| 8 | + |
| 9 | +var schemaSimple = { |
| 10 | + type: 'string' |
| 11 | +}; |
| 12 | + |
| 13 | +vows.describe('Async Error').addBatch({ |
| 14 | + 'when object is valid and callback does not throw an error': { |
| 15 | + topic: function () { |
| 16 | + var schema = createSchema(schemaSimple); |
| 17 | + var count = 0; |
| 18 | + try { |
| 19 | + schema.validate('a string', function nice() { |
| 20 | + ++count; |
| 21 | + if (count === 1) { |
| 22 | + // do nothing, play nice |
| 23 | + } else { |
| 24 | + this.callback(new Error('Callback was called twice')); |
| 25 | + } |
| 26 | + }.bind(this)); |
| 27 | + this.callback(); |
| 28 | + } catch(err) { |
| 29 | + this.callback(err); |
| 30 | + } |
| 31 | + }, |
| 32 | + 'we get no error': function (err, result) { |
| 33 | + if (err) { |
| 34 | + console.error(err.toString()); |
| 35 | + } |
| 36 | + should.not.exist(err); |
| 37 | + should.not.exist(result); |
| 38 | + } |
| 39 | + }, |
| 40 | + 'when object is valid and callback throws an error': { |
| 41 | + topic: function () { |
| 42 | + var schema = createSchema(schemaSimple); |
| 43 | + var count = 0; |
| 44 | + try { |
| 45 | + schema.validate('a string', function naughty() { |
| 46 | + ++count; |
| 47 | + if (count === 1) { |
| 48 | + throw new Error('I am a naughty callback function'); |
| 49 | + } else { |
| 50 | + this.callback(new Error('Callback was called twice')); |
| 51 | + } |
| 52 | + }.bind(this)); |
| 53 | + this.callback(new Error('we did not get an error')); |
| 54 | + } catch(err) { |
| 55 | + // error is expected |
| 56 | + this.callback(); |
| 57 | + } |
| 58 | + }, |
| 59 | + 'we get an error': function (err, result) { |
| 60 | + if (err) { |
| 61 | + console.error(err.toString()); |
| 62 | + } |
| 63 | + should.not.exist(err); |
| 64 | + should.not.exist(result); |
| 65 | + } |
| 66 | + }, |
| 67 | + 'when object is invalid and callback throws an error': { |
| 68 | + topic: function () { |
| 69 | + var schema = createSchema(schemaSimple); |
| 70 | + var count = 0; |
| 71 | + try { |
| 72 | + schema.validate(42, function naughty() { |
| 73 | + ++count; |
| 74 | + if (count === 1) { |
| 75 | + throw new Error('I am a naughty callback function'); |
| 76 | + } else { |
| 77 | + this.callback(new Error('Callback was called twice')); |
| 78 | + } |
| 79 | + }.bind(this)); |
| 80 | + this.callback(new Error('we did not get an error')); |
| 81 | + } catch(err) { |
| 82 | + // error is expected |
| 83 | + this.callback(); |
| 84 | + } |
| 85 | + }, |
| 86 | + 'we get an error': function (err, result) { |
| 87 | + if (err) { |
| 88 | + console.error(err.toString()); |
| 89 | + } |
| 90 | + should.not.exist(err); |
| 91 | + should.not.exist(result); |
| 92 | + } |
| 93 | + } |
| 94 | +}).export(module); |
0 commit comments