i try to use the testing tool mocha in node. Consider the following test scenario
var requirejs = require('requirejs'); requirejs.config({ //Pass the top-level main.js/index.js require //function to requirejs so that node modules //are loaded relative to the top-level JS file. nodeRequire: require }); describe('Testing controller', function () { it('Should be pass', function (done) { (4).should.equal(4); done(); }); it('Should avoid name king', function (done) { requirejs(['../server/libs/validate_name'], function (validateName) { var err_test, accountExists_test, notAllow_test, available_test; validateName('anu', function (err, accountExists, notAllow, available) { accountExists.should.not.be.true; done(); }); }); }); });
as a testing result i have got:
$ make test ./node_modules/.bin/mocha \ --reporter list . Testing controller Should be pass: 0ms 1) Testing controller Should avoid name anu 1 passing (560 ms) 1 failing 1) Testing controller Should avoid name anu: Uncaught TypeError: Cannot read property 'should' of null at d:\townspeech\test\test.spec.js:23:30 at d:\townspeech\server\libs\validate_name.js:31:20 at d:\townspeech\test\test.spec.js:22:13 at Object.context.execCb (d:\townspeech\node_modules\requirejs\bin\r.js:1869:33) at Object.Module.check (d:\townspeech\node_modules\requirejs\bin\r.js:1105:51) at Object.Module.enable (d:\townspeech\node_modules\requirejs\bin\r.js:1376:22) at Object.Module.init (d:\townspeech\node_modules\requirejs\bin\r.js:1013:26) at null._onTimeout (d:\townspeech\node_modules\requirejs\bin\r.js:1646:36) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) make: *** [test] Error 1
The first pass without any complication but the second, it seems like, that could not attach module shouldjs. Why?
The "Cannot read property 'value' of null" error occurs when: trying to access the value property on a null value, e.g. after calling getElementById with an invalid identifier. inserting the JS script tag before the DOM elements have been declared.
To resolve your TypeError: Cannot read properties of undefined (reading '0') , go through these steps: Ensure you are using the correct variable. Perform a simple check on your variable before using it to make sure it is not undefined. Create a default value for the variable to use if it does happen to be undefined.
I had the same problem. I solved it by using:
(err === null).should.be.true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With