|
| 1 | +var longjohn = require('../index') |
| 2 | + , assert = require("assert"); |
| 3 | + |
| 4 | +describe('longjohn', function() { |
| 5 | + it('should work with normal throw', function() { |
| 6 | + assert.throws(function() { |
| 7 | + throw new Error('foo'); |
| 8 | + }); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should work with errors from other modules', function(done) { |
| 12 | + var fs = require('fs'); |
| 13 | + |
| 14 | + fs.readFile('not_there.txt', 'utf8', function(err, text) { |
| 15 | + if (err && err instanceof Error && err.stack === "Error: ENOENT, open 'not_there.txt'") { |
| 16 | + return done(); |
| 17 | + } |
| 18 | + assert.fail(); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should track frames across setTimeout', function(done) { |
| 23 | + setTimeout(function() { |
| 24 | + console.log(new Error('foobar').stack); |
| 25 | + }, 1); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should allow stack size limiting', function(done) { |
| 29 | + longjohn.async_trace_limit = 2; |
| 30 | + |
| 31 | + var counter = 0; |
| 32 | + |
| 33 | + var foo = function() { |
| 34 | + if (++counter > 3) { |
| 35 | + assert.throws(function() { |
| 36 | + try { |
| 37 | + throw new Error('foo'); |
| 38 | + } catch (e) { |
| 39 | + throw e; |
| 40 | + } |
| 41 | + }, function(err) { |
| 42 | + return err.stack.split(longjohn.empty_frame).length === 3; |
| 43 | + }); |
| 44 | + return done(); |
| 45 | + } |
| 46 | + setTimeout(foo, 1); |
| 47 | + } |
| 48 | + |
| 49 | + foo(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should work with on/emit', function() { |
| 53 | + var count = 0; |
| 54 | + |
| 55 | + var foo = function() { |
| 56 | + ++count; |
| 57 | + }; |
| 58 | + |
| 59 | + var emitter = new (require('events').EventEmitter)(); |
| 60 | + |
| 61 | + emitter.on('foo', foo); |
| 62 | + emitter.on('foo', foo); |
| 63 | + emitter.on('foo', foo); |
| 64 | + |
| 65 | + emitter.emit('foo'); |
| 66 | + |
| 67 | + assert.equal(count, 3); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should work with removeListener', function() { |
| 71 | + var count = 0; |
| 72 | + |
| 73 | + var foo = function() { |
| 74 | + ++count; |
| 75 | + }; |
| 76 | + |
| 77 | + var emitter = new (require('events').EventEmitter)(); |
| 78 | + |
| 79 | + emitter.on('foo', foo); |
| 80 | + emitter.on('foo', foo); |
| 81 | + |
| 82 | + emitter.removeListener('foo', foo); |
| 83 | + |
| 84 | + emitter.emit('foo'); |
| 85 | + |
| 86 | + assert.equal(count, 1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should work with setTimeout', function(done) { |
| 90 | + setTimeout(function() { |
| 91 | + assert.deepEqual(Array.prototype.slice.call(arguments), [1, 2, 3]); |
| 92 | + done(); |
| 93 | + }, 1000, 1, 2, 3); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should work with setInterval', function(done) { |
| 97 | + setInterval(function() { |
| 98 | + assert.deepEqual(Array.prototype.slice.call(arguments), [1, 2, 3]); |
| 99 | + done(); |
| 100 | + }, 1000, 1, 2, 3); |
| 101 | + }); |
| 102 | +}); |
0 commit comments