delliottg delliottg - 2 months ago 20
Node.js Question

Mocha assertion error: expected {} to be a string

I've been beating my head all morning on this and I'm pretty sure I'm just missing something simple. It appears I'm getting a new object ({}) for a return value when I'm expecting a string, but even if I hard code a string for a return value I get the same error.

I've worked through the examples found here with no trouble. My

package.json
is set to test properly (or at least I don't think that's the problem, but I can post it as well if it'll help troubleshoot my problem). I'm new-ish to Node.js (but well experienced with JS) & just learning Mocho & Chai.

What am I missing? Why am I getting what appears to be an empty object when I should be getting a string? What's causing the test to fail?

I've written a simple API to get the username from a host PC:

const username = require('username');
exports.getUserName = function() {
console.log(username.sync());
return username.sync();
};


And I've written a test using Mocha & Chai:

var expect = require("chai").expect;
var getUserName = require('./username.js');

describe("User name API", function () {
it("Returns a string with the user's name", function () {
expect(getUserName).to.be.a('string');
});
});


Here's the error that's returned when I run the test with
npm test
:

> sbserialwidget@0.0.1 test C:\deg\node_modules\sbSerialWidget
> mocha --reporter spec

running

User name API
1) Returns a string with the user's name

0 passing (13ms)
1 failing

1) User name API Returns a string with the user's name:
AssertionError: expected {} to be a string
at Context.<anonymous> (C:\deg\node_modules\sbSerialWidget\test\username.js:6:29)
at callFn (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runnable.js:334:21)
at Test.Runnable.run (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runnable.js:327:7)
at Runner.runTest (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:429:10)
at C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:535:12
at next (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:349:14)
at C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:359:7
at next (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:285:14)
at Immediate._onImmediate (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:327:5)


If I change the test to expect an object, the test works:
expect(getUserName).to.be.an('object');
.

However if I do a
console.log(typeof username.sync());
it says it's a string.

What do I need to do to fix this?




Edit for solution:

Here's the code that I eventually got to work. I think part of the problem was a path issue (I'm in a Windows environment), partly me simply not quite understanding what needed to be done, and finally me not understanding how to call the function properly in the test (see below).

Here's the modified
username.js
code:

const username = require('username');
exports.getUserName = function() {
console.log(username.sync());
return username.sync();
}


Here's the modified
usernametest.js
:

var expect = require("chai").expect;
//here's where one point of confusion was, I was trying to call the original getUserName()
//function, but it's been turned into a variable called username
var username = require('..\\js\\username.js').getUserName;

describe("User name API", function () {
it("returns a string with the user's name", function () {
//so here, instead of calling username.getUserName(), I call username()
//instead. Voila, it works...
expect(username()).to.be.a('string');
});
});

Answer

you are not executing the function

change

expect(getUserName).to.be.a('string');

to

expect(getUserName()).to.be.a('string');

edit

I din't figure out that your are exporting an object

exports.getUsername = function(){...}

should be

expect(getUserName.getUserName()).to.be.a('string');

thanks to @robertklep

Comments