Skip to content

Commit 5bb1a87

Browse files
Migrate testing framework from jest to vitest
@bytecodealliance/preview2-shim is ESM-only package and it's hard to maintain ESM tests with jest. So it's good time to migrate our test suites to vitest.
1 parent 063408f commit 5bb1a87

File tree

10 files changed

+3979
-9856
lines changed

10 files changed

+3979
-9856
lines changed

package-lock.json

Lines changed: 3944 additions & 9820 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/npm-packages/ruby-wasm-wasi/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
],
4040
"license": "MIT",
4141
"scripts": {
42-
"test:run": "npm run test:unit && npm run test:jest && npm run test:e2e",
43-
"test:jest": "NODE_OPTIONS=\"--experimental-wasi-unstable-preview1\" jest --coverage",
42+
"test:run": "npm run test:unit && npm run test:vitest && npm run test:e2e",
43+
"test:vitest": "NODE_OPTIONS=\"--experimental-wasi-unstable-preview1\" vitest run --pool=forks --testTimeout 300000 ./test/",
4444
"test:unit": "./tools/run-test-unit.mjs",
4545
"test:e2e": "playwright install && npm run test:e2e:examples && npm run test:e2e:integrations",
4646
"test:e2e:examples": "playwright test -c test-e2e/playwright.examples.config.ts",
@@ -57,11 +57,10 @@
5757
"@bytecodealliance/jco": "../../../vendor/jco",
5858
"@rollup/plugin-node-resolve": "^15.2.3",
5959
"@rollup/plugin-typescript": "^11.1.6",
60-
"@types/jest": "^29.5.11",
6160
"@types/node": "20.12.2",
62-
"jest": "^29.7.0",
6361
"prettier": "^3.2.5",
64-
"typescript": "^5.4.3"
62+
"typescript": "^5.4.3",
63+
"vitest": "^1.5.2"
6564
},
6665
"dependencies": {
6766
"tslib": "^2.6.1"

packages/npm-packages/ruby-wasm-wasi/test/eval.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { initRubyVM } = require("./init");
1+
import { initRubyVM } from "./init";
2+
import { describe, test, expect } from "vitest"
23

34
describe("Ruby code evaluation", () => {
45
test("empty expression", async () => {

packages/npm-packages/ruby-wasm-wasi/test/eval_async.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { initRubyVM } = require("./init");
1+
import { initRubyVM } from "./init";
2+
import { describe, test, expect } from "vitest"
23

34
describe("Async Ruby code evaluation", () => {
45
test("async eval over microtasks", async () => {

packages/npm-packages/ruby-wasm-wasi/test/gc.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { initRubyVM } = require("./init");
1+
import { initRubyVM } from "./init";
2+
import { describe, test, expect } from "vitest"
23

34
describe("GC integration", () => {
45
test("Wrapped Ruby object should live until wrapper will be released", async () => {

packages/npm-packages/ruby-wasm-wasi/test/init.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ const initRubyVM = async ({ suppressStderr } = { suppressStderr: false }) => {
2424
if (process.env.RUBY_ROOT) {
2525
preopens["/usr"] = path.join(process.env.RUBY_ROOT, "./usr");
2626
}
27+
let stderrFd = 2;
28+
if (suppressStderr) {
29+
const devNullFd = await fs.open("/dev/null", "w");
30+
stderrFd = devNullFd.fd;
31+
}
2732
const wasi = new WASI({
2833
args: ["ruby.wasm"].concat(process.argv.slice(2)),
29-
stderr: suppressStderr ? 0 : 2,
34+
stderr: stderrFd,
3035
preopens,
3136
});
3237

packages/npm-packages/ruby-wasm-wasi/test/package.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const fs = require("fs/promises");
2-
const path = require("path");
3-
const { WASI } = require("wasi");
4-
const { RubyVM } = require("../dist/cjs/index");
5-
const { DefaultRubyVM } = require("../dist/cjs/node");
1+
import * as path from "path";
2+
import * as fs from "fs/promises";
3+
import { WASI } from "wasi";
4+
import { RubyVM } from "../dist/esm/index";
5+
import { DefaultRubyVM } from "../dist/esm/node";
6+
import { describe, test, expect } from "vitest"
67

78
const initRubyVM = async (rubyModule, args) => {
89
const wasi = new WASI();
@@ -28,7 +29,6 @@ const initRubyVM = async (rubyModule, args) => {
2829
};
2930

3031
describe("Packaging validation", () => {
31-
jest.setTimeout(20 /*sec*/ * 1000);
3232
if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
3333
test.skip("skip", () => {});
3434
return;

packages/npm-packages/ruby-wasm-wasi/test/to_js.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { initRubyVM } = require("./init");
1+
import { initRubyVM } from "./init";
2+
import { describe, test, expect } from "vitest"
23

34
describe("RbValue#toJS", () => {
45
test.each([

packages/npm-packages/ruby-wasm-wasi/test/vm.test.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { initRubyVM, rubyVersion } = require("./init");
1+
import { initRubyVM, rubyVersion } from "./init";
2+
import { describe, test, expect, vi } from "vitest"
23

34
describe("RubyVM", () => {
45
test("empty expression", async () => {
@@ -170,26 +171,15 @@ eval:11:in \`<main>'`
170171
const setVM = vm.eval(`proc { |vm| JS::RubyVM = vm }`);
171172
setVM.call("call", vm.wrap(vm));
172173

173-
// HACK: We need to capture all promises to avoid unhandled promise rejection
174-
const promises = [];
175-
const _Promise = global.Promise;
176-
const spy = jest.spyOn(global, "Promise").mockImplementation((future) => {
177-
const promise = new _Promise(future);
178-
promises.push(promise);
179-
return promise;
180-
});
181-
182-
vm.evalAsync(code);
183-
184174
const rejections = [];
185-
for (const promise of promises) {
186-
try {
187-
await promise;
188-
} catch (e) {
175+
const _evalAsync = vm.evalAsync;
176+
const spy = vi.spyOn(vm, "evalAsync").mockImplementation((code) => {
177+
const promise = _evalAsync.call(vm, code).catch((e) => {
189178
rejections.push(e);
190-
}
191-
}
192-
179+
});
180+
return promise
181+
})
182+
await vm.evalAsync(code)
193183
spy.mockReset();
194184

195185
expect(rejections[0].message).toMatch(

packages/npm-packages/ruby-wasm-wasi/test/wrap.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { initRubyVM } = require("./init");
1+
import { initRubyVM } from "./init";
2+
import { describe, test, expect } from "vitest"
23

34
describe("RubyVM#wrap", () => {
45
test("Wrap arbitrary JS object to RbValue", async () => {

0 commit comments

Comments
 (0)