@iobroker/testing 5.0.0 → 5.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/build/index.d.ts +1 -1
  4. package/build/lib/adapterTools.d.ts +7 -1
  5. package/build/lib/adapterTools.js +36 -21
  6. package/build/lib/executeCommand.d.ts +3 -2
  7. package/build/lib/executeCommand.js +30 -29
  8. package/build/lib/str2regex.js +4 -4
  9. package/build/tests/index.d.ts +9 -9
  10. package/build/tests/integration/index.d.ts +1 -1
  11. package/build/tests/integration/index.js +44 -39
  12. package/build/tests/integration/lib/adapterSetup.d.ts +1 -1
  13. package/build/tests/integration/lib/adapterSetup.js +50 -50
  14. package/build/tests/integration/lib/controllerSetup.d.ts +1 -10
  15. package/build/tests/integration/lib/controllerSetup.js +59 -56
  16. package/build/tests/integration/lib/dbConnection.d.ts +12 -12
  17. package/build/tests/integration/lib/dbConnection.js +71 -69
  18. package/build/tests/integration/lib/harness.d.ts +9 -7
  19. package/build/tests/integration/lib/harness.js +68 -49
  20. package/build/tests/integration/lib/logger.js +1 -1
  21. package/build/tests/integration/lib/tools.d.ts +6 -1
  22. package/build/tests/integration/lib/tools.js +27 -12
  23. package/build/tests/packageFiles/index.js +92 -90
  24. package/build/tests/unit/harness/createMocks.d.ts +3 -2
  25. package/build/tests/unit/harness/createMocks.js +0 -1
  26. package/build/tests/unit/index.js +5 -4
  27. package/build/tests/unit/mocks/mockAdapter.d.ts +3 -3
  28. package/build/tests/unit/mocks/mockAdapter.js +196 -162
  29. package/build/tests/unit/mocks/mockAdapterCore.d.ts +4 -9
  30. package/build/tests/unit/mocks/mockAdapterCore.js +23 -14
  31. package/build/tests/unit/mocks/mockDatabase.d.ts +13 -12
  32. package/build/tests/unit/mocks/mockDatabase.js +39 -43
  33. package/build/tests/unit/mocks/mockLogger.d.ts +1 -1
  34. package/build/tests/unit/mocks/mockLogger.js +1 -1
  35. package/build/tests/unit/mocks/tools.d.ts +2 -2
  36. package/build/tests/unit/mocks/tools.js +14 -11
  37. package/package.json +15 -22
@@ -15,25 +15,34 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  Object.defineProperty(exports, "__esModule", { value: true });
26
36
  exports.mockAdapterCore = mockAdapterCore;
27
37
  const os = __importStar(require("os"));
28
38
  const path = __importStar(require("path"));
29
39
  const mockAdapter_1 = require("./mockAdapter");
30
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
31
40
  function mockAdapterCore(database, options = {}) {
32
41
  /**
33
42
  * The root directory of JS-Controller
34
43
  * If this has to exist in the test, the user/tester has to take care of it!
35
44
  */
36
- const controllerDir = path.join(options.adapterDir || "", "..", "iobroker.js-controller");
45
+ const controllerDir = path.join(options.adapterDir || '', '..', 'iobroker.js-controller');
37
46
  const dataDir = path.join(os.tmpdir(), `test-iobroker-data`);
38
47
  /**
39
48
  * The test location for iobroker-data
@@ -55,14 +64,14 @@ function mockAdapterCore(database, options = {}) {
55
64
  }
56
65
  const AdapterConstructor = function (nameOrOptions) {
57
66
  // This needs to be a class with the correct `this` context or the ES6 tests won't work
58
- if (!(this instanceof AdapterConstructor))
67
+ if (!(this instanceof AdapterConstructor)) {
59
68
  return new AdapterConstructor(nameOrOptions);
60
- const createAdapterMockOptions = typeof nameOrOptions === "string"
61
- ? { name: nameOrOptions }
62
- : nameOrOptions;
69
+ }
70
+ const createAdapterMockOptions = typeof nameOrOptions === 'string' ? { name: nameOrOptions } : nameOrOptions;
63
71
  mockAdapter_1.createAdapterMock.bind(this)(database, createAdapterMockOptions);
64
- if (typeof options.onAdapterCreated === "function")
72
+ if (typeof options.onAdapterCreated === 'function') {
65
73
  options.onAdapterCreated(this);
74
+ }
66
75
  return this;
67
76
  };
68
77
  return {
@@ -1,4 +1,4 @@
1
- import type { MockAdapter } from "./mockAdapter";
1
+ import type { MockAdapter } from './mockAdapter';
2
2
  /**
3
3
  * A minimalistic version of ioBroker's Objects and States DB that just operates on a Map
4
4
  */
@@ -33,19 +33,20 @@ export declare class MockDatabase {
33
33
  /**
34
34
  * Returns a collection of predefined assertions to be used in unit tests
35
35
  * Those include assertions for:
36
- * * State exists
37
- * * State has a certain value, ack flag, object property
38
- * * Object exists
39
- * * Object has a certain common or native part
36
+ * - State exists
37
+ * - State has a certain value, ack flag, object property
38
+ * - Object exists
39
+ * - Object has a certain common or native part
40
+ *
40
41
  * @param db The mock database to operate on
41
42
  * @param adapter The mock adapter to operate on
42
43
  */
43
44
  export declare function createAsserts(db: MockDatabase, adapter: MockAdapter): {
44
- assertObjectExists(id: string | string[]): void;
45
- assertStateExists(id: string | string[]): void;
46
- assertStateHasValue(id: string | string[], value: any): void;
47
- assertStateIsAcked(id: string | string[], ack?: boolean): void;
48
- assertStateProperty(id: string | string[], property: string, value: any): void;
49
- assertObjectCommon(id: string | string[], common: ioBroker.ObjectCommon): void;
50
- assertObjectNative(id: string | string[], native: Record<string, any>): void;
45
+ assertObjectExists: (id: string | string[]) => void;
46
+ assertStateExists: (id: string | string[]) => void;
47
+ assertStateHasValue: (id: string | string[], value: any) => void;
48
+ assertStateIsAcked: (id: string | string[], ack: boolean) => void;
49
+ assertStateProperty: (id: string | string[], property: string, value: any) => void;
50
+ assertObjectCommon: (id: string | string[], common: ioBroker.ObjectCommon) => void;
51
+ assertObjectNative: (id: string | string[], native: Record<string, any>) => void;
51
52
  };
@@ -2,12 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MockDatabase = void 0;
4
4
  exports.createAsserts = createAsserts;
5
+ // @ts-expect-error no types
5
6
  const objects_1 = require("alcalzone-shared/objects");
6
- const typeguards_1 = require("alcalzone-shared/typeguards");
7
7
  const str2regex_1 = require("../../../lib/str2regex");
8
8
  const objectTemplate = Object.freeze({
9
- type: "state",
10
- common: { name: "an object" },
9
+ type: 'state',
10
+ common: { name: 'an object' },
11
11
  native: {},
12
12
  });
13
13
  const stateTemplate = Object.freeze({
@@ -33,10 +33,12 @@ class MockDatabase {
33
33
  this.clearStates();
34
34
  }
35
35
  publishObject(obj) {
36
- if (obj._id == null)
37
- throw new Error("An object must have an ID");
38
- if (obj.type == null)
39
- throw new Error("An object must have a type");
36
+ if (obj._id == null) {
37
+ throw new Error('An object must have an ID');
38
+ }
39
+ if (obj.type == null) {
40
+ throw new Error('An object must have a type');
41
+ }
40
42
  const completeObject = (0, objects_1.extend)({}, objectTemplate, obj);
41
43
  this.objects.set(obj._id, completeObject);
42
44
  }
@@ -44,22 +46,16 @@ class MockDatabase {
44
46
  objects.forEach(this.publishObject.bind(this));
45
47
  }
46
48
  publishStateObjects(...objects) {
47
- objects
48
- .map((obj) => (0, objects_1.extend)({}, obj, { type: "state" }))
49
- .forEach(this.publishObject.bind(this));
49
+ objects.map(obj => (0, objects_1.extend)({}, obj, { type: 'state' })).forEach(this.publishObject.bind(this));
50
50
  }
51
51
  publishChannelObjects(...objects) {
52
- objects
53
- .map((obj) => (0, objects_1.extend)({}, obj, { type: "channel" }))
54
- .forEach(this.publishObject.bind(this));
52
+ objects.map(obj => (0, objects_1.extend)({}, obj, { type: 'channel' })).forEach(this.publishObject.bind(this));
55
53
  }
56
54
  publishDeviceObjects(...objects) {
57
- objects
58
- .map((obj) => (0, objects_1.extend)({}, obj, { type: "device" }))
59
- .forEach(this.publishObject.bind(this));
55
+ objects.map(obj => (0, objects_1.extend)({}, obj, { type: 'device' })).forEach(this.publishObject.bind(this));
60
56
  }
61
57
  deleteObject(objOrID) {
62
- this.objects.delete(typeof objOrID === "string" ? objOrID : objOrID._id);
58
+ this.objects.delete(typeof objOrID === 'string' ? objOrID : objOrID._id);
63
59
  }
64
60
  publishState(id, state) {
65
61
  // if (typeof id !== "string") throw new Error("The id must be given!");
@@ -79,37 +75,38 @@ class MockDatabase {
79
75
  }
80
76
  }
81
77
  hasObject(namespaceOrId, id) {
82
- id = namespaceOrId + (id ? "." + id : "");
78
+ id = namespaceOrId + (id ? `.${id}` : '');
83
79
  return this.objects.has(id);
84
80
  }
85
81
  getObject(namespaceOrId, id) {
86
82
  // combines getObject and getForeignObject into one
87
- id = namespaceOrId + (id ? "." + id : "");
83
+ id = namespaceOrId + (id ? `.${id}` : '');
88
84
  return this.objects.get(id);
89
85
  }
90
86
  hasState(namespaceOrId, id) {
91
- id = namespaceOrId + (id ? "." + id : "");
87
+ id = namespaceOrId + (id ? `.${id}` : '');
92
88
  return this.states.has(id);
93
89
  }
94
90
  getState(namespaceOrId, id) {
95
91
  // combines getObject and getForeignObject into one
96
- id = namespaceOrId + (id ? "." + id : "");
92
+ id = namespaceOrId + (id ? `.${id}` : '');
97
93
  return this.states.get(id);
98
94
  }
99
- getObjects(namespaceOrPattern, patternOrType, type) {
95
+ getObjects(namespaceOrPattern,
96
+ // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
97
+ patternOrType, type) {
100
98
  // combines getObjects and getForeignObjects into one
101
99
  let pattern;
102
100
  if (type != null) {
103
- pattern =
104
- namespaceOrPattern + (patternOrType ? "." + patternOrType : "");
101
+ pattern = namespaceOrPattern + (patternOrType ? `.${patternOrType}` : '');
105
102
  }
106
103
  else if (patternOrType != null) {
107
- if (["state", "channel", "device"].indexOf(patternOrType) > -1) {
104
+ if (['state', 'channel', 'device'].indexOf(patternOrType) > -1) {
108
105
  type = patternOrType;
109
106
  pattern = namespaceOrPattern;
110
107
  }
111
108
  else {
112
- pattern = namespaceOrPattern + "." + patternOrType;
109
+ pattern = `${namespaceOrPattern}.${patternOrType}`;
113
110
  }
114
111
  }
115
112
  else {
@@ -130,21 +127,22 @@ exports.MockDatabase = MockDatabase;
130
127
  /**
131
128
  * Returns a collection of predefined assertions to be used in unit tests
132
129
  * Those include assertions for:
133
- * * State exists
134
- * * State has a certain value, ack flag, object property
135
- * * Object exists
136
- * * Object has a certain common or native part
130
+ * - State exists
131
+ * - State has a certain value, ack flag, object property
132
+ * - Object exists
133
+ * - Object has a certain common or native part
134
+ *
137
135
  * @param db The mock database to operate on
138
136
  * @param adapter The mock adapter to operate on
139
137
  */
140
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
141
138
  function createAsserts(db, adapter) {
142
139
  function normalizeID(id) {
143
- if ((0, typeguards_1.isArray)(id))
144
- id = id.join(".");
140
+ if (Array.isArray(id)) {
141
+ id = id.join('.');
142
+ }
145
143
  // Test if this ID is fully qualified
146
144
  if (!/^[a-z0-9\-_]+\.\d+\./.test(id)) {
147
- id = adapter.namespace + "." + id;
145
+ id = `${adapter.namespace}.${id}`;
148
146
  }
149
147
  return id;
150
148
  }
@@ -158,31 +156,29 @@ function createAsserts(db, adapter) {
158
156
  db.hasState(id).should.equal(true, `The state "${adapter.namespace}.${id}" does not exist but it was expected to!`);
159
157
  },
160
158
  assertStateHasValue(id, value) {
161
- ret.assertStateProperty(id, "val", value);
159
+ ret.assertStateProperty(id, 'val', value);
162
160
  },
163
161
  assertStateIsAcked(id, ack = true) {
164
- ret.assertStateProperty(id, "ack", ack);
162
+ ret.assertStateProperty(id, 'ack', ack);
165
163
  },
166
164
  assertStateProperty(id, property, value) {
167
165
  id = normalizeID(id);
168
166
  ret.assertStateExists(id);
169
- db.getState(id)
170
- .should.be.an("object")
171
- .that.has.property(property, value);
167
+ db.getState(id).should.be.an('object').that.has.property(property, value);
172
168
  },
173
169
  assertObjectCommon(id, common) {
174
170
  id = normalizeID(id);
175
171
  ret.assertObjectExists(id);
176
172
  const dbObj = db.getObject(id);
177
- dbObj.should.be.an("object").that.has.property("common");
178
- dbObj.common.should.be.an("object").that.nested.include(common);
173
+ dbObj.should.be.an('object').that.has.property('common');
174
+ dbObj.common.should.be.an('object').that.nested.include(common);
179
175
  },
180
176
  assertObjectNative(id, native) {
181
177
  id = normalizeID(id);
182
178
  ret.assertObjectExists(id);
183
179
  const dbObj = db.getObject(id);
184
- dbObj.should.be.an("object").that.has.property("native");
185
- dbObj.native.should.be.an("object").that.nested.include(native);
180
+ dbObj.should.be.an('object').that.has.property('native');
181
+ dbObj.native.should.be.an('object').that.nested.include(native);
186
182
  },
187
183
  };
188
184
  return ret;
@@ -1,4 +1,4 @@
1
- import { Mock } from "./tools";
1
+ import { type Mock } from './tools';
2
2
  export type MockLogger = Mock<ioBroker.Logger> & {
3
3
  resetMock(): void;
4
4
  resetMockHistory(): void;
@@ -16,7 +16,7 @@ function createLoggerMock() {
16
16
  error: (0, sinon_1.stub)(),
17
17
  debug: (0, sinon_1.stub)(),
18
18
  silly: (0, sinon_1.stub)(),
19
- level: "info",
19
+ level: 'info',
20
20
  // Mock-specific methods
21
21
  resetMockHistory() {
22
22
  // reset Logger
@@ -1,4 +1,4 @@
1
- import type { Equals, Overwrite } from "alcalzone-shared/types";
1
+ import type { Equals, Overwrite } from 'alcalzone-shared/types';
2
2
  export type IsAny<T> = Equals<T extends never ? false : true, boolean>;
3
3
  export type MockableMethods<T, All = Required<T>, NoAny = {
4
4
  [K in keyof All]: IsAny<All[K]> extends true ? never : All[K] extends (...args: any[]) => void ? K : never;
@@ -9,4 +9,4 @@ export type Mock<T extends {}> = Overwrite<T, {
9
9
  export declare function doResetHistory(parent: Record<string, any>): void;
10
10
  export declare function doResetBehavior(parent: Record<string, any>, implementedMethods: Record<string, any>): void;
11
11
  export declare function stubAndPromisifyImplementedMethods<T extends string>(parent: Record<T, any>, implementedMethods: Partial<Record<T, any>>, allowUserOverrides?: T[]): void;
12
- export type ImplementedMethodDictionary<T> = Partial<Record<MockableMethods<T>, "none" | "normal" | "no error">>;
12
+ export type ImplementedMethodDictionary<T> = Partial<Record<MockableMethods<T>, 'none' | 'normal' | 'no error'>>;
@@ -8,47 +8,50 @@ const sinon_1 = require("sinon");
8
8
  function doResetHistory(parent) {
9
9
  for (const prop of Object.keys(parent)) {
10
10
  const val = parent[prop];
11
- if (val && typeof val.resetHistory === "function")
11
+ if (val && typeof val.resetHistory === 'function') {
12
12
  val.resetHistory();
13
+ }
13
14
  }
14
15
  }
15
16
  function doResetBehavior(parent, implementedMethods) {
16
17
  for (const prop of Object.keys(parent)) {
17
- if (prop in implementedMethods ||
18
- (prop.endsWith("Async") && prop.slice(0, -5) in implementedMethods))
18
+ if (prop in implementedMethods || (prop.endsWith('Async') && prop.slice(0, -5) in implementedMethods)) {
19
19
  continue;
20
+ }
20
21
  const val = parent[prop];
21
- if (val && typeof val.resetBehavior === "function")
22
+ if (val && typeof val.resetBehavior === 'function') {
22
23
  val.resetBehavior();
24
+ }
23
25
  }
24
26
  }
25
27
  function dontOverwriteThis() {
26
- throw new Error("You must not overwrite the behavior of this stub!");
28
+ throw new Error('You must not overwrite the behavior of this stub!');
27
29
  }
28
30
  function stubAndPromisifyImplementedMethods(parent, implementedMethods, allowUserOverrides = []) {
29
31
  // The methods implemented above are no stubs, but we claimed they are
30
32
  // Therefore hook them up with a real stub
31
33
  for (const methodName of Object.keys(implementedMethods)) {
32
- if (methodName.endsWith("Async"))
34
+ if (methodName.endsWith('Async')) {
33
35
  continue;
36
+ }
34
37
  const originalMethod = parent[methodName];
35
38
  const callbackFake = (parent[methodName] = (0, sinon_1.stub)());
36
39
  callbackFake.callsFake(originalMethod);
37
40
  // Prevent the user from changing the stub's behavior
38
- if (allowUserOverrides.indexOf(methodName) === -1) {
41
+ if (!allowUserOverrides.includes(methodName)) {
39
42
  callbackFake.returns = dontOverwriteThis;
40
43
  callbackFake.callsFake = dontOverwriteThis;
41
44
  }
42
45
  // Construct the async fake if there's any
43
46
  const asyncType = implementedMethods[methodName];
44
- if (asyncType === "none")
47
+ if (asyncType === 'none') {
45
48
  continue;
46
- const promisifyMethod = asyncType === "no error" ? async_1.promisifyNoError : async_1.promisify;
49
+ }
50
+ const promisifyMethod = asyncType === 'no error' ? async_1.promisifyNoError : async_1.promisify;
47
51
  const asyncFake = (0, sinon_1.stub)().callsFake(promisifyMethod(originalMethod, parent));
48
52
  parent[`${methodName}Async`] = asyncFake;
49
53
  // Prevent the user from changing the stub's behavior
50
- if (allowUserOverrides.indexOf(methodName) === -1 ||
51
- allowUserOverrides.indexOf((methodName + "Async")) === -1) {
54
+ if (!allowUserOverrides.includes(methodName) || !allowUserOverrides.includes(`${methodName}Async`)) {
52
55
  asyncFake.returns = dontOverwriteThis;
53
56
  asyncFake.callsFake = dontOverwriteThis;
54
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/testing",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "description": "Shared utilities for adapter and module testing in ioBroker",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "lint:ts": "eslint -c eslint.config.mjs",
19
19
  "lint": "npm run lint:ts",
20
20
  "release": "release-script",
21
- "update-packages": "ncu --upgrade"
21
+ "update-packages": "npx -y npm-check-updates --upgrade"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",
@@ -41,37 +41,30 @@
41
41
  "devDependencies": {
42
42
  "@alcalzone/release-script": "^3.8.0",
43
43
  "@alcalzone/release-script-plugin-license": "^3.7.0",
44
- "@eslint/js": "^9.10.0",
45
- "@eslint/eslintrc": "^3.1.0",
46
- "@iobroker/adapter-core": "^3.1.6",
47
- "@iobroker/types": "^6.0.11",
48
- "@tsconfig/node14": "^14.1.2",
49
- "@types/chai": "^4.3.19",
50
- "@types/chai-as-promised": "^8.0.0",
44
+ "@iobroker/adapter-core": "^3.2.3",
45
+ "@iobroker/eslint-config": "^1.0.0",
46
+ "@iobroker/types": "^7.0.6",
47
+ "@tsconfig/node16": "^16.1.3",
48
+ "@types/chai": "^4.3.20",
49
+ "@types/chai-as-promised": "^8.0.1",
51
50
  "@types/debug": "4.1.12",
52
51
  "@types/fs-extra": "^11.0.4",
53
- "@types/mocha": "^10.0.8",
54
- "@types/node": "^22.5.5",
52
+ "@types/mocha": "^10.0.10",
53
+ "@types/node": "^22.10.7",
55
54
  "@types/sinon": "^17.0.3",
56
55
  "@types/sinon-chai": "^3.2.12",
57
- "@typescript-eslint/eslint-plugin": "^8.5.0",
58
- "@typescript-eslint/parser": "^8.5.0",
59
- "eslint": "^9.10.0",
60
- "eslint-config-prettier": "^9.1.0",
61
- "eslint-plugin-prettier": "^5.2.1",
62
- "prettier": "^3.3.3",
63
56
  "rimraf": "^6.0.1",
64
57
  "source-map-support": "^0.5.21",
65
58
  "ts-node": "^10.9.2",
66
- "typescript": "~5.6.2"
59
+ "typescript": "~5.7.3"
67
60
  },
68
61
  "dependencies": {
69
- "alcalzone-shared": "~4.0.8",
62
+ "alcalzone-shared": "~5.0.0",
70
63
  "chai": "^4.5.0",
71
64
  "chai-as-promised": "^7.1.2",
72
- "debug": "^4.3.7",
73
- "fs-extra": "^11.2.0",
74
- "mocha": "^10.7.3",
65
+ "debug": "^4.4.0",
66
+ "fs-extra": "^11.3.0",
67
+ "mocha": "^11.0.1",
75
68
  "sinon": "^19.0.2",
76
69
  "sinon-chai": "^3.7.0"
77
70
  }