@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
Adapter startup (Unit test)
Run the following snippet in a mocha
test file to test the adapter startup process against a mock database.
If the adapter supports compact mode, that is tested aswell.
const path = require("path");
const { tests } = require("@iobroker/testing");
const nobleMock = {
on() {},
state: "poweredOff",
}
tests.unit.adapterStartup(path.join(__dirname, ".."), {
allowedExitCodes: [11],
additionalMockedModules: {
"noble": nobleMock,
"@abandonware/noble": nobleMock,
}
});
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],
defineAdditionalTests: (getHarness) => {
describe("Test sendTo()", () => {
it("Should work", () => {
return new Promise(async (resolve) => {
const harness = getHarness();
await harness.startAdapterAndWait();
harness.sendTo("adapter.0", "test", "message", (resp) => {
console.dir(resp);
resolve();
});
});
});
})
}
});
Build your own unit tests
Take a look at src/lib/startMockAdapter.ts
to get an idea how to test the adapter against a mock database with all the necessary objects in place.
TODO: An API for simplified usage is in the works.