From 39909fb1ab61404dd4268e42b7a42aae5e08c146 Mon Sep 17 00:00:00 2001 From: Colton Ehrman Date: Mon, 19 Aug 2024 18:53:37 -0500 Subject: [PATCH] test: update tests to be less brittle --- test/test.js | 151 ++++++++++++++++++++------------------------------- 1 file changed, 59 insertions(+), 92 deletions(-) diff --git a/test/test.js b/test/test.js index a888b66..844347b 100644 --- a/test/test.js +++ b/test/test.js @@ -582,113 +582,80 @@ describe('finalhandler(req, res)', function () { it('should return Error message with 1 level cause trace', function (done) { var err = new Error('foo', { cause: new Error('bar') }) - var expectedError = `Error: foo - at Context.<anonymous> (/finalhandler/test/test.js:583:17) - at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) - ... 7 lines matching cause stack trace ... - at process.processImmediate (node:internal/timers:483:21) { - [cause]: Error: bar - at Context.<anonymous> (/finalhandler/test/test.js:583:43) - at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) - at Runnable.run (/finalhandler/node_modules/mocha/lib/runnable.js:338:7) - at Runner.runTest (/finalhandler/node_modules/mocha/lib/runner.js:666:10) - at /finalhandler/node_modules/mocha/lib/runner.js:789:12 - at next (/finalhandler/node_modules/mocha/lib/runner.js:581:14) - at /finalhandler/node_modules/mocha/lib/runner.js:591:7 - at next (/finalhandler/node_modules/mocha/lib/runner.js:474:14) - at Immediate.<anonymous> (/finalhandler/node_modules/mocha/lib/runner.js:559:5) - at process.processImmediate (node:internal/timers:483:21) -}`; - - // Transform the error text into a formatted error response - finalhandler adds
for \n to make it HTML readable - var expectedTransformedError = expectedError.replaceAll(/\n/g, '
') - - var expectedHTMLText = ` - - - -Error - - -
${expectedTransformedError}
- - -` + /* + Example error response + + Error: foo + at Context.<anonymous> (/finalhandler/test/test.js:583:17) + at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) + ... 7 lines matching cause stack trace ... + at process.processImmediate (node:internal/timers:483:21) { + [cause]: Error: bar + at Context.<anonymous> (/finalhandler/test/test.js:583:43) + at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) + at Runnable.run (/finalhandler/node_modules/mocha/lib/runnable.js:338:7) + at Runner.runTest (/finalhandler/node_modules/mocha/lib/runner.js:666:10) + at /finalhandler/node_modules/mocha/lib/runner.js:789:12 + at next (/finalhandler/node_modules/mocha/lib/runner.js:581:14) + at /finalhandler/node_modules/mocha/lib/runner.js:591:7 + at next (/finalhandler/node_modules/mocha/lib/runner.js:474:14) + at Immediate.<anonymous> (/finalhandler/node_modules/mocha/lib/runner.js:559:5) + at process.processImmediate (node:internal/timers:483:21) + } + */ + var expectedRegex = [/Error: foo/, /\[cause\]: Error: bar/]; request(createServer(err)) .get('/') .then((response) => { - // Regular expression to match the root part of any UNIX-style file path - // The pathRegex is meant to find/replace the `/Users/.../finalhandler` parts of the expectedError - // The pathRegex is also intended to work when ran on other computers and from different root paths. - // Warning: this may or may not work for your local environment - var pathRegex = /\/Users(?:\/[^/]+){3}/g; - - var actualTransformedHTMLText = response.text - .replaceAll(pathRegex, '') - .replaceAll(' ', '') - - assert.equal(actualTransformedHTMLText, expectedHTMLText) + for (var i = 0; i < expectedRegex.length; ++i) { + var regex = expectedRegex[i]; + assert.match(response.text, regex); + } done() }) .catch(done) }) - it('should return Error message with 2 level cause trace', function (done) { + it.only('should return Error message with 2 level cause trace', function (done) { var err = new Error('foo', { cause: new Error('bar', { cause: new Error('baz') }) }) - var expectedError = `Error: foo - at Context.<anonymous> (/finalhandler/test/test.js:638:17) - at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) - ... 7 lines matching cause stack trace ... - at process.processImmediate (node:internal/timers:483:21) { - [cause]: Error: bar - at Context.<anonymous> (/finalhandler/test/test.js:638:43) - at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) - ... 7 lines matching cause stack trace ... - at process.processImmediate (node:internal/timers:483:21) { - [cause]: Error: baz - at Context.<anonymous> (/finalhandler/test/test.js:638:69) - at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) - at Runnable.run (/finalhandler/node_modules/mocha/lib/runnable.js:338:7) - at Runner.runTest (/finalhandler/node_modules/mocha/lib/runner.js:666:10) - at /finalhandler/node_modules/mocha/lib/runner.js:789:12 - at next (/finalhandler/node_modules/mocha/lib/runner.js:581:14) - at /finalhandler/node_modules/mocha/lib/runner.js:591:7 - at next (/finalhandler/node_modules/mocha/lib/runner.js:474:14) - at Immediate.<anonymous> (/finalhandler/node_modules/mocha/lib/runner.js:559:5) - at process.processImmediate (node:internal/timers:483:21) - } -}`; - - // Transform the error text into a formatted error response - finalhandler adds
for \n to make it HTML readable - var expectedTransformedError = expectedError.replaceAll(/\n/g, '
') - - var expectedHTMLText = ` - - - -Error - - -
${expectedTransformedError}
- - -` + /* + Example error response + + Error: foo + at Context.<anonymous> (/finalhandler/test/test.js:638:17) + at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) + ... 7 lines matching cause stack trace ... + at process.processImmediate (node:internal/timers:483:21) { + [cause]: Error: bar + at Context.<anonymous> (/finalhandler/test/test.js:638:43) + at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) + ... 7 lines matching cause stack trace ... + at process.processImmediate (node:internal/timers:483:21) { + [cause]: Error: baz + at Context.<anonymous> (/finalhandler/test/test.js:638:69) + at callFnAsync (/finalhandler/node_modules/mocha/lib/runnable.js:394:21) + at Runnable.run (/finalhandler/node_modules/mocha/lib/runnable.js:338:7) + at Runner.runTest (/finalhandler/node_modules/mocha/lib/runner.js:666:10) + at /finalhandler/node_modules/mocha/lib/runner.js:789:12 + at next (/finalhandler/node_modules/mocha/lib/runner.js:581:14) + at /finalhandler/node_modules/mocha/lib/runner.js:591:7 + at next (/finalhandler/node_modules/mocha/lib/runner.js:474:14) + at Immediate.<anonymous> (/finalhandler/node_modules/mocha/lib/runner.js:559:5) + at process.processImmediate (node:internal/timers:483:21) + } + } + */ + var expectedRegex = [/Error: foo/, /\[cause\]: Error: bar/, /\[cause\]: Error: baz/]; request(createServer(err)) .get('/') .then((response) => { - // Regular expression to match the root part of any UNIX-style file path - // The pathRegex is meant to find/replace the `/Users/.../finalhandler` parts of the expectedError - // The pathRegex is also intended to work when ran on other computers and from different root paths. - // Warning: this may or may not work for your local environment - var pathRegex = /\/Users(?:\/[^/]+){3}/g; - var actualTransformedHTMLText = response.text - .replaceAll(pathRegex, '') - .replaceAll(' ', '') - - assert.equal(actualTransformedHTMLText, expectedHTMLText) + for (var i = 0; i < expectedRegex.length; ++i) { + var regex = expectedRegex[i]; + assert.match(response.text, regex); + } done() }) .catch(done)