Rhum
A lightweight testing framework for Deno.
Table of Contents
Quick Start
Create your test plan:
// File: app_test.ts
import { Rhum } from "https://deno.land/x/rhum@v1.0.0/mod.ts";
let value = false;
function run() {
return true;
}
async function close() {
value = true;
return value;
}
Rhum.testPlan("app_test.ts", () => {
// Run the first test suite
Rhum.testSuite("run()", () => {
Rhum.testCase("Returns true", () => {
const result = run();
Rhum.asserts.assertEquals(true, result);
});
});
// Run the second test suite
Rhum.testSuite("close()", () => {
Rhum.testCase("Returns true", async () => {
const result = await close();
Rhum.asserts.assertEquals(true, result);
});
});
});
Run your test plan:
$ deno test app_test.ts
Read the output:
Compile file:///.deno.test.ts
running 2 tests
app_test.ts
run()
Returns true ... ok (3ms)
close()
Returns true ... ok (1ms)
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (4ms)
Features
- Descriptive naming for your tests
- Lightweight
- Zero dependencies
- Simple and easy to use
- Asynchronous support
- Still uses
Deno.test
under the hood - Skip functionality
- Mock requests
Documentation
Rhum.testPlan
Groups up test suites to describe a test plan. Usually, a test plan is per file and contains the tests suites and test cases for a single file.
Rhum.testPlan("app_test.ts", () => {
...
...
...
});
Rhum.testSuite
A test suite usually describes a method or property name and groups up all test cases for that method or property. You can define multiple test suites under a test plan.
Rhum.testPlan("app_test.ts", () => {
Rhum.testSuite("run()", () => {
...
...
...
});
Rhum.testSuite("close()", () => {
...
...
...
});
});
Rhum.testCase
A test case is grouped by a test suite and it is what makes the assertions - it is the test. You can define multiple test cases under a test suite. Test cases can also be asynchronous.
Rhum.testPlan("app_test.ts", () => {
Rhum.testSuite("run()", () => {
Rhum.testCase("should return true", () => {
Rhum.assert.assertEquals(run(), true);
});
Rhum.testCase("should return false", () => {
Rhum.assert.assertEquals(run(), false);
});
});
});
Rhum.asserts
The asserts module, but attached to Rhum
.
Rhum.asserts.assertEquals(true, true) // pass
Rhum.asserts.assertEquals(true, false) // fail
Rhum.skip
Allows a test plan, suite, or case to be skipped when the tests run.
Rhum.testPlan("app_test.ts", () => {
Rhum.skip("run()", () => { // will not run this block
Rhum.testCase("Returns true", () => {
...
});
});
Rhum.testSuite("close()", () => {
Rhum.testCase("Returns true", () => {
...
});
Rhum.skip("Returns true", () => { // will not run this block
...
});
});
});
Rhum.mocks
An object of functions to help you mock objects.
Rhum.mocks.ServerRequest
Creates a mock object of a ServerRequest.
const encodedBody = new TextEncoder().encode(JSON.stringify({ body_param: "hello", })); const body = new Deno.Buffer(encodedBody as ArrayBuffer); const mockRequest = Rhum.mocks.ServerRequest("/api/users/1", "GET", { headers: { "Content-Type": "application/json", "Token": "Rhum" }, body: body, });
Why Use Rhum?
Rhum allows you to write tests in a very descriptive way – from a code perspective or output perspective.
Rhum is designed to aid your testing efforts – providing many utilities as wrappers around Deno’s existing Deno.test
. Rhum is meant to improve the user experience when it comes to writing tests, such as:
- Readability for test cases
- Features that aren’t available in Deno yet (hooks)
Rhum takes concepts from the following:
Rhum can be added directly into any project. All you need to do is import Rhum and you are ready to start writing tests or bring your existing tests under Rhum.
Contributing
Contributors are welcomed!
Please read through our contributing guidelines. Included are directions for opening issues, coding standards, and notes on development.
License
By contributing your code, you agree to license your contribution under the MIT License.