Skip to content

Commit

Permalink
Test: Convert es2018/throws.js to main, fix Chrome-specific expect
Browse files Browse the repository at this point in the history
* Improve "failing assertions" to check the failure.
  This is based on the "inspect expected values" variant, but now
  applied to both cases.

* Improve "does not die" test to not hardcode the exact message
  that Chrome throws. Make it pass in Firefox and Safari too, which
  use slightly different error messages.
  • Loading branch information
Krinkle committed Jul 8, 2024
1 parent 99aee51 commit bf1406e
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 90 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@
"compat/compat": "off"
}
},
{
"files": ["test/main/*-es6.js"],
"env": {
"es6": true
},
"parserOptions": {
"ecmaVersion": 6
},
"rules": {
"compat/compat": "off"
}
},
{
"files": ["test/benchmark/**", "test/es2018/**"],
"env": {
Expand Down
5 changes: 3 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = function (grunt) {
'test/events-filters.html',
'test/events-in-test.html',
'test/headless.html',
'test/index-es5.html',
'test/logs.html',
'test/module-skip.html',
'test/module-todo.html',
Expand Down Expand Up @@ -117,6 +118,7 @@ module.exports = function (grunt) {
// Sync with test/index.html
'test/main/assert.js',
'test/main/assert-step.js',
'test/main/assert-throws-es6.js',
'test/main/assert-timeout.js',
'test/main/async.js',
'test/main/deepEqual.js',
Expand All @@ -142,8 +144,7 @@ module.exports = function (grunt) {
'test/node/storage-2.js',

'test/es2018/async-functions.js',
'test/es2018/rejects.js',
'test/es2018/throws.js'
'test/es2018/rejects.js'
]
}
});
Expand Down
2 changes: 1 addition & 1 deletion build/browserstack-debug.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"test_framework": "qunit",
"test_path": ["test/index.html"],
"test_path": ["test/index-es5.html"],
"test_server_port": "8899",
"exit_with_fail": true,
"timeout": 600,
Expand Down
2 changes: 1 addition & 1 deletion build/browserstack-full.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"test_framework": "qunit",
"test_path": ["test/index.html"],
"test_path": ["test/index-es5.html"],
"test_server_port": "8899",
"exit_with_fail": true,
"timeout": 600,
Expand Down
2 changes: 1 addition & 1 deletion build/browserstack-quick.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"test_framework": "qunit",
"test_path": ["test/index.html"],
"test_path": ["test/index-es5.html"],
"test_server_port": "8899",
"exit_with_fail": true,
"timeout": 600,
Expand Down
85 changes: 0 additions & 85 deletions test/es2018/throws.js

This file was deleted.

36 changes: 36 additions & 0 deletions test/index-es5.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QUnit Main Test Suite</title>
<link rel="stylesheet" href="../src/qunit.css">
<script src="../qunit/qunit.js"></script>
<script>
// For main/module.js
QUnit.config.reorder = false;
</script>

<script src="main/assert.js"></script>
<script src="main/assert-step.js"></script>
<script src="main/assert-timeout.js"></script>
<script src="main/async.js"></script>
<script src="main/deepEqual.js"></script>
<script src="main/diff.js"></script>
<script src="main/dump.js"></script>
<script src="main/each.js"></script>
<script src="main/HtmlReporter.js"></script>
<script src="main/modules.js"></script>
<script src="main/onUncaughtException.js"></script>
<script src="main/promise.js"></script>
<script src="main/setTimeout.js"></script>
<script src="main/stacktrace.js"></script>
<script src="main/test.js"></script>
<script src="main/utilities.js"></script>

<script src="reporter-html/unhandled-rejection.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
</body>
</html>
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<script src="main/assert.js"></script>
<script src="main/assert-step.js"></script>
<script src="main/assert-throws-es6.js"></script>
<script src="main/assert-timeout.js"></script>
<script src="main/async.js"></script>
<script src="main/deepEqual.js"></script>
Expand Down
88 changes: 88 additions & 0 deletions test/main/assert-throws-es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
QUnit.module('assert.throws [es6]', function () {
function CustomError (message) {
this.message = message;
}

CustomError.prototype.toString = function () {
return this.message;
};

QUnit.test('throws [matcher arrow function]', function (assert) {
assert.throws(
function () {
throw new CustomError('some error description');
},
err => {
return err instanceof CustomError && /description/.test(err);
}
);
});

QUnit.test('throws [matcher Error subclass]', function (assert) {
class CustomError extends Error {}

assert.throws(
function () {
throw new CustomError('foo');
},
CustomError
);
});

// TODO: Once we run browser tests with QTap, remove this hack
// and instead write expected failures in .tap.txt snapshots.
QUnit.module('failing assertions', function (hooks) {
hooks.beforeEach(function (assert) {
const original = assert.pushResult;
assert.pushResult = (resultInfo) => {
if (this.expectedFailure &&
this.expectedFailure.actual === resultInfo.actual &&
this.expectedFailure.expected.test(resultInfo.expected)
) {
// Restore
assert.pushResult = original;
this.expectedFailure = null;
// Inverts the result of the (one) expected failure
resultInfo.result = !resultInfo.result;
}

return original.call(this, resultInfo);
};
});

QUnit.test('throws [matcher arrow fn returns false]', function (assert) {
this.expectedFailure = {
actual: 'Error: foo',
expected: /^null$/
};

assert.throws(
function () {
throw new Error('foo');
},
() => false
);
});

QUnit.test('throws [graceful failure when class given as matcher]', function (assert) {
// Avoid uncaught "Died on test" and instead report it
// gracefully as an assertion failure
// https://github.com/qunitjs/qunit/issues/1530
this.expectedFailure = {
actual: 'Error: foo',
// [Firefox 127] TypeError: class constructors must be invoked with 'new'
// [Safari 17] TypeError: Cannot call a class constructor without |new|
// [Chrome 126] TypeError: Class constructor CustomError cannot be invoked without 'new'
expected: /^TypeError: .*[Cc]lass constructors? .*\bnew/
};

class CustomError extends Error {}
assert.throws(
() => {
throw new Error('foo');
},
CustomError
);
});
});
});
1 change: 1 addition & 0 deletions test/mozjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ QUnit.on('runEnd', (suiteEnd) => {
// Sync with test/index.html
loadRelativeToScript('../test/main/assert.js');
loadRelativeToScript('../test/main/assert-step.js');
loadRelativeToScript('../test/main/assert-throws-es6.js');
// loadRelativeToScript( "../test/main/assert-timeout.js" ); // Requires setTimeout
// loadRelativeToScript( "../test/main/async.js" ); // Requires setTimeout
loadRelativeToScript('../test/main/deepEqual.js');
Expand Down
1 change: 1 addition & 0 deletions test/webWorker-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ importScripts(
// Sync with test/index.html
'main/assert.js',
'main/assert-step.js',
'main/assert-throws-es6.js',
'main/assert-timeout.js',
'main/async.js',
'main/deepEqual.js',
Expand Down

0 comments on commit bf1406e

Please sign in to comment.