Skip to content

Commit

Permalink
test: update tests to be less brittle
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonehrman committed Aug 19, 2024
1 parent 0812d25 commit 39909fb
Showing 1 changed file with 59 additions and 92 deletions.
151 changes: 59 additions & 92 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:583:17)
at callFnAsync (<root>/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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:583:43)
at callFnAsync (<root>/finalhandler/node_modules/mocha/lib/runnable.js:394:21)
at Runnable.run (<root>/finalhandler/node_modules/mocha/lib/runnable.js:338:7)
at Runner.runTest (<root>/finalhandler/node_modules/mocha/lib/runner.js:666:10)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:789:12
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:581:14)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:591:7
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:474:14)
at Immediate.&lt;anonymous&gt; (<root>/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 <br> for \n to make it HTML readable
var expectedTransformedError = expectedError.replaceAll(/\n/g, '<br>')

var expectedHTMLText = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>${expectedTransformedError}</pre>
</body>
</html>
`
/*
Example error response
Error: foo
at Context.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:583:17)
at callFnAsync (<root>/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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:583:43)
at callFnAsync (<root>/finalhandler/node_modules/mocha/lib/runnable.js:394:21)
at Runnable.run (<root>/finalhandler/node_modules/mocha/lib/runnable.js:338:7)
at Runner.runTest (<root>/finalhandler/node_modules/mocha/lib/runner.js:666:10)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:789:12
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:581:14)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:591:7
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:474:14)
at Immediate.&lt;anonymous&gt; (<root>/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, '<root>')
.replaceAll('&nbsp;', '')

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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:638:17)
at callFnAsync (<root>/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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:638:43)
at callFnAsync (<root>/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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:638:69)
at callFnAsync (<root>/finalhandler/node_modules/mocha/lib/runnable.js:394:21)
at Runnable.run (<root>/finalhandler/node_modules/mocha/lib/runnable.js:338:7)
at Runner.runTest (<root>/finalhandler/node_modules/mocha/lib/runner.js:666:10)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:789:12
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:581:14)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:591:7
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:474:14)
at Immediate.&lt;anonymous&gt; (<root>/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 <br> for \n to make it HTML readable
var expectedTransformedError = expectedError.replaceAll(/\n/g, '<br>')

var expectedHTMLText = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>${expectedTransformedError}</pre>
</body>
</html>
`
/*
Example error response
Error: foo
at Context.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:638:17)
at callFnAsync (<root>/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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:638:43)
at callFnAsync (<root>/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.&lt;anonymous&gt; (<root>/finalhandler/test/test.js:638:69)
at callFnAsync (<root>/finalhandler/node_modules/mocha/lib/runnable.js:394:21)
at Runnable.run (<root>/finalhandler/node_modules/mocha/lib/runnable.js:338:7)
at Runner.runTest (<root>/finalhandler/node_modules/mocha/lib/runner.js:666:10)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:789:12
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:581:14)
at <root>/finalhandler/node_modules/mocha/lib/runner.js:591:7
at next (<root>/finalhandler/node_modules/mocha/lib/runner.js:474:14)
at Immediate.&lt;anonymous&gt; (<root>/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, '<root>')
.replaceAll('&nbsp;', '')

assert.equal(actualTransformedHTMLText, expectedHTMLText)
for (var i = 0; i < expectedRegex.length; ++i) {
var regex = expectedRegex[i];
assert.match(response.text, regex);
}
done()
})
.catch(done)
Expand Down

0 comments on commit 39909fb

Please sign in to comment.