Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You ask:</p> <blockquote> <p>However, I notice that node.js/express.js/supertest automatically convert uncaught exceptions to an error message in the response. Perhaps that is what is stopping them from being caught by the mocha test?</p> </blockquote> <p>Yes, that's pretty much how HTTP servers work by default: exceptions that do not have any special meaning for the server software are transformed into status 500 responses. So if you want to test that the server has thrown an exception, you can record this fact yourself and use it later. For instance:</p> <pre><code>var http = require("http"), express = require("express"), assert = require('assert'); var app = express(); function handler (req, res) { throw new assert.AssertionError({ message: "Can I throw an assertion error? "} ); res.end('HEY'); } describe('Really crazy test of assertions', function () { it("cannot create a server which throws an assertion error", function (done) { var port = Math.floor(Math.random() * 9999) + 1; var caught; app.get('/', function catcher() { try { handler.apply(this, arguments); } catch (ex) { caught = ex; // Capture it ... throw ex; // ... and rethrow it. } }).listen(port); setTimeout(function () { http.get('http://localhost:' + port + '/', function (res) { // Act on the caught exception, if it exists. // This could be assert.equal or whatever. if (caught) throw caught; done(); }); }, 1000); }); }); </code></pre> <p>This is meant to illustrate the principle. The <code>catcher</code> function could be designed to be generic enough to work for a whole bunch of different tests.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload