Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test runner for import.meta fields #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
29 changes: 29 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# `import.meta` Tests

These tests provide structured documentation on the expected behavior of different
`import.meta` properties and their availability across different runtimes and tools.

The code is organized as follows:

1. `fixtures/`: Each file is a test scenario.
1. `runtimes/`: How to run the scenarios in different runtimes/environments.
1. `features/`: Code to test for different `import.meta` fields, given loaded fixtures.

To run the tests in bun:

```sh
bun test
```

To run them in node.js:

```sh
npm install
# Then:
node --test **/*.spec.js
# or:
npm test
```

To write a summary of the current status, run `npm run test:report` which will
update [report.md](./report.md).
13 changes: 13 additions & 0 deletions tests/features/main.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, it } from "#test";

import { describeMetaField } from "../runtimes.js";

describeMetaField("main", ({ fixtures: { reflect, indirect } }) => {
it("is true for entry point", () => {
expect(reflect.meta.main).toBe(true);
});

it("is false for non-entry point", () => {
expect(indirect.meta.main).toBe(false);
});
});
13 changes: 13 additions & 0 deletions tests/features/url.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, it } from "#test";

import { describeMetaField } from "../runtimes.js";

describeMetaField("url", ({ baseURL, fixtures: { reflect, url__char } }) => {
it("correct URL for reflect.js", () => {
expect(reflect.meta.url).toBe(new URL(reflect.filename, baseURL).href);
});

it("escapes URL characters", () => {
expect(url__char.meta.url).toMatch(/\/url%20char\.js$/);
});
});
5 changes: 5 additions & 0 deletions tests/fixtures/indirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { printImportMeta } from "./util/print.js";

import meta from './indirect/reflect.js';

printImportMeta(meta);
1 change: 1 addition & 0 deletions tests/fixtures/indirect/reflect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default import.meta;
3 changes: 3 additions & 0 deletions tests/fixtures/reflect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { printImportMeta } from "./util/print.js";

printImportMeta(import.meta);
3 changes: 3 additions & 0 deletions tests/fixtures/url char.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { printImportMeta } from "./util/print.js";

printImportMeta(import.meta);
14 changes: 14 additions & 0 deletions tests/fixtures/util/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function printImportMeta(metaObj) {
// Not all runtimes set meta properties has plain own properties.
const plainObject = {};
for (const key in metaObj) {
plainObject[key] = metaObj[key];
}

console.log(
JSON.stringify({
meta: plainObject,
ownMeta: { ...metaObj },
})
);
}
Loading