Test environment for QuickJS with Bun/Jest/Vitest-compatible primitives
npm install @ricsam/quickjs-test-environmentTest primitives for running tests in sandboxed QuickJS. Provides a Bun/Jest/Vitest-compatible API with event-based result streaming.
> Note: This is a low-level package. For most use cases, use @ricsam/quickjs-runtime with createRuntime({ testEnvironment: true }) instead.
``bash`
bun add @ricsam/quickjs-test-environment
`typescript
import { setupTestEnvironment } from "@ricsam/quickjs-test-environment";
const handle = setupTestEnvironment(context, {
onEvent: (event) => {
if (event.type === "testEnd") {
const icon = event.test.status === "pass" ? "✓" : "✗";
console.log(${icon} ${event.test.fullName}); Error: ${event.test.error.message}
if (event.test.error) {
console.log();\n${event.results.passed}/${event.results.total} tests passed
}
} else if (event.type === "runEnd") {
console.log();`
}
},
});
- describe, it, test (with .skip, .only, .todo modifiers)beforeAll
- , afterAll, beforeEach, afterEachexpect
- with matchers (toBe, toEqual, toThrow, etc.) and modifiers (.not, .resolves, .rejects)
`javascript
describe("Math operations", () => {
beforeEach(() => {
// setup before each test
});
it("should add numbers", () => {
expect(1 + 1).toBe(2);
});
it("should multiply numbers", async () => {
await Promise.resolve();
expect(2 * 3).toEqual(6);
});
describe("edge cases", () => {
it.skip("should handle infinity", () => {
expect(1 / 0).toBe(Infinity);
});
});
});
`
`typescript
// Load untrusted test code
context.evalCode(userProvidedTestCode);
// Check test count
console.log(Found ${handle.getTestCount()} tests);
// Run all registered tests
const results = await handle.run();
console.log(${results.passed}/${results.total} passed);
// Reset for re-running (optional)
handle.reset();
handle.dispose();
`
| Event Type | Description |
|------------|-------------|
| runStart | Emitted when test run begins (includes testCount, suiteCount) |suiteStart
| | Emitted when a describe block begins |suiteEnd
| | Emitted when a describe block completes |testStart
| | Emitted before each test runs |testEnd
| | Emitted when a test completes (includes status: pass/fail/skip/todo) |runEnd
| | Emitted after all tests complete (includes full results`) |