Skip to content

Commit

Permalink
chore: run test in Node.js (denoland#5258)
Browse files Browse the repository at this point in the history
Co-authored-by: Asher Gomez <[email protected]>
  • Loading branch information
kt3k and iuioiua authored Jul 4, 2024
1 parent 442a497 commit 10be0e3
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
root = true

[*.{ts,js,yml}]
[*.{ts,js,yml,mjs}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ jobs:
with:
name: ${{ matrix.os }}-${{ matrix.deno }}

test-node:
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
node:
- latest
os:
- ubuntu-latest

steps:
- name: Clone repository
uses: actions/checkout@v4

- name: Set up Deno
uses: denoland/setup-deno@v1

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- run: npm install
working-directory: _tools/node_test_runner

- name: Run tests
run: deno task test:node

lint:
runs-on: ${{ matrix.os }}
timeout-minutes: 30
Expand Down
2 changes: 2 additions & 0 deletions _tools/node_test_runner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
52 changes: 52 additions & 0 deletions _tools/node_test_runner/deno_compat_hooks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { transform } from "sucrase";
import { readFile } from "node:fs/promises";
import * as path from "node:path";
import { pathToFileURL } from "node:url";

const decoder = new TextDecoder();
const encoder = new TextEncoder();

export async function resolve(specifier, context, nextResolve) {
const match = specifier.match(/^@std\/([^\/]+)(\/[^\/]+)?$/);
if (match) {
const dir = match[1].replaceAll("-", "_");
const exportName = match[2] ? "." + match[2] : ".";
const denoJson = await readFile(path.join(dir, "deno.json"), "utf-8");
const { exports } = JSON.parse(denoJson);
const submodPath = path.resolve(dir, exports[exportName]);
return nextResolve(pathToFileURL(submodPath).href, context);
}

return await nextResolve(specifier, context);
}

export async function load(specifier, context, nextLoad) {
const { format, source } = await nextLoad(specifier, context).catch(
async (error) => {
if (
error.code === "ERR_UNKNOWN_FILE_EXTENSION" &&
specifier.endsWith(".ts")
) {
return await nextLoad(specifier, {
...context,
format: "module",
});
} else {
throw error;
}
},
);

if (source && specifier.endsWith(".ts")) {
return {
format,
source: encoder.encode(
transform(decoder.decode(source), { transforms: ["typescript"] }).code,
),
};
}

return { format, source };
}
8 changes: 8 additions & 0 deletions _tools/node_test_runner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "node_test_runner",
"private": true,
"dependencies": {
"@deno/shim-deno-test": "^0.5.0",
"sucrase": "^3.35.0"
}
}
9 changes: 9 additions & 0 deletions _tools/node_test_runner/register_deno_shim.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { Deno, testDefinitions } from "@deno/shim-deno-test";
import { register } from "node:module";

register(new URL("deno_compat_hooks.mjs", import.meta.url));

globalThis.Deno = Deno;
globalThis.testDefinitions = testDefinitions;
55 changes: 55 additions & 0 deletions _tools/node_test_runner/run_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { test } from "node:test";

import "../../collections/aggregate_groups_test.ts";
import "../../collections/associate_by_test.ts";
import "../../collections/associate_with_test.ts";
import "../../collections/chunk_test.ts";
import "../../collections/deep_merge_test.ts";
import "../../collections/distinct_by_test.ts";
import "../../collections/distinct_test.ts";
import "../../collections/drop_last_while_test.ts";
import "../../collections/drop_while_test.ts";
import "../../collections/filter_entries_test.ts";
import "../../collections/filter_keys_test.ts";
import "../../collections/filter_values_test.ts";
import "../../collections/find_single_test.ts";
import "../../collections/first_not_nullish_of_test.ts";
import "../../collections/includes_value_test.ts";
import "../../collections/intersect_test.ts";
import "../../collections/invert_by_test.ts";
// TODO(kt3k): Enable this
// import "../../collections/invert_test.ts";
import "../../collections/join_to_string_test.ts";
import "../../collections/map_entries_test.ts";
import "../../collections/map_keys_test.ts";
import "../../collections/map_not_nullish_test.ts";
import "../../collections/map_values_test.ts";
import "../../collections/max_by_test.ts";
import "../../collections/max_of_test.ts";
import "../../collections/max_with_test.ts";
import "../../collections/min_by_test.ts";
import "../../collections/min_of_test.ts";
import "../../collections/min_with_test.ts";
import "../../collections/omit_test.ts";
import "../../collections/partition_entries_test.ts";
import "../../collections/partition_test.ts";
import "../../collections/permutations_test.ts";
import "../../collections/pick_test.ts";
import "../../collections/reduce_groups_test.ts";
import "../../collections/running_reduce_test.ts";
import "../../collections/sample_test.ts";
import "../../collections/sliding_windows.ts";
import "../../collections/sort_by_test.ts";
import "../../collections/sum_of_test.ts";
import "../../collections/take_last_while_test.ts";
import "../../collections/take_while_test.ts";
import "../../collections/union_test.ts";
import "../../collections/unzip_test.ts";
import "../../collections/without_all_test.ts";
import "../../collections/zip_test.ts";

for (const testDef of testDefinitions) {
test(testDef.name, testDef.fn);
}
2 changes: 2 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"tasks": {
"test": "deno test --unstable-http --unstable-webgpu --doc --allow-all --parallel --coverage --trace-leaks --clean",
"test:browser": "git grep --name-only \"This module is browser compatible.\" | grep -v deno.json | grep -v .github/workflows | grep -v _tools | grep -v encoding/README.md | grep -v media_types/vendor/update.ts | xargs deno check --config browser-compat.tsconfig.json",
"test:node": "node --import ./_tools/node_test_runner/register_deno_shim.mjs ./_tools/node_test_runner/run_test.mjs",
"fmt:licence-headers": "deno run --allow-read --allow-write ./_tools/check_licence.ts",
"lint:deprecations": "deno run --allow-read --allow-net --allow-env ./_tools/check_deprecation.ts",
"lint:circular": "deno run --allow-env --allow-read --allow-write --allow-net=deno.land,jsr.io ./_tools/check_circular_package_dependencies.ts",
Expand All @@ -76,6 +77,7 @@
"cov",
"jsonc/testdata",
"front_matter/testdata",
"_tools/node_test_runner",
"coverage",
"docs"
],
Expand Down

0 comments on commit 10be0e3

Please sign in to comment.