Skip to content

Commit 50fdd5d

Browse files
committed
Move test environment docs into the new, single environment.js entrypoint.
1 parent 3dbbdf9 commit 50fdd5d

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

packages/tests/README.md

-49
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,6 @@ don't need to be managed collectively otherwise.
3131
there was intent to migrate this repo's tests to WTR, so it still contains its
3232
own tests. These should eventually be moved here too.
3333

34-
## Why are two imports required to set up the test environment in WTR tests?
35-
36-
The test environment is _intentionally_ imported into the test pages through two
37-
separate entrypoints: one for polyfills that the test runner depends on and
38-
another for the test runner itself.
39-
40-
`@web/test-runner-mocha` depends on a module that is _dynamically-generated_ by
41-
the test server, so this compilation step must occur as server middleware.
42-
Particularly, this compilation step can't happen partially at build time because
43-
all modules in the same dependency graph need to be visible to the same
44-
compilation tool to be linked correctly but the dynamically-generated module
45-
does not exist on disk.
46-
47-
This dynamic module requires support for `fetch` as well as `URL` with both a
48-
user-callable constructor and a `searchParams` property (implying
49-
`URLSearchParams` support). Some of the browsers these packages are tested in do
50-
not natively support these features so polyfills are required: `whatwg-fetch`
51-
for `fetch` and `core-js` for `URL` (`core-js` was the only suitable candidate
52-
found at the time for `URL`).
53-
54-
The required `core-js` polyfills are only available in Common JS format.
55-
However, the server middleware used for module compilation does not support
56-
Common JS, so `core-js` must be bundled in an earlier step by a tool that _is_
57-
capable of compiling Common JS.
58-
59-
If `@web/test-runner-mocha` were included in the same bundle as `core-js` and
60-
the bundler were instructed to output a single standard module that still
61-
contains the import statement pointing to the dynamically-generated module
62-
(because that module does not exist as a file on disk that the bundler could
63-
read to include in the bundle), then the bundler would be forced to emit a
64-
module that implicitly describes the entirety of that bundle executing only
65-
_after_ the imported module. **This would cause the dynamically-generated module
66-
that depends on the `core-js` polyfills to run before those polyfills are
67-
installed, making this an unviable option.**
68-
69-
So, to work around this ordering problem and guarantee that the `core-js`
70-
polyfills are (a) able to be bundled, (b) run before the dynamically-generated
71-
module dependency of `@web/test-runner-mocha`, and (c) linked properly with
72-
other modules in the dependency graph by use of a single compilation tool, the
73-
test environment is split into two separately bundled standard modules.
74-
75-
The first module bundle contains polyfills required by `@web/test-runner-mocha`
76-
and the second contains the modules that depend on those polyfills. These
77-
polyfills are then included as separate import statements in each test file,
78-
which guarantees that the server middleware is the one step where all modules
79-
are compiled out. This gives that middleware full control of the loading order
80-
of all modules, allowing it to properly delay the dynamically-generated module
81-
from running until the polyfills it requires have loaded.
82-
8334
## Why does the `webcomponentsjs_` directory name end with a `_`?
8435

8536
`polyserve`, the server used internally by `web-component-tester` (WCT), will

packages/tests/environment.js

+52
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
1+
/**
2+
* The test environment is intentionally imported into the test pages through
3+
* this unbundled entrypoint module as two separate (bundled) modules: one for
4+
* polyfills that the test runner depends on and another for the test runner
5+
* itself.
6+
*
7+
* `@web/test-runner-mocha` depends on a module that is dynamically-generated by
8+
* the test server, so module compilation must occur as server middleware.
9+
* Particularly, this compilation step can't happen partially at build time
10+
* because all modules in the same dependency graph need to be visible to the
11+
* same compilation tool to be linked correctly, but the dynamically-generated
12+
* module does not exist on disk.
13+
*
14+
* This dynamic module requires support for `fetch` as well as `URL` with both a
15+
* user-callable constructor and a `searchParams` property (implying
16+
* `URLSearchParams` support). Some of the browsers these packages are tested in
17+
* do not natively support these features so polyfills are required:
18+
* `whatwg-fetch` for `fetch` and `core-js` for `URL` (`core-js` was the only
19+
* suitable candidate found at the time for `URL`).
20+
*
21+
* The required `core-js` polyfills and `chai` are only available in Common JS
22+
* format. However, the server middleware used for module compilation does not
23+
* support Common JS, so these dependencies must be bundled in an earlier step.
24+
*
25+
* If `@web/test-runner-mocha` were included in the same bundle as the Common JS
26+
* dependencies and the bundler were instructed to output a single standard
27+
* module that still contains the import statement pointing to the
28+
* dynamically-generated module (because that module does not exist as a file on
29+
* disk that the bundler could read to include in the bundle), then the bundler
30+
* would be forced to emit a module that implicitly describes the entirety of
31+
* that bundle executing only _after_ the imported module. **This would cause
32+
* the dynamically-generated module that requires the Common JS dependencies to
33+
* run before those polyfills are installed, making this an unviable option.**
34+
*
35+
* So, to work around this ordering problem and guarantee that (a) the Common JS
36+
* dependencies are bundled before being served, (b) the polyfills are run
37+
* before the dynamically-generated module dependency of
38+
* `@web/test-runner-mocha`, and (c) the module dependency graph is linked
39+
* properly by use of a single compilation tool, the test environment is split
40+
* into two separately bundled standard modules which are then imported by this
41+
* module entrypoint.
42+
*
43+
* The first module bundle contains polyfills required by
44+
* `@web/test-runner-mocha` and the second contains the modules that depend on
45+
* those polyfills. These polyfills are then included as separate import
46+
* statements in each test file, which guarantees that the server middleware is
47+
* the one step where all modules are compiled out. This gives that middleware
48+
* full control of the loading order of all modules, allowing it to properly
49+
* delay the dynamically-generated module from running until the polyfills it
50+
* requires have loaded.
51+
*/
52+
153
export * from './environment-polyfills-bundle.js';
254
export * from './environment-runner-bundle.js';

0 commit comments

Comments
 (0)