Skip to content

Commit

Permalink
refactor: leverage ES Modules in tests (#208)
Browse files Browse the repository at this point in the history
* refactor: remove embedded Deno assert bundle

We can import the vendored module using a relative path now.

* refactor: extract `test(name, fn)` helper

Move the function creating isolated scopes for individual tests
into the `helpers.js` and use this new shared helper in all tests.

---------

Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
bajtos authored May 11, 2023
1 parent 17a038c commit 75462c4
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 90 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
!**/*.md
!**/*.js
runtime/vendored
runtime/tests/js/vendored
target

# Let's keep LICENSE.md in the same formatting as we use in other PL repositories
Expand Down
8 changes: 2 additions & 6 deletions runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,11 @@ impl ModuleLoader for ZinniaModuleLoader {
};
if is_module_local {
read_file_to_string(module_specifier.to_file_path().unwrap()).await?
} else if spec_str == "https://deno.land/std@0.177.0/testing/asserts.ts" {
} else if spec_str == "https://deno.land/std@0.177.0/testing/asserts.ts" || spec_str == "https://deno.land/std@0.181.0/testing/asserts.ts" {
return Err(anyhow!(
"The vendored version of deno asserts was upgraded to 0.181.0. Please update your imports.\nModule URL: {spec_str}\nImported from: {}",
"Zinnia no longer bundles Deno asserts. Please vendor the module yourself and load it using a relative path.\nModule URL: {spec_str}\nImported from: {}",
maybe_referrer.map(|u| u.to_string()).unwrap_or("(none)".into())
));
} else if spec_str == "https://deno.land/std@0.181.0/testing/asserts.ts" {
// Temporary workaround until we implement ES Modules
// https://github.com/filecoin-station/zinnia/issues/43
include_str!("./vendored/asserts.bundle.js").to_string()
} else {
let mut msg = if module_specifier.scheme() == "file" {
format!("Cannot import files outside of module root directory {}. ", module_root.display())
Expand Down
15 changes: 2 additions & 13 deletions runtime/tests/js/fetch_tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, assertEquals } from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { test } from "./helpers.js";
import { assert, assertEquals } from "./vendored/asserts.bundle.js";

await test("fetch", async () => {
const res = await fetch("https://google.com/");
Expand Down Expand Up @@ -32,15 +33,3 @@ await test("Response", async () => {
const response = new Response();
await response.arrayBuffer();
});

// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
async function test(name, fn) {
try {
return await fn();
} catch (err) {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}
}
23 changes: 23 additions & 0 deletions runtime/tests/js/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
export function test(name, fn) {
let maybePromise;
try {
maybePromise = fn();
} catch {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}

if (typeof maybePromise?.then !== "function") {
// The test fn was synchronous, we are done.
return maybePromise;
}

// The test fn returned a promise, we need to wait for completion
return maybePromise.catch((err) => {
err.message = `Test ${name} failed. ` + err.message;
throw err;
});
}
15 changes: 2 additions & 13 deletions runtime/tests/js/libp2p_tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, assertEquals } from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { assert, assertEquals } from "./vendored/asserts.bundle.js";
import { test } from "./helpers.js";

await test("get peer id", () => {
const id = Zinnia.peerId;
Expand Down Expand Up @@ -80,15 +81,3 @@ await test("ping remote peer", async () => {
// The chunk should be Uint8Array
assertEquals(chunks[0].constructor, Uint8Array);
});

// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
async function test(name, fn) {
try {
return await fn();
} catch (err) {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}
}
20 changes: 2 additions & 18 deletions runtime/tests/js/module_loader_tests.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
assertEquals,
assertMatch,
assertRejects,
assertThrows,
} from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { assertEquals, assertMatch, assertRejects } from "./vendored/asserts.bundle.js";
import { test } from "./helpers.js";

test("dynamically import file next to the main module file", async () => {
const { KEY } = await import("./empty_module.js");
Expand All @@ -25,15 +21,3 @@ test("cannot import files over http", async () => {
let err = await assertRejects(() => import("https://deno.land/std@0.181.0/version.ts"));
assertMatch(err.message, /Zinnia supports importing from relative paths only/);
});

// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
async function test(name, fn) {
try {
return await fn();
} catch (err) {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}
}
15 changes: 2 additions & 13 deletions runtime/tests/js/station_apis_tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertStrictEquals } from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { assertStrictEquals } from "./vendored/asserts.bundle.js";
import { test } from "./helpers.js";

test("Zinnia.walletAddress", () => {
// Runtime JS tests are executed with the default configuration
Expand All @@ -14,15 +15,3 @@ test("smoke tests for reporting APIs", () => {
Zinnia.activity.error("activity.error");
Zinnia.jobCompleted();
});

// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
function test(name, fn) {
try {
fn();
} catch (err) {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}
}
File renamed without changes.
15 changes: 2 additions & 13 deletions runtime/tests/js/webapis_tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertEquals } from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { assertEquals } from "./vendored/asserts.bundle.js";
import { test } from "./helpers.js";

test("AbortController", () => {
assertEquals(typeof AbortController, "function", "typeof AbortController");
Expand Down Expand Up @@ -27,15 +28,3 @@ test("URL", () => {
const url = new URL("https://filstation.app");
assertEquals(url.host, "filstation.app");
});

// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
function test(name, fn) {
try {
return fn();
} catch (err) {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}
}
15 changes: 2 additions & 13 deletions runtime/tests/js/webcrypto_tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertEquals, assertNotEquals } from "https://deno.land/std@0.181.0/testing/asserts.ts";
import { assertEquals, assertNotEquals } from "./vendored/asserts.bundle.js";
import { test } from "./helpers.js";

await test("getRandomValues()", async () => {
const first = new Uint8Array(4);
Expand Down Expand Up @@ -32,15 +33,3 @@ await test("generateKey(), sign() and verify()", async () => {

assertEquals(result, "signature verified");
});

// A dummy wrapper to create isolated scopes for individual tests
// We should eventually replace this with a proper test runner
// See https://github.com/filecoin-station/zinnia/issues/30
async function test(name, fn) {
try {
return await fn();
} catch (err) {
err.message = `Test ${name} failed. ` + err.message;
throw err;
}
}
2 changes: 1 addition & 1 deletion runtime/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { fromFileUrl } from "https://deno.land/std@0.181.0/path/mod.ts";
await vendor("https://deno.land/std@0.181.0/testing/asserts.ts", "asserts.bundle.js");

async function vendor(url, outfile) {
const outpath = fromFileUrl(import.meta.resolve(`./vendored/${outfile}`));
const outpath = fromFileUrl(import.meta.resolve(`./tests/js/vendored/${outfile}`));
const cmd = ["deno", "bundle", url, "--", outpath];
const child = Deno.run({ cmd });
const status = await child.status();
Expand Down

0 comments on commit 75462c4

Please sign in to comment.