1# Web Platform Tests 2 3The tests here are drivers for running the [Web Platform Tests][]. 4 5See [`test/fixtures/wpt/README.md`][] for a hash of the last 6updated WPT commit for each module being covered here. 7 8See the json files in [the `status` folder](./status) for prerequisites, 9expected failures, and support status for specific tests in each module. 10 11Currently there are still some Web Platform Tests titled `test-whatwg-*` 12under `test/parallel` that have not been migrated to be run with the 13WPT harness and have automatic updates. There are also a few 14`test-whatwg-*-custom-*` tests that may need to be upstreamed. 15This folder covers the tests that have been migrated. 16 17<a id="add-tests"></a> 18 19## How to add tests for a new module 20 21### 1. Create a status file 22 23For example, to add the URL tests, add a `test/wpt/status/url.json` file. 24 25In the beginning, it's fine to leave an empty object `{}` in the file if 26it's not yet clear how compliant the implementation is, 27the requirements and expected failures can be figured out in a later step 28when the tests are run for the first time. 29 30See [Format of a status JSON file](#status-format) for details. 31 32### 2. Pull the WPT files 33 34Use the [git node wpt][] command to download the WPT files into 35`test/fixtures/wpt`. For example, to add URL tests: 36 37```text 38$ cd /path/to/node/project 39$ git node wpt url 40``` 41 42### 3. Create the test driver 43 44For example, for the URL tests, add a file `test/wpt/test-url.js`: 45 46```js 47'use strict'; 48 49const { WPTRunner } = require('../common/wpt'); 50 51const runner = new WPTRunner('url'); 52 53// Set Node.js flags required for the tests. 54runner.setFlags(['--expose-internals']); 55 56// Set a script that will be executed in the worker before running the tests. 57runner.setInitScript(` 58 const { internalBinding } = require('internal/test/binding'); 59 const { DOMException } = internalBinding('messaging'); 60 global.DOMException = DOMException; 61`); 62 63runner.runJsTests(); 64``` 65 66This driver is capable of running the tests located in `test/fixtures/wpt/url` 67with the WPT harness while taking the status file into account. 68 69### 4. Run the tests 70 71Run the test using `tools/test.py` and see if there are any failures. 72For example, to run all the URL tests under `test/fixtures/wpt/url`: 73 74```text 75$ tools/test.py wpt/test-url 76``` 77 78To run a specific test in WPT, for example, `url/url-searchparams.any.js`, 79pass the file name as argument to the corresponding test driver: 80 81```text 82node test/wpt/test-url.js url-searchparams.any.js 83``` 84 85If there are any failures, update the corresponding status file 86(in this case, `test/wpt/status/url.json`) to make the test pass. 87 88For example, to mark `url/url-searchparams.any.js` as expected to fail, 89add this to `test/wpt/status/url.json`: 90 91```json 92 "url-searchparams.any.js": { 93 "fail": { 94 "expected": [ 95 "test name in the WPT test case, e.g. second argument passed to test()" 96 ] 97 } 98 } 99``` 100 101See [Format of a status JSON file](#status-format) for details. 102 103### 5. Commit the changes and submit a Pull Request 104 105See [the contributing guide](../../CONTRIBUTING.md). 106 107## How to update tests for a module 108 109The tests can be updated in a way similar to how they are added. 110Run Step 2 and Step 4 of [adding tests for a new module](#add-tests). 111 112The [git node wpt][] command maintains the status of the local 113WPT subset, if no files are updated after running it for a module, 114the local subset is up to date and there is no need to update them 115until they are changed in the upstream. 116 117## How it works 118 119Note: currently this test suite only supports `.js` tests. There is 120ongoing work in the upstream to properly split out the tests into files 121that can be run in a shell environment like Node.js. 122 123### Getting the original test files and harness from WPT 124 125The original files and harness from WPT are downloaded and stored in 126`test/fixtures/wpt`. 127 128The [git node wpt][] command automate this process while maintaining a map 129containing the hash of the last updated commit for each module in 130`test/fixtures/wpt/versions.json` and [`test/fixtures/wpt/README.md`][]. 131It also maintains the LICENSE file in `test/fixtures/wpt`. 132 133### Loading and running the tests 134 135Given a module, the `WPTRunner` class in [`test/common/wpt`](../common/wpt.js) 136loads: 137 138* `.js` test files (for example, `test/common/wpt/url/*.js` for `url`) 139* Status file (for example, `test/wpt/status/url.json` for `url`) 140* The WPT harness 141 142Then, for each test, it creates a worker thread with the globals and mocks, 143sets up the harness result hooks, loads the metadata in the test (including 144loading extra resources), and runs all the tests in that worker thread, 145skipping tests that cannot be run because of lack of dependency or 146expected failures. 147 148<a id="status-format"></a> 149 150## Format of a status JSON file 151 152```text 153{ 154 "something.scope.js": { // the file name 155 // Optional: If the requirement is not met, this test will be skipped 156 "requires": ["small-icu"], // supports: "small-icu", "full-icu" 157 158 // Optional: the test will be skipped with the reason printed 159 "skip": "explain why we cannot run a test that's supposed to pass", 160 161 // Optional: failing tests 162 "fail": { 163 "note": "You may leave an optional arbitrary note e.g. with TODOs", 164 "expected": [ 165 "test name in the WPT test case, e.g. second argument passed to test()", 166 "another test name" 167 ], 168 "flaky": [ 169 "flaky test name" 170 ] 171 } 172 } 173} 174``` 175 176A test may have to be skipped because it depends on another irrelevant 177Web API, or certain harness has not been ported in our test runner yet. 178In that case it needs to be marked with `skip` instead of `fail`. 179 180[Web Platform Tests]: https://github.com/web-platform-tests/wpt 181[`test/fixtures/wpt/README.md`]: ../fixtures/wpt/README.md 182[git node wpt]: https://github.com/nodejs/node-core-utils/blob/HEAD/docs/git-node.md#git-node-wpt 183