I cannot export jQuery correctly; testing Node.js with mocha and chai

26 views
Skip to first unread message

EB

unread,
Apr 29, 2018, 5:16:34 PM4/29/18
to nodejs
I'm confused how to export jQuery correctly in order to test several functions with mocha and the assertion library chai. Hopefully this is a beginner's error...

Here is the code I am testing, with the branch `add_tests`:


I'm having trouble with the functions in 

with the tests here:


So, in order to export jQuery, I include the following line: 


    const $ = require('jquery');

However, I'm still getting errors when I try to use the functions in `miscTest.js`, whereby I'm running `mocha` or `npm test`, I get a TypeError:

    TypeError: $.ajax is not a function

This is from trying to run 

    console.log(Misc.metadata);

within `misc.js`

How do I correctly export jQuery so it's recognized? 

Mikkel Wilson

unread,
Apr 30, 2018, 4:39:34 PM4/30/18
to nodejs
So, this is a server-side javascript environment. jQuery is more suited to a client-side environment (in a browser, not on a server). I think you're trying to use jQuery to make a request to a /metadata endpoint on a server to get some JSON. I'd suggest you use the 'request' package instead.

First, install the package:

npm install request --save


Then, you can use it in your misc.js file:

const request = require('request');


Finally, you can replace your $.ajax call with request like this:

request(Misc.server+'/metadata', function(err, response, body){
 
if(err) {
    console
.log(err);
   
return;
 
}
  console
.log(body);
}

HTH,
Mikkel

Evan Biederstedt

unread,
Apr 30, 2018, 5:08:50 PM4/30/18
to nod...@googlegroups.com
Hi Mikkel

This is a good work around. I think you might be right---I may not have been thinking clearly about jquery. 

Thanks, Evan 

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscribe@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/76053819-61e2-4878-9f7b-2c44af693e31%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages