Discussion:
[nodejs] I cannot export jQuery correctly; testing Node.js with mocha and chai
EB
2018-04-29 10:23:21 UTC
Permalink
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`:

https://github.com/mskilab/gGnome.js/tree/add_tests

I'm having trouble with the functions in
https://github.com/mskilab/gGnome.js/blob/master/js/misc.js

with the tests here:
https://github.com/mskilab/gGnome.js/blob/add_tests/test/miscTest.js


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

https://github.com/mskilab/gGnome.js/blob/add_tests/js/misc.js#L2

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?
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/947925b2-69d3-4146-8c88-c561b5d6d925%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mikkel Wilson
2018-04-30 19:54:33 UTC
Permalink
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
https://www.oblivious.io/ <https://www.oblivious.io/?r=googlenodejs>
Post by EB
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...
https://github.com/mskilab/gGnome.js/tree/add_tests
I'm having trouble with the functions in
https://github.com/mskilab/gGnome.js/blob/master/js/misc.js
https://github.com/mskilab/gGnome.js/blob/add_tests/test/miscTest.js
https://github.com/mskilab/gGnome.js/blob/add_tests/js/misc.js#L2
const $ = require('jquery');
However, I'm still getting errors when I try to use the functions in
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?
--
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+***@googlegroups.com.
To post to this group, send email to ***@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.
Evan Biederstedt
2018-04-30 21:07:25 UTC
Permalink
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
Post by Mikkel Wilson
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.
npm install request --save
const request = require('request');
request(Misc.server+'/metadata', function(err, response, body){
if(err) {
console.log(err);
return;
}
console.log(body);
}
HTH,
Mikkel
https://www.oblivious.io/ <https://www.oblivious.io/?r=googlenodejs>
Post by EB
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...
https://github.com/mskilab/gGnome.js/tree/add_tests
I'm having trouble with the functions in
https://github.com/mskilab/gGnome.js/blob/master/js/misc.js
https://github.com/mskilab/gGnome.js/blob/add_tests/test/miscTest.js
https://github.com/mskilab/gGnome.js/blob/add_tests/js/misc.js#L2
const $ = require('jquery');
However, I'm still getting errors when I try to use the functions in
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?
--
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
To view this discussion on the web visit https://groups.google.com/d/
msgid/nodejs/76053819-61e2-4878-9f7b-2c44af693e31%40googlegroups.com
<https://groups.google.com/d/msgid/nodejs/76053819-61e2-4878-9f7b-2c44af693e31%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CABFhwYNFwiOEOvm1-WpYNSrQ2JfmxYmNQZGF2G%3DhDvUSJTqSAQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...