unit testing using JSTestDriver

126 views
Skip to first unread message

Reza Razavipour

unread,
May 8, 2014, 5:23:14 PM5/8/14
to nod...@googlegroups.com
I am trying to learn and start using automated testing and eventually TDD.
So I have some classes, plain old js code with no require. All the tests run and pass.
As you as I start testing a js file that has a require in it, tests fail with 
ReferenceError: Can't find variable: require

How do I overcome this? These tests run in the browser.

Angel Java Lopez

unread,
May 8, 2014, 7:10:37 PM5/8/14
to nod...@googlegroups.com
I'm use node.js server-side assert, or some test framework, like node-unit.

No problem with modules that have require.

Can you describe your test environment, workflow, in more detail? any example code?

Angel "Java" Lopez
@ajlopez



--
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+un...@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/049b3eb5-566f-447c-8a22-321b4ace14c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Rust

unread,
May 9, 2014, 12:35:11 AM5/9/14
to nod...@googlegroups.com
Assuming you're testing javascript that is intended to run in node (javascript with require()s in it), you should run the tests in node, not the browser. That said, it's possible to "browserify" your node javascript to get it to run in the browser, but it's better and simpler to test node javascript in node itself.

Some of the most popular browser-based test frameworks (qunit & jasmine) have node-based runners (jasmine-node and node-qunit). Or you could use a test framework that is more node-focused (like mocha or vows).

If you're writing cross-platform code that needs to run both in node and in the browser, you should run the tests in both environments to verify that both work as expected (personally I write mocha tests and use both their html reporter (browser) and their default "Dot Matrix" reporter (node)).

Aside: if you're interested in writing code that runs in both the browser and node, you can use browserify, which a lot of the people on this list use, or you can use a little boilerplate in your libraries (as the backbone & underscore libraries do) -- I wrote a little bit about this here: http://csnw.github.io/2013/06/23/this-in-node-modules-and-iifes.html.

Peter Rust

unread,
May 9, 2014, 12:42:21 AM5/9/14
to nod...@googlegroups.com
Reza,

Sorry, I just now noticed "JSTestDriver" in the subject. JSTestDriver is intended for browser javascript, not node javascript -- so your code either needs to not have any requires in it, or if it's built for node you need to "browserify" it before testing it, or if it's built for a browser-side require like RequireJS, you'll need to include that library with your tests (how exactly to do that with JSTestDriver is a question for their mailing list (https://groups.google.com/forum/#!forum/js-test-driver).

If you're trying to test node javascript, I would recommend switching from JSTestDriver to something focused on node, like mocha.

-- peter

Reza Razavipour

unread,
May 9, 2014, 2:34:05 PM5/9/14
to nod...@googlegroups.com
Thank you Peter, 

that is exactly where I have landed. I feel more confident that I have made the correct choice.

Reza
Reply all
Reply to author
Forward
0 new messages