![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@iobroker/testing
Advanced tools
This repo provides utilities for testing of ioBroker adapters and other ioBroker-related modules. It supports:
The unit tests are realized using the following tools that are provided by this module:
ioBroker
's Objects and States DB by operating on Map
objects.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.
const path = require("path");
const { tests } = require("@iobroker/testing");
// Run tests
tests.packageFiles(path.join(__dirname, ".."));
// ~~~~~~~~~~~~~~~~~~~~~~~~~
// This should be the adapter's root directory
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");
// You can also mock external modules to create a more controlled environment during testing.
// Define the mocks as objects and include them below
const nobleMock = {
on() {},
state: "poweredOff",
}
// Run tests
tests.unit(path.join(__dirname, ".."), {
// ~~~~~~~~~~~~~~~~~~~~~~~~~
// This should be the adapter's root directory
// If the adapter may call process.exit during startup, define here which exit codes are allowed.
// By default, 0 is ok. Providing this option overrides the default.
// Make sure to include 0 if other exit codes are allowed aswell.
allowedExitCodes: [11],
// optionally define which modules should be mocked.
additionalMockedModules: {
"noble": nobleMock,
"@abandonware/noble": nobleMock,
}
// Define your own tests inside defineAdditionalTests
defineAdditionalTests() {
it("works", () => {
// see below how these could look like
});
}
});
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");
// Run tests
tests.integration(path.join(__dirname, ".."), {
// ~~~~~~~~~~~~~~~~~~~~~~~~~
// This should be the adapter's root directory
// If the adapter may call process.exit during startup, define here which exit codes are allowed.
// By default, termination during startup is not allowed.
allowedExitCodes: [11],
// Define your own tests inside defineAdditionalTests
// Since the tests are heavily instrumented, you need to create and use a so called "harness" to control the tests.
defineAdditionalTests(getHarness) {
describe("Test sendTo()", () => {
it("Should work", () => {
return new Promise(async (resolve) => {
// Create a fresh harness instance each test!
const harness = getHarness();
// Start the adapter and wait until it has started
await harness.startAdapterAndWait();
// Perform the actual test:
harness.sendTo("adapter.0", "test", "message", (resp) => {
console.dir(resp);
resolve();
});
});
});
})
}
});
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:
const { database, adapter } = utils.unit.createMocks();
This method creates a mock database and a mock adapter. See below for a more detailed description
const asserts = utils.unit.createAsserts(database, adapter);
This methods takes a mock database and adapter and creates 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.TODO
TODO
Here's an example how this can be used in a unit test. Note that this will not be the final syntax:
import { tests, utils } from "@iobroker/testing";
// Run tests
tests.unit(path.join(__dirname, ".."), {
// ~~~~~~~~~~~~~~~~~~~~~~~~~
// This should be the adapter's root directory
// Define your own tests inside defineAdditionalTests
defineAdditionalTests() {
// Create mocks and asserts
const { adapter, database } = utils.unit.createMocks();
const { assertObjectExists } = utils.unit.createAsserts(database, adapter);
describe("my test", () => {
afterEach(() => {
// The mocks keep track of all method invocations - reset them after each single test
adapter.resetMockHistory();
// We want to start each test with a fresh database
database.clear();
});
it("works", () => {
// Create an object in the fake db we will use in this test
const theObject: ioBroker.PartialObject = {
_id: "whatever",
type: "state",
common: {
role: "whatever",
},
};
mocks.database.publishObject(theObject);
// Do something that should be tested
// Assert that the object still exists
assertObjectExists(theObject._id);
});
});
}
});
FAQs
Shared utilities for adapter and module testing in ioBroker
The npm package @iobroker/testing receives a total of 5,672 weekly downloads. As such, @iobroker/testing popularity was classified as popular.
We found that @iobroker/testing demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.