From 0e13d65b05911c9d0e0f5cfc79ab4d23d3697123 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sun, 21 Jul 2024 18:09:39 +0100 Subject: [PATCH] Test: Adopt assert.closeTo() in test/logs.js for improved debugging Also disable config.reorder and config.storage, since this suite relies on exact order, and fresh state since the `previousFailure` property is also being observed upon itself, which would lead to a state where it can never pass no many how many times you reload. --- test/logs.html | 4 ++ test/logs.js | 132 ++++++++++++++++++------------------------------- 2 files changed, 52 insertions(+), 84 deletions(-) diff --git a/test/logs.html b/test/logs.html index 0540cbe27..d3782438b 100644 --- a/test/logs.html +++ b/test/logs.html @@ -5,6 +5,10 @@ logs + diff --git a/test/logs.js b/test/logs.js index cceff26f2..fbea7dfc3 100644 --- a/test/logs.js +++ b/test/logs.js @@ -1,6 +1,6 @@ QUnit.config.reorder = false; -var totalTests; var moduleContext; var moduleDoneContext; var testContext; var testDoneContext; var logContext; +var totalTests; var moduleContext; var moduleDoneContext; var testContext; var testDoneContext; var beginModules; var module1Test1; var module1Test2; var module2Test1; var module2Test2; var module2Test3; var module2Test4; var module2Test5; var module2Test6; @@ -95,16 +95,13 @@ QUnit.testDone(function (context) { testDoneContext = context; }); -QUnit.log(function (context) { +QUnit.log(function () { log++; - logContext = context; }); QUnit.module(module1Context.name); QUnit.test(module1Test1.name, function (assert) { - assert.expect(18); - assert.equal( typeof totalTests, 'number', @@ -114,7 +111,6 @@ QUnit.test(module1Test1.name, function (assert) { while (beginModules.length > 2) { beginModules.pop(); } - assert.propContains( beginModules, [module1begin, module2begin], @@ -127,101 +123,79 @@ QUnit.test(module1Test1.name, function (assert) { assert.equal(testDone, 0, 'QUnit.testDone calls'); assert.equal(moduleDone, 0, 'QUnit.moduleDone calls'); - assert.equal( - logContext.runtime >= 0 && logContext.runtime < 50, - true, - 'log runtime was a reasonable number' - ); - - delete logContext.runtime; - assert.deepEqual(logContext, { - name: module1Test1.name, + assert.strictEqual(testDoneContext, undefined, 'testDone context'); + assert.deepEqual(testContext, { module: module1Context.name, - result: true, - message: 'log runtime was a reasonable number', - actual: true, - expected: true, - negative: false, + name: module1Test1.name, testId: module1Test1.testId, - todo: false - }, 'log context after equal(actual, expected, message)'); + previousFailure: false + }, 'test context'); - assert.equal('foo', 'foo'); + assert.strictEqual(moduleDoneContext, undefined, 'moduleDone context'); + assert.deepEqual(moduleContext, module1Context, 'module context'); - delete logContext.runtime; - assert.deepEqual(logContext, { + var logContext; + QUnit.log(function (details) { + if (details.message === 'example message') { + logContext = details; + } + }); + assert.equal('foo', 'foo', 'example message'); + + assert.closeTo(logContext.runtime, 25, 25, 'assertion runtime 0-50ms'); + assert.propContains(logContext, { name: module1Test1.name, module: module1Context.name, result: true, - message: undefined, + message: 'example message', actual: 'foo', expected: 'foo', negative: false, testId: module1Test1.testId, todo: false - }, 'log context after equal(actual, expected)'); - - assert.ok(true, 'ok(true, message)'); - - delete logContext.runtime; - assert.deepEqual(logContext, { - module: module1Context.name, + }, 'log event after equal(actual, expected, message)'); + + QUnit.log(function (details) { + if (details.actual === 'bar') { + logContext = details; + } + }); + assert.equal('bar', 'bar'); + assert.propContains(logContext, { name: module1Test1.name, + module: module1Context.name, result: true, - message: 'ok(true, message)', - actual: true, - expected: true, + message: undefined, + actual: 'bar', + expected: 'bar', negative: false, testId: module1Test1.testId, todo: false - }, 'log context after ok(true, message)'); + }, 'log event after equal(actual, expected)'); - assert.strictEqual(testDoneContext, undefined, 'testDone context'); - assert.deepEqual(testContext, { - module: module1Context.name, - name: module1Test1.name, - testId: module1Test1.testId, - previousFailure: false - }, 'test context'); - - assert.strictEqual(moduleDoneContext, undefined, 'moduleDone context'); - assert.deepEqual(moduleContext, module1Context, 'module context'); - - assert.equal(log, 17, 'QUnit.log calls'); + assert.equal(log, 16, 'QUnit.log calls'); }); QUnit.test(module1Test2.name, function (assert) { - assert.expect(12); assert.equal(begin, 1, 'QUnit.begin calls'); assert.equal(moduleStart, 1, 'QUnit.moduleStart calls'); assert.equal(testStart, 2, 'QUnit.testStart calls'); assert.equal(testDone, 1, 'QUnit.testDone calls'); assert.equal(moduleDone, 0, 'QUnit.moduleDone calls'); - assert.equal( - testDoneContext.runtime >= 0 && testDoneContext.runtime < 1000, - true, - 'test runtime was a reasonable number' - ); - + assert.closeTo(testDoneContext.runtime, 500, 500, 'test runtime 0-1000ms'); assert.true(testDoneContext.assertions instanceof Array, 'testDone context: assertions'); - // TODO: more tests for testDoneContext.assertions - delete testDoneContext.runtime; - - // Delete testDoneContext.assertions so we can easily jump to next assertion - delete testDoneContext.assertions; - - // Delete testDoneContext.source delete testDoneContext.source; - + // TODO: more tests for testDoneContext.assertions + delete testDoneContext.assertions; assert.deepEqual(testDoneContext, { module: module1Context.name, name: module1Test1.name, failed: 0, - passed: 18, - total: 18, + passed: 17, + total: 17, testId: module1Test1.testId, skipped: false, todo: false @@ -232,16 +206,15 @@ QUnit.test(module1Test2.name, function (assert) { testId: module1Test2.testId, previousFailure: false }, 'test context'); - assert.strictEqual(moduleDoneContext, undefined, 'moduleDone context'); assert.deepEqual(moduleContext, module1Context, 'module context'); - assert.equal(log, 29, 'QUnit.log calls'); + + assert.equal(log, 28, 'QUnit.log calls'); }); QUnit.module(module2Context.name); QUnit.test(module2Test1.name, function (assert) { - assert.expect(10); assert.equal(begin, 1, 'QUnit.begin calls'); assert.equal(moduleStart, 2, 'QUnit.moduleStart calls'); assert.equal(testStart, 3, 'QUnit.testStart calls'); @@ -255,27 +228,22 @@ QUnit.test(module2Test1.name, function (assert) { previousFailure: false }, 'test context'); - assert.equal( - moduleDoneContext.runtime >= 0 && moduleDoneContext.runtime < 5000, - true, - 'module runtime was a reasonable number' - ); - delete moduleDoneContext.runtime; + assert.closeTo(moduleDoneContext.runtime, 2500, 2500, 'module runtime 0-5000ms'); + delete moduleDoneContext.runtime; assert.deepEqual(moduleDoneContext, { name: module1Context.name, tests: module1Context.tests, failed: 0, - passed: 30, - total: 30 + passed: 29, + total: 29 }, 'moduleDone context'); assert.deepEqual(moduleContext, module2Context, 'module context'); - assert.equal(log, 39, 'QUnit.log calls'); + assert.equal(log, 38, 'QUnit.log calls'); }); QUnit.test(module2Test2.name, function (assert) { - assert.expect(8); assert.equal(begin, 1, 'QUnit.begin calls'); assert.equal(moduleStart, 2, 'QUnit.moduleStart calls'); assert.equal(testStart, 4, 'QUnit.testStart calls'); @@ -290,14 +258,12 @@ QUnit.test(module2Test2.name, function (assert) { }, 'test context'); assert.deepEqual(moduleContext, module2Context, 'module context'); - assert.equal(log, 47, 'QUnit.log calls'); + assert.equal(log, 46, 'QUnit.log calls'); }); QUnit.skip(module2Test3.name); QUnit.test(module2Test4.name, function (assert) { - assert.expect(1); - delete testDoneContext.source; assert.deepEqual(testDoneContext, { @@ -320,8 +286,6 @@ QUnit.todo(module2Test5.name, function (assert) { }); QUnit.test(module2Test6.name, function (assert) { - assert.expect(1); - delete testDoneContext.runtime; delete testDoneContext.duration; delete testDoneContext.source;