Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 2.16 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I test uncaught errors in mocha?
  2. function throwNextTick(error) {
  3.     process.nextTick(function () {
  4.         throw error;
  5.     });
  6. }
  7.        
  8. describe("throwNextTick", function () {
  9.     it("works as expected", function (next) {
  10.         var error = new Error("boo!");
  11.         var recordedError = null;
  12.         process.once("uncaughtException", function (error) {
  13.             recordedError = error;
  14.         });
  15.  
  16.         throwNextTick(error);
  17.  
  18.         process.nextTick(function () {
  19.             recordedError.should.be(error);
  20.             next();
  21.         });
  22.     });
  23. });
  24.        
  25. C:UsersddenicolaProgramming (Synced)pubit>mocha test/basicTest.js
  26.  
  27.   throwNextTick
  28.     0) works as expected
  29.  
  30.   ? 1 of 1 tests failed:
  31.  
  32.   1) throwNextTick works as expected:
  33.      Error: boo!
  34.       at Test.fn (C:UsersddenicolaProgramming (Synced)pubittestbasicTest.js:11:21)
  35.       at Test.run (C:UsersddenicolaAppDataRoamingnpmnode_modulesmochalibrunnable.js:144:15)
  36.       at Runner.runTest (C:UsersddenicolaAppDataRoamingnpmnode_modulesmochalibrunner.js:271:10)
  37.       at C:UsersddenicolaAppDataRoamingnpmnode_modulesmochalibrunner.js:315:12
  38.       at next (C:UsersddenicolaAppDataRoamingnpmnode_modulesmochalibrunner.js:199:14)
  39.       at C:UsersddenicolaAppDataRoamingnpmnode_modulesmochalibrunner.js:208:7
  40.       at next (C:UsersddenicolaAppDataRoamingnpmnode_modulesmochalibrunner.js:157:23)
  41.       at Array.0 (C:UsersddenicolaAppDataRoamingnpmnode_modulesmochalibrunner.js:176:5)
  42.       at EventEmitter._tickCallback (node.js:192:40)
  43.        
  44. var assert = require('assert')
  45.  
  46. function throwNextTick(error) {
  47.     process.nextTick(function () {
  48.         throw error
  49.     })
  50. }
  51.  
  52.  
  53. describe("throwNextTick", function () {
  54.     it("works as expected", function (next) {
  55.         var error = new Error("boo!")
  56.         var recordedError = null
  57.         var originalException = process.listeners('uncaughtException').pop()
  58.         process.once("uncaughtException", function (error) {
  59.             recordedError = error
  60.         })
  61.         throwNextTick(error);
  62.         process.nextTick(function () {
  63.             process.listeners('uncaughtException').push(originalException)
  64.             assert.equal(recordedError, error)
  65.             next()
  66.         })
  67.     })
  68. })