@iobroker/testing
This repo provides utilities for testing of ioBroker adapters and other ioBroker-related modules. It supports:
- Unit tests using mocks (without a running JS-Controller)
- Integration tests that test against a running JS-Controller instance.
The unit tests are realized using the following tools that are provided by this module:
- A mock database which implements the most basic functionality of
ioBroker
's Objects and States DB by operating on Map
objects. - A mock
Adapter
that is connected to the mock database. It implements basic functionality of the real Adapter
class, but only operates on the mock database.
Predefined methods for both unit and integration tests are exported.
Usage
Validating package files (package.json, io-package.json, ...)
const path = require("path");
const { tests } = require("@iobroker/testing");
tests.packageFiles(path.join(__dirname, ".."));
Adapter startup (Integration test)
Run the following snippet in a mocha
test file to test the adapter startup process against a real JS-Controller instance:
const path = require("path");
const { tests } = require("@iobroker/testing");
tests.integration(path.join(__dirname, ".."), {
allowedExitCodes: [11],
controllerVersion: "latest",
defineAdditionalTests({ suite }) {
suite("Test sendTo()", (getHarness) => {
let harness;
before(() => {
harness = getHarness();
});
it("Should work", () => {
return new Promise(async (resolve) => {
await harness.startAdapterAndWait();
harness.sendTo("adapter.0", "test", "message", (resp) => {
console.dir(resp);
resolve();
});
});
});
});
suite.only("Only this will run", (getHarness) => {
});
suite.skip("This will never run", (getHarness) => {
});
},
});
Adapter startup (Unit test)
Unit tests for adapter startup were removed and are essentially a no-op now.
If you defined your own tests, they should still work.
const path = require("path");
const { tests } = require("@iobroker/testing");
tests.unit(path.join(__dirname, ".."), {
defineAdditionalTests() {
it("works", () => {
});
},
});
Helper functions for your own tests
Under utils
, several functions are exposed to use in your own tests:
const { utils } = require("@iobroker/testing");
Currently, only utils.unit
is defined which contains tools for unit tests:
createMocks()
const { database, adapter } = utils.unit.createMocks();
const { database, adapter } = utils.unit.createMocks(adapterOptions);
This method creates a mock database and a mock adapter. See below for a more detailed description
createAsserts()
const asserts = utils.unit.createAsserts(database, adapter);
These methods take a mock database and adapter and create a set of asserts for your tests. All IDs may either be a string, which is taken literally, or an array of strings which are concatenated with "."
. If an ID is not fully qualified, the adapter namespace is prepended automatically.
assertObjectExists(id: string | string[])
asserts that an object with the given ID exists in the database.assertStateExists(id: string | string[])
asserts that a state with the given ID exists in the database.assertStateHasValue(id: string | string[], value: any)
asserts that a state has the given value.assertStateIsAcked(id: string | string[], ack: boolean = true)
asserts that a state is ack
ed (or not if ack === false
).assertStateProperty(id: string | string[], property: string, value: any)
asserts that one of the state's properties (e.g. from
) has the given valueassertObjectCommon(id: string | string[], common: ioBroker.ObjectCommon)
asserts that an object's common part includes the given common
object.assertObjectNative(id: string | string[], native: object)
asserts that an object's native part includes the given native
object.
MockDatabase
TODO
MockAdapter
TODO
Example
Here's an example how this can be used in a unit test:
import { tests, utils } from "@iobroker/testing";
tests.unit(path.join(__dirname, ".."), {
defineAdditionalTests() {
const { adapter, database } = utils.unit.createMocks();
const { assertObjectExists } = utils.unit.createAsserts(
database,
adapter,
);
describe("my test", () => {
afterEach(() => {
adapter.resetMockHistory();
database.clear();
});
it("works", () => {
const theObject: ioBroker.PartialObject = {
_id: "whatever",
type: "state",
common: {
role: "whatever",
},
};
mocks.database.publishObject(theObject);
assertObjectExists(theObject._id);
});
});
},
});