From f3667c188fc930ada88bbba0b208c783f191031c Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Tue, 3 Sep 2024 11:32:37 +0200 Subject: [PATCH] Inspect the precious counterexamples with infinite depth so the details don't get dotted out --- lib/unexpected-check.js | 18 ++++++++++-- test/unexpected-check.spec.js | 52 ++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/lib/unexpected-check.js b/lib/unexpected-check.js index 9c6bfe5..1b23268 100644 --- a/lib/unexpected-check.js +++ b/lib/unexpected-check.js @@ -135,7 +135,7 @@ output .jsFunctionName('fuzz') .text('(') - .appendItems(value.args, ', ') + .appendItemsWithInfiniteDepth(value.args, ', ') .text(', ') .appendInspected(value.mutatorFunction) .text(')'); @@ -158,6 +158,20 @@ }, }); + expect.addStyle( + 'appendItemsWithInfiniteDepth', + function (items, separator) { + const that = this; + separator = separator || ''; + items.forEach((item, index) => { + if (index > 0) { + that.append(separator); + } + that.appendInspected(item, Infinity); + }); + } + ); + const promiseLoop = function (condition, action) { return expect.promise(function (resolve, reject) { const loop = function () { @@ -386,7 +400,7 @@ output.i().block(function (output) { output .text('Generated input: ') - .appendItems(bestFailure.args, ', ') + .appendItemsWithInfiniteDepth(bestFailure.args, ', ') .nl() .text('with: ') .appendItems(options.generators, ', ') diff --git a/test/unexpected-check.spec.js b/test/unexpected-check.spec.js index ab43599..897071a 100644 --- a/test/unexpected-check.spec.js +++ b/test/unexpected-check.spec.js @@ -1,5 +1,5 @@ /* global describe, it, beforeEach */ -const { array, integer, string } = require('chance-generators'); +const { array, integer, string, shape } = require('chance-generators'); const assert = require('assert'); const expect = require('unexpected').clone(); expect.output.preferredWidth = 80; @@ -62,6 +62,56 @@ describe('unexpected-check', function () { ); }); + it('does not dot out the failing example even when it is highly nested', function () { + expect( + function () { + expect( + function (input) { + const sorted = sort(input.numbers); + + expect(sorted, 'to have length', input.numbers.length).and( + 'to be sorted' + ); + }, + 'to be valid for all', + { + generators: [ + shape({ + numbers: numbers, + a: { + bunch: { + of: { + deeply: { + nested: { but: { highly: { relevant: 'stuff' } } }, + }, + }, + }, + }, + }), + ], + maxIterations: 50, + maxErrorIterations: 200, + maxErrors: 30, + } + ); + }, + 'to throw', + 'Found an error after 1 iteration, 20 additional errors found.\n' + + 'counterexample:\n' + + '\n' + + ' Generated input: {\n' + + ' numbers: [ -2, -1 ],\n' + + " a: { bunch: { of: { deeply: { nested: { but: { highly: { relevant: 'stuff' } } } } } } }\n" + + ' }\n' + + ' with: shape({\n' + + ' numbers: array({ itemGenerator: integer({ min: -20, max: 20 }), min: 1, max: 20 }),\n' + + ' a: { bunch: { of: ... } }\n' + + ' })\n' + + '\n' + + ' expected [ -1, -2 ] to be sorted' + ); + }); + describe('when an assertion error is caught', () => { it('should not report a full stack trace', () => { expect(