A Lightweight Testing Framework for Deno

Rhum

A lightweight testing framework for Deno.

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);
    });
  });
});

Rhum.run();

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
  • Hooks

Documentation

Rhum can also be used with SuperDeno. You can also read Craig Morten’s tutorial on strengthening your testing process using both Rhum and SuperDeno, here.

Alongside using Rhum, the RhumRunner is also exposed allowing you to extend it, which Rhum is an instance of. It has been exposed to allow you to extend it, should you have any reason or use case to do so. An example can be seen below:

import { RhumRunner } from "https://deno.land/x/rhum@v1.0.0/mod.ts";

class MyRunner extends RhumRunner {
  ...
}
const Runner = new MyRunner();
Runner.testPlan(...); // Same as "Rhum.testPlan(...)"

Properties

Rhum.asserts

The asserts module from the testing module, but attached to Rhum.

Rhum.asserts.assertEquals(true, true); // pass
Rhum.asserts.assertEquals(true, false); // fail

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,
});

Methods

Rhum.afterAll

Used to define a hook that will execute after all test suites or test cases. If this is used inside of a test plan, then it will execute after all test suites. If this is used inside of a test suite, then it will execute after all test cases.

Rhum.testPlan("My Plan", () => {
  Rhum.afterAll(() => {
    // Runs once after all test suites in this test plan
  });
  Rhum.testSuite("My Suite 1", () => {
    Rhum.afterAll(() => {
      // Runs once after all test cases in this test suite
    });
    Rhum.testCase("My Test Case 1", () => {
      ...
    });
  });
});

Rhum.afterEach

Used to define a hook that will execute after each test suite or test case. If this is used inside of a test plan, then it will execute after each test suite. If this is used inside of a test suite, then it will execute after each test case.

Rhum.testPlan("My Plan", () => {
  Rhum.afterEach(() => {
    // Runs after each test suite in this test plan
  });
  Rhum.testSuite("My Suite 1", () => {
    Rhum.afterEach(() => {
      // Runs after each test case in this test suite
    });
    Rhum.testCase("My Test Case 1", () => {
      ...
    });
  });
});

Rhum.beforeAll

Used to define a hook that will execute before all test suites or test cases. If this is used inside of a test plan, then it will execute before all test suites. If this is used inside of a test suite, then it will execute before all test cases.

Rhum.testPlan("My Plan", () => {
  Rhum.beforeAll(() => {
    // Runs once before all test suites in this test plan
  });
  Rhum.testSuite("My Suite 1", () => {
    Rhum.beforeAll(() => {
      // Runs once before all test cases in this test suite
    });
    Rhum.testCase("My Test Case 1", () => {
      ...
    });
  });
});

Rhum.beforeEach

Used to define a hook that will execute before each test suite or test case. If this is used inside of a test plan, then it will execute before each test suite. If this is used inside of a test suite, then it will execute before each test case.

Rhum.testPlan("My Plan", () => {
  Rhum.beforeEach(() => {
    // Runs before each test suite in this test plan
  });
  Rhum.testSuite("My Suite 1", () => {
    Rhum.beforeEach(() => {
      // Runs before each test case in this test suite
    });
    Rhum.testCase("My Test Case 1", () => {
      ...
    });
  });
});

Rhum.run

Runs your test plan.

Rhum.testPlan("My Plan", () => {
  ...
});
Rhum.run();

Rhum.skip

Allows a test plan, suite, or case to be skipped when the tests run.

Rhum.testPlan("My Plan", () => {
  Rhum.skip("My Suite 1", () => { // will not run this block
    Rhum.testCase("My Test Case In Suite 1", () => {
      ...
    });
  });
  Rhum.testSuite("My Suite 2", () => {
    Rhum.testCase("My Test Case In Suite 2", () => {
      ...
    });
    Rhum.skip("My Other Test Case In Suite 2", () => { // will not run this block
      ...
    });
  });
});

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. Test cases can only be defined inside of a test suite.

Rhum.testPlan("My Plan", () => {
  Rhum.testSuite("My Suite 1", () => {
    Rhum.testCase("My Test Case 1", () => {
      Rhum.assert.assertEquals(something, true);
    });
    Rhum.testCase("My Test Case 2", () => {
      Rhum.assert.assertEquals(something, false);
    });
  });
});

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. Test plans are required in order to define a test suite with test cases.

Rhum.testPlan("My Plan", () => {
    ...
});

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. Test suites can only be defined inside of a test plan.

Rhum.testPlan("My Plan", () => {
  Rhum.testSuite("My Suite 1", () => {
    ...
  });
  Rhum.testSuite("My Suite 2", () => {
    ...
  });
});

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:

  • Mocha — For how you write tests in Rhum, and the use of hooks
  • Baretest — Being minimalistic

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.

Download Details:

Author: drashland

GitHub: https://github.com/drashland/rhum

#deno #nodejs #node-js #javascript

A Lightweight Testing Framework for Deno
23.00 GEEK