Skip to content

Commit c22efd2

Browse files
committed
Refactor the test environment bundle and simplify the docs.
1 parent 50fdd5d commit c22efd2

9 files changed

+41
-68
lines changed

.eslintignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ packages/shadycss/src/interface.*
2828
packages/shadycss/.tsbuildinfo
2929

3030
# tests package
31-
packages/tests/environment-polyfills-bundle.js
32-
packages/tests/environment-runner-bundle.js
31+
packages/tests/chai-bundle.js
32+
packages/tests/core-js_url-bundle.js
3333
packages/tests/shadycss/module/generated/
3434

3535
# webcomponentsjs package

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ packages/shadycss/src/interface.*
2323
packages/shadycss/.tsbuildinfo
2424

2525
# tests package
26-
packages/tests/environment-polyfills-bundle.js
27-
packages/tests/environment-runner-bundle.js
26+
packages/tests/chai-bundle.js
27+
packages/tests/core-js_url-bundle.js
2828
packages/tests/shadycss/module/generated/
2929

3030
# webcomponentsjs package

.prettierignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ packages/shadycss/src/interface.*
2828
packages/shadycss/.tsbuildinfo
2929

3030
# tests package
31-
packages/tests/environment-polyfills-bundle.js
32-
packages/tests/environment-runner-bundle.js
31+
packages/tests/chai-bundle.js
32+
packages/tests/core-js_url-bundle.js
3333
packages/tests/shadycss/module/generated/
3434

3535
# webcomponentsjs package

packages/tests/chai.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* This is the entrypoint for the Chai bundle used as part of the test
3+
* environment. Chai is only distributed as Common JS, so it must be bundled.
4+
*/
5+
6+
export * from 'chai/index.mjs';

packages/tests/core-js_url.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* This is the entrypoint for the `core-js` bundle used as part of the test
3+
* environment. `core-js` is only distributed as Common JS, so it must be
4+
* bundled.
5+
*/
6+
7+
import 'core-js/stable/url';

packages/tests/environment-polyfills.js

-2
This file was deleted.

packages/tests/environment-runner.js

-7
This file was deleted.

packages/tests/environment.js

+18-49
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,23 @@
11
/**
22
* 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.
3+
* this _unbundled_ module so that the server middleware is the only step that
4+
* will compile out modules, if they aren't natively supported. This is
5+
* necessary to guarantee that their loading order matches the native behavior,
6+
* which is important here because `@web/test-runner-mocha` and the
7+
* dynamically-generated module that it imports depend on some of the polyfills
8+
* included here.
69
*
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.
10+
* Also, `chai` and `core-js` are only distributed as Common JS, but the server
11+
* middleware does not support compiling out Common JS, so they are each bundled
12+
* separately and those bundles are referenced here.
5113
*/
5214

53-
export * from './environment-polyfills-bundle.js';
54-
export * from './environment-runner-bundle.js';
15+
import './core-js_url-bundle.js';
16+
import 'whatwg-fetch';
17+
export {assert} from './chai-bundle.js';
18+
import {mocha, runTests as wtrRunTests} from '@web/test-runner-mocha';
19+
20+
export const runTests = (...args) => {
21+
mocha.setup({ui: 'tdd'});
22+
return wtrRunTests(...args);
23+
};

packages/tests/rollup.config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {nodeResolve} from '@rollup/plugin-node-resolve';
33

44
export default [
55
{
6-
input: 'environment-polyfills.js',
6+
input: 'chai.js',
77
output: {
8-
file: 'environment-polyfills-bundle.js',
8+
file: 'chai-bundle.js',
99
format: 'es',
1010
},
1111
plugins: [
@@ -16,9 +16,9 @@ export default [
1616
],
1717
},
1818
{
19-
input: 'environment-runner.js',
19+
input: 'core-js_url.js',
2020
output: {
21-
file: 'environment-runner-bundle.js',
21+
file: 'core-js_url-bundle.js',
2222
format: 'es',
2323
},
2424
plugins: [

0 commit comments

Comments
 (0)