11cb0ef41Sopenharmony_ci# Web Platform Tests
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciThe tests here are drivers for running the [Web Platform Tests][].
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciSee [`test/fixtures/wpt/README.md`][] for a hash of the last
61cb0ef41Sopenharmony_ciupdated WPT commit for each module being covered here.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciSee the json files in [the `status` folder](./status) for prerequisites,
91cb0ef41Sopenharmony_ciexpected failures, and support status for specific tests in each module.
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciCurrently there are still some Web Platform Tests titled `test-whatwg-*`
121cb0ef41Sopenharmony_ciunder `test/parallel` that have not been migrated to be run with the
131cb0ef41Sopenharmony_ciWPT harness and have automatic updates. There are also a few
141cb0ef41Sopenharmony_ci`test-whatwg-*-custom-*` tests that may need to be upstreamed.
151cb0ef41Sopenharmony_ciThis folder covers the tests that have been migrated.
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci<a id="add-tests"></a>
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci## How to add tests for a new module
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci### 1. Create a status file
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciFor example, to add the URL tests, add a `test/wpt/status/url.json` file.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciIn the beginning, it's fine to leave an empty object `{}` in the file if
261cb0ef41Sopenharmony_ciit's not yet clear how compliant the implementation is,
271cb0ef41Sopenharmony_cithe requirements and expected failures can be figured out in a later step
281cb0ef41Sopenharmony_ciwhen the tests are run for the first time.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciSee [Format of a status JSON file](#status-format) for details.
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci### 2. Pull the WPT files
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciUse the [git node wpt][] command to download the WPT files into
351cb0ef41Sopenharmony_ci`test/fixtures/wpt`. For example, to add URL tests:
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci```text
381cb0ef41Sopenharmony_ci$ cd /path/to/node/project
391cb0ef41Sopenharmony_ci$ git node wpt url
401cb0ef41Sopenharmony_ci```
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci### 3. Create the test driver
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciFor example, for the URL tests, add a file `test/wpt/test-url.js`:
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci```js
471cb0ef41Sopenharmony_ci'use strict';
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciconst { WPTRunner } = require('../common/wpt');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciconst runner = new WPTRunner('url');
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// Set Node.js flags required for the tests.
541cb0ef41Sopenharmony_cirunner.setFlags(['--expose-internals']);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci// Set a script that will be executed in the worker before running the tests.
571cb0ef41Sopenharmony_cirunner.setInitScript(`
581cb0ef41Sopenharmony_ci  const { internalBinding } = require('internal/test/binding');
591cb0ef41Sopenharmony_ci  const { DOMException } = internalBinding('messaging');
601cb0ef41Sopenharmony_ci  global.DOMException = DOMException;
611cb0ef41Sopenharmony_ci`);
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_cirunner.runJsTests();
641cb0ef41Sopenharmony_ci```
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciThis driver is capable of running the tests located in `test/fixtures/wpt/url`
671cb0ef41Sopenharmony_ciwith the WPT harness while taking the status file into account.
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci### 4. Run the tests
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciRun the test using `tools/test.py` and see if there are any failures.
721cb0ef41Sopenharmony_ciFor example, to run all the URL tests under `test/fixtures/wpt/url`:
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci```text
751cb0ef41Sopenharmony_ci$ tools/test.py wpt/test-url
761cb0ef41Sopenharmony_ci```
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciTo run a specific test in WPT, for example, `url/url-searchparams.any.js`,
791cb0ef41Sopenharmony_cipass the file name as argument to the corresponding test driver:
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci```text
821cb0ef41Sopenharmony_cinode test/wpt/test-url.js url-searchparams.any.js
831cb0ef41Sopenharmony_ci```
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ciIf there are any failures, update the corresponding status file
861cb0ef41Sopenharmony_ci(in this case, `test/wpt/status/url.json`) to make the test pass.
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciFor example, to mark `url/url-searchparams.any.js` as expected to fail,
891cb0ef41Sopenharmony_ciadd this to `test/wpt/status/url.json`:
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci```json
921cb0ef41Sopenharmony_ci  "url-searchparams.any.js": {
931cb0ef41Sopenharmony_ci    "fail": {
941cb0ef41Sopenharmony_ci      "expected": [
951cb0ef41Sopenharmony_ci        "test name in the WPT test case, e.g. second argument passed to test()"
961cb0ef41Sopenharmony_ci      ]
971cb0ef41Sopenharmony_ci    }
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci```
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciSee [Format of a status JSON file](#status-format) for details.
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci### 5. Commit the changes and submit a Pull Request
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ciSee [the contributing guide](../../CONTRIBUTING.md).
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci## How to update tests for a module
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciThe tests can be updated in a way similar to how they are added.
1101cb0ef41Sopenharmony_ciRun Step 2 and Step 4 of [adding tests for a new module](#add-tests).
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ciThe [git node wpt][] command maintains the status of the local
1131cb0ef41Sopenharmony_ciWPT subset, if no files are updated after running it for a module,
1141cb0ef41Sopenharmony_cithe local subset is up to date and there is no need to update them
1151cb0ef41Sopenharmony_ciuntil they are changed in the upstream.
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci## How it works
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ciNote: currently this test suite only supports `.js` tests. There is
1201cb0ef41Sopenharmony_ciongoing work in the upstream to properly split out the tests into files
1211cb0ef41Sopenharmony_cithat can be run in a shell environment like Node.js.
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci### Getting the original test files and harness from WPT
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciThe original files and harness from WPT are downloaded and stored in
1261cb0ef41Sopenharmony_ci`test/fixtures/wpt`.
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciThe [git node wpt][] command automate this process while maintaining a map
1291cb0ef41Sopenharmony_cicontaining the hash of the last updated commit for each module in
1301cb0ef41Sopenharmony_ci`test/fixtures/wpt/versions.json` and [`test/fixtures/wpt/README.md`][].
1311cb0ef41Sopenharmony_ciIt also maintains the LICENSE file in `test/fixtures/wpt`.
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci### Loading and running the tests
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ciGiven a module, the `WPTRunner` class in [`test/common/wpt`](../common/wpt.js)
1361cb0ef41Sopenharmony_ciloads:
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci* `.js` test files (for example, `test/common/wpt/url/*.js` for `url`)
1391cb0ef41Sopenharmony_ci* Status file (for example, `test/wpt/status/url.json` for `url`)
1401cb0ef41Sopenharmony_ci* The WPT harness
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ciThen, for each test, it creates a worker thread with the globals and mocks,
1431cb0ef41Sopenharmony_cisets up the harness result hooks, loads the metadata in the test (including
1441cb0ef41Sopenharmony_ciloading extra resources), and runs all the tests in that worker thread,
1451cb0ef41Sopenharmony_ciskipping tests that cannot be run because of lack of dependency or
1461cb0ef41Sopenharmony_ciexpected failures.
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci<a id="status-format"></a>
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci## Format of a status JSON file
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci```text
1531cb0ef41Sopenharmony_ci{
1541cb0ef41Sopenharmony_ci  "something.scope.js": {  // the file name
1551cb0ef41Sopenharmony_ci    // Optional: If the requirement is not met, this test will be skipped
1561cb0ef41Sopenharmony_ci    "requires": ["small-icu"],  // supports: "small-icu", "full-icu"
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci    // Optional: the test will be skipped with the reason printed
1591cb0ef41Sopenharmony_ci    "skip": "explain why we cannot run a test that's supposed to pass",
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci    // Optional: failing tests
1621cb0ef41Sopenharmony_ci    "fail": {
1631cb0ef41Sopenharmony_ci      "note": "You may leave an optional arbitrary note e.g. with TODOs",
1641cb0ef41Sopenharmony_ci      "expected": [
1651cb0ef41Sopenharmony_ci        "test name in the WPT test case, e.g. second argument passed to test()",
1661cb0ef41Sopenharmony_ci        "another test name"
1671cb0ef41Sopenharmony_ci      ],
1681cb0ef41Sopenharmony_ci      "flaky": [
1691cb0ef41Sopenharmony_ci        "flaky test name"
1701cb0ef41Sopenharmony_ci      ]
1711cb0ef41Sopenharmony_ci    }
1721cb0ef41Sopenharmony_ci  }
1731cb0ef41Sopenharmony_ci}
1741cb0ef41Sopenharmony_ci```
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ciA test may have to be skipped because it depends on another irrelevant
1771cb0ef41Sopenharmony_ciWeb API, or certain harness has not been ported in our test runner yet.
1781cb0ef41Sopenharmony_ciIn that case it needs to be marked with `skip` instead of `fail`.
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci[Web Platform Tests]: https://github.com/web-platform-tests/wpt
1811cb0ef41Sopenharmony_ci[`test/fixtures/wpt/README.md`]: ../fixtures/wpt/README.md
1821cb0ef41Sopenharmony_ci[git node wpt]: https://github.com/nodejs/node-core-utils/blob/HEAD/docs/git-node.md#git-node-wpt
183