Skip to content

Commit

Permalink
Core: Deprecate QUnit.load() and document migration path
Browse files Browse the repository at this point in the history
Based on qunitjs#1502 by @smcclure15.
Closes qunitjs#1084.

Co-authored-by: Steve <[email protected]>
  • Loading branch information
Krinkle and smcclure15 committed Oct 16, 2023
1 parent 552ad59 commit 93a63b5
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 12 deletions.
6 changes: 6 additions & 0 deletions build/prep-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function versionAddedString (version) {
return `version_added: "${version}"`;
}

function versionDeprecatedString (version) {
return `version_deprecated: "${version}"`;
}

function formatChangelogColumn (version) {
return `| [QUnit ${version}](https://github.com/qunitjs/qunit/releases/tag/${version}) |`;
}
Expand All @@ -30,6 +34,7 @@ const Repo = {
}
{
const UNRELEASED_ADDED = versionAddedString('unreleased');
const UNRELEASED_DEP = versionDeprecatedString('unreleased');
const UNRELEASED_CHANGELOG = '| UNRELEASED |';

// Silence error from grep, which exits non-zero if no results.
Expand All @@ -42,6 +47,7 @@ const Repo = {
fs.writeFileSync(filePath,
doc
.replace(UNRELEASED_ADDED, versionAddedString(version))
.replace(UNRELEASED_DEP, versionDeprecatedString(version))
.replace(UNRELEASED_CHANGELOG, formatChangelogColumn(version))
);
});
Expand Down
2 changes: 1 addition & 1 deletion docs/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source "https://rubygems.org"
ruby RUBY_VERSION

# To apply changes, run `bundle update`.
gem "jekyll-theme-amethyst", "0.5.6", group: :jekyll_plugins
gem "jekyll-theme-amethyst", "1.0.0", group: :jekyll_plugins
44 changes: 44 additions & 0 deletions docs/QUnit/load.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
layout: page-api
title: QUnit.load()
excerpt: Notify the test runner to consider starting test execution.
groups:
- deprecated
redirect_from:
- "/start/"
version_added: "1.0.0"
version_deprecated: "unreleased"
---

`QUnit.load()`

Notify the test runner to try to start test execution.

This is an internal method that previously facilitated [`QUnit.config.autostart`](../config/autostart.md) in a web browser context. When QUnit is already started, or if `QUnit.config.autostart` is false, then calls to `QUnit.load()` are silently ignored.

<p class="note note--warning" markdown="1">This method is __deprecated__. Remove call, or replace by a single call to [`QUnit.start()`](./start.md).</p>

## Changelog

| UNRELEASED | Deprecated. Use [`QUnit.start()`](./start.md) instead.
| [QUnit 2.1.1](https://github.com/qunitjs/qunit/releases/tag/2.1.1) | `QUnit.start()` no longer requires calling `QUnit.load()` first.

## Migration examples

If you still call `QUnit.load()` with QUnit 2.2 or later, the call is usually redundant and safe to remove (including before you upgrade to QUnit 3).

## Calling both `QUnit.load()` and `QUnit.start()`

If your project started with QUnit 1.x, and you use `QUnit.config.autostart = false`, then you might be calling both of these methods. [`QUnit.start()`](./start.md) previously required calling `QUnit.load()`.

This has not been needed since [QUnit 2.1.1](https://github.com/qunitjs/qunit/releases/tag/2.1.1), and the call to `QUnit.load()` is safe to remove.

## Calling only `QUnit.load()`

Prior to QUnit 2.21, the built-in HTML Reporter called `QUnit.load()` from the window.onload event, which in turn gracefully calls `QUnit.start()` if it has not been called already.

If your test runner works in a similar way, call [`QUnit.start()`](./start.md) instead of `QUnit.load()`. This will solve the deprecation warning and prepares you for QUnit 3.

## Calling neither

Calling neither requires no changes. Refer to [`QUnit.start()`](./start.md) if you're curious how this is automatically called for you.
14 changes: 11 additions & 3 deletions docs/QUnit/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ version_added: "1.0.0"

`QUnit.start()`

Start the test runner manually, when [`QUnit.config.autostart`](../config/autostart.md) is `false`. For example, if you load test files with AMD, RequireJS, or ESM dynamic imports.
Call this method to start the test runner. This indicates that all relevant source code has been loaded and all tests have been defined.

Note: See [`QUnit.config.autostart`](../config/autostart.md) for detailed examples of how to use this.
In most environments this is **automatically called** and you do not need to call it. This includes use in web browsers and via the QUnit CLI.

<p class="note note--warning" markdown="1">**Warning**: Prior to QUnit 1.16, this method was used for resuming an async `QUnit.start` function, as complement to `QUnit.stop()`. To resume asynchronous tests, use [`assert.async()`](../assert/async.md) instead.</p>
If you build a custom test runner (such in SpiderMonkey or Node.js), or if you disable `QUnit.config.autostart` and load test files asynchronously (with AMD, RequireJS, or ESM dynamic imports), then you need to call this once after your test files have been loaded. See [`QUnit.config.autostart`](../config/autostart.md) for detailed examples of how to use this.

<p class="note note--warning" markdown="1">Prior to QUnit 1.16, this method was used for resuming an async `QUnit.test()` function, as complement to `QUnit.stop()`. To resume asynchronous tests, use [`assert.async()`](../assert/async.md) instead.</p>

## Changelog

| [QUnit 2.1.1](https://github.com/qunitjs/qunit/releases/tag/2.1.1) | `QUnit.start()` no longer requires calling [`QUnit.load()`](./load.md) first.
| [QUnit 2.0](https://github.com/qunitjs/qunit/releases/tag/2.0.0) | Support for calling `start()` to resume an async test was removed. ([Migration guide](https://qunitjs.com/upgrade-guide-2.x/#introducing-assertasync))
| [QUnit 1.16](https://github.com/qunitjs/qunit/releases/tag/1.16.0) | Use of `start()` to resume an async test was deprecated in favour of [`assert.async()`](../assert/async.md).
2 changes: 1 addition & 1 deletion docs/assert/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Instruct QUnit to wait for an asynchronous operation.

`assert.async()` returns a callback function and pauses test processing until the callback function is called. The callback will throw an `Error` if it is invoked more often than the required call count.

This replaces functionality previously provided by `QUnit.stop()` and [`QUnit.start()`](../QUnit/start.md).
This replaces functionality that QUnit 1.x provided via `QUnit.stop()` and [`QUnit.start()`](../QUnit/start.md).

## Examples

Expand Down
27 changes: 22 additions & 5 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,21 @@ extend(QUnit, {
'QUnit.config.autostart was true');
}

// Until we remove QUnit.load() in QUnit 3, we keep `pageLoaded`.
// It no longer serves any purpose other than to support old test runners
// that still call only QUnit.load(), or that call both it and QUnit.start().
if (!config.pageLoaded) {
// The page isn't completely loaded yet, so we set autostart and then
// load if we're in Node or wait for the browser's load event.
// If the test runner used `autostart = false` and is calling QUnit.start()
// to tell is their resources are ready, but the browser isn't ready yet,
// then enable autostart now, and we'll let the tests really start after
// the browser's "load" event handler calls autostart().
config.autostart = true;

// Starts from Node even if .load was not previously called. We still return
// early otherwise we'll wind up "beginning" twice.
// If we're in Node or another non-browser environment, we start now as there
// won't be any "load" event. We return early either way since autostart
// is responsible for calling scheduleBegin (avoid "beginning" twice).
if (!document) {
QUnit.load();
QUnit.autostart();
}

return;
Expand All @@ -115,9 +121,20 @@ extend(QUnit, {
},

load: function () {
Logger.warn('QUnit.load is deprecated and will be removed in QUnit 3.0.' +
' Refer to <https://api.qunitjs.com/QUnit/load/>.');

QUnit.autostart();
},

/**
* @internal
*/
autostart: function () {
config.pageLoaded = true;

// Initialize the configuration options
// TODO: Move this to config.js in QUnit 3.
extend(config, {
started: 0,
updateRate: 1000,
Expand Down
4 changes: 2 additions & 2 deletions src/html-reporter/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,9 +1079,9 @@ const stats = {
}

if (!usingPhantom && document.readyState === 'complete') {
QUnit.load();
QUnit.autostart();
} else {
addEvent(window, 'load', QUnit.load);
addEvent(window, 'load', QUnit.autostart);
}

// Wrap window.onerror. We will call the original window.onerror to see if
Expand Down

0 comments on commit 93a63b5

Please sign in to comment.