NEWS
iobroker/testing error
-
I wrote an adapter, but the following error occurred during automated testing, I need a little help
"@iobroker/testing": "^1.2.5",The command "npm run test:package" exited with 0. 1.37s$ npm run test:unit > iobroker.serve@0.0.1 test:unit /home/travis/build/smarthomefans/ioBroker.serve > mocha test/unit --exit Test the adapter (in a mocked environment) 1) The adapter starts in normal mode 2) The adapter starts in compact mode 0 passing (78ms) 2 failing 1) Test the adapter (in a mocked environment) The adapter starts in normal mode: Error: Cannot find module '/home/travis/build/smarthomefans/iobroker.js-controller/lib/tools.js' at loadModuleInternal (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:166:26) at Module.fakeRequire [as require] (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:36:16) at require (internal/module.js:20:19) at startAdapter (main.js:36:58) at __dirname (main.js:151:2) at Object.<anonymous> (main.js:153:3) at Module.module._compile (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:158:24) at Object.replaceJsLoader (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:162:9) at require (internal/module.js:20:19) at loadModuleInternal (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:166:26) at Object.loadModuleInHarness (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:185:12) at Object.<anonymous> (node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:79:45) at next (native) at /home/travis/build/smarthomefans/ioBroker.serve/node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:8:71 at __awaiter (node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:4:12) at Object.startMockAdapter (node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:36:12) at Context.<anonymous> (node_modules/@iobroker/testing/build/tests/unit/index.js:65:83) at next (native) at /home/travis/build/smarthomefans/ioBroker.serve/node_modules/@iobroker/testing/build/tests/unit/index.js:9:71 at __awaiter (node_modules/@iobroker/testing/build/tests/unit/index.js:5:12) at Context.<anonymous> (node_modules/@iobroker/testing/build/tests/unit/index.js:61:20) 2) Test the adapter (in a mocked environment) The adapter starts in compact mode: Error: Cannot find module '/home/travis/build/smarthomefans/iobroker.js-controller/lib/tools.js' at loadModuleInternal (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:166:26) at Module.fakeRequire [as require] (node_modules/@iobroker/testing/build/tests/unit/harness/loader.js:36:16) at require (internal/module.js:20:19) at startAdapter (main.js:36:58) at Object.<anonymous> (node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:89:17) at next (native) at /home/travis/build/smarthomefans/ioBroker.serve/node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:8:71 at __awaiter (node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:4:12) at Object.startMockAdapter (node_modules/@iobroker/testing/build/tests/unit/harness/startMockAdapter.js:36:12) at Context.<anonymous> (node_modules/@iobroker/testing/build/tests/unit/index.js:80:105) at next (native) at /home/travis/build/smarthomefans/ioBroker.serve/node_modules/@iobroker/testing/build/tests/unit/index.js:9:71 at __awaiter (node_modules/@iobroker/testing/build/tests/unit/index.js:5:12) at Context.<anonymous> (node_modules/@iobroker/testing/build/tests/unit/index.js:76:24) npm ERR! Linux 4.4.0-101-generic npm ERR! argv "/home/travis/.nvm/versions/node/v6.17.1/bin/node" "/home/travis/.nvm/versions/node/v6.17.1/bin/npm" "run" "test:unit" npm ERR! node v6.17.1 npm ERR! npm v3.10.10 npm ERR! code ELIFECYCLE npm ERR! iobroker.serve@0.0.1 test:unit: `mocha test/unit --exit` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the iobroker.serve@0.0.1 test:unit script 'mocha test/unit --exit'. npm ERR! Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem with the iobroker.serve package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! mocha test/unit --exit npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs iobroker.serve npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm owner ls iobroker.serve npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! /home/travis/build/smarthomefans/ioBroker.serve/npm-debug.log The command "npm run test:unit" exited with 1. -
@孙善明 If you are relying on files from js-controller (like
tools.js), you need to mock the methods in order to use them in unit tests. Remember, those don't have a real js-controller in the background.
This is documented here: https://github.com/ioBroker/testing#adapter-startup-unit-test (in the middle of the code block). You would have to mockgetDefaultDataDir, like this:"{CONTROLLER_DIR}/lib/tools.js": { getDefaultDataDir() { return "../../iobroker-data"; } },(I'm not 100% sure about the actual return value).
-
I found the problem here.
* @param {Partial<ioBroker.AdapterOptions>} [options] */ function startAdapter(options) { // Create the adapter and define its methods dataDir = path.normalize(path.join(utils.controllerDir, require(path.join(utils.controllerDir, 'lib', 'tools.js')).getDefaultDataDir())); return adapter = utils.adapter(Object.assign({}, options, { name: "serve", // The ready callback is called when databases are connected and adapter received configuration. // start here! ready: main, // Main method defined below for readability // is called when adapter shuts down - callback has to be called under any circumstances! unload: (callback) => { try { adapter.log.info("cleaned everything up..."); callback(); } catch (e) { callback(); } }, -
@孙善明 If you are relying on files from js-controller (like
tools.js), you need to mock the methods in order to use them in unit tests. Remember, those don't have a real js-controller in the background.
This is documented here: https://github.com/ioBroker/testing#adapter-startup-unit-test (in the middle of the code block). You would have to mockgetDefaultDataDir, like this:"{CONTROLLER_DIR}/lib/tools.js": { getDefaultDataDir() { return "../../iobroker-data"; } },(I'm not 100% sure about the actual return value).
Hey! Du scheinst an dieser Unterhaltung interessiert zu sein, hast aber noch kein Konto.
Hast du es satt, bei jedem Besuch durch die gleichen Beiträge zu scrollen? Wenn du dich für ein Konto anmeldest, kommst du immer genau dorthin zurück, wo du zuvor warst, und kannst dich über neue Antworten benachrichtigen lassen (entweder per E-Mail oder Push-Benachrichtigung). Du kannst auch Lesezeichen speichern und Beiträge positiv bewerten, um anderen Community-Mitgliedern deine Wertschätzung zu zeigen.
Mit deinem Input könnte dieser Beitrag noch besser werden 💗
Registrieren Anmelden