Skip to content

Commit

Permalink
Test: Adopt assert.closeTo() in test/logs.js for improved debugging
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Krinkle committed Jul 21, 2024
1 parent 5f5af57 commit 0e13d65
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 84 deletions.
4 changes: 4 additions & 0 deletions test/logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<title>logs</title>
<link rel="stylesheet" href="../src/core/qunit.css">
<script src="../qunit/qunit.js"></script>
<script>
QUnit.config.reorder = false;
QUnit.config.storage = undefined;
</script>
<script src="logs.js"></script>
</head>
<body>
Expand Down
132 changes: 48 additions & 84 deletions test/logs.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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',
Expand All @@ -114,7 +111,6 @@ QUnit.test(module1Test1.name, function (assert) {
while (beginModules.length > 2) {
beginModules.pop();
}

assert.propContains(
beginModules,
[module1begin, module2begin],
Expand All @@ -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
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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, {
Expand All @@ -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;
Expand Down

0 comments on commit 0e13d65

Please sign in to comment.