tests.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const
  2. should = require('should'),
  3. memwatch = require('./');
  4. describe('the library', function() {
  5. it('should export a couple functions', function(done) {
  6. should.exist(memwatch.gc);
  7. should.exist(memwatch.on);
  8. should.exist(memwatch.once);
  9. should.exist(memwatch.removeAllListeners);
  10. should.exist(memwatch.HeapDiff);
  11. done();
  12. });
  13. });
  14. describe('calling .gc()', function() {
  15. it('should cause a stats() event to be emitted', function(done) {
  16. memwatch.once('stats', function(s) {
  17. s.should.be.a('object');
  18. done();
  19. });
  20. memwatch.gc();
  21. });
  22. });
  23. describe('HeapDiff', function() {
  24. it('should detect allocations', function(done) {
  25. function LeakingClass() {};
  26. var arr = [];
  27. var hd = new memwatch.HeapDiff();
  28. for (var i = 0; i < 100; i++) arr.push(new LeakingClass());
  29. var diff = hd.end();
  30. (Array.isArray(diff.change.details)).should.be.ok;
  31. diff.change.details.should.be.an.instanceOf(Array);
  32. // find the LeakingClass elem
  33. var leakingReport;
  34. diff.change.details.forEach(function(d) {
  35. if (d.what === 'LeakingClass')
  36. leakingReport = d;
  37. });
  38. should.exist(leakingReport);
  39. ((leakingReport['+'] - leakingReport['-']) > 0).should.be.ok;
  40. done();
  41. });
  42. });
  43. describe('HeapDiff', function() {
  44. it('double end should throw', function(done) {
  45. var hd = new memwatch.HeapDiff();
  46. var arr = [];
  47. (function() { hd.end(); }).should.not.throw();
  48. (function() { hd.end(); }).should.throw();
  49. done();
  50. });
  51. });
  52. describe('improper HeapDiff allocation', function() {
  53. it('should throw an exception', function(done) {
  54. // equivalent to "new require('memwatch').HeapDiff()"
  55. // see issue #30
  56. (function() { new (memwatch.HeapDiff()); }).should.throw();
  57. done();
  58. });
  59. });