问题
Is it possible to create and run test suites for custom client-side JavaScript, not created as Node modules? How should the configuration be altered then?
There is loader section in the Intern configuration, specifying the packages that will be loaded, if I get it right. Is it necessary to include my custom JS code here somehow?
Where shall I put my test suites and how should they look like, when they are not belonging to any Node package, only to my custom JS code, again. In other words, how will I "load" and run them?
I have tried to get it work in some way, whereas I am not experienced with Node. My attempts resulted like this:
$ node client.js config=tests/js/intern
Defaulting to "console" reporter
回答1:
1. Your test modules will always need to be written as AMD modules, but you can test any client-side code you want. Within your test module, just specify the non-AMD code as a dependency like any other module, then access the global variables that your script creates:
define([
'intern!tdd',
'intern/chai!assert',
'intern/order!myPackage/myFoo.js'
], function (tdd, assert /* note, no assignment here */) {
tdd.suite('foo suite', function () {
tdd.test('something in foo', function () {
// accessing a global variable created by `myPackage/myFoo.js`
assert.ok(window.myFoo, 'Global myFoo object should exist');
});
});
});
2. You don’t need to do anything explicit in the Intern configuration in order for this to work, except to specify your test module in the suites
array.
3. The recommended directory structure for a client-side application needing testing would look something like this:
/ - your entire application
src/
index.html - your app’s entry-point HTML
app/ - your app package, containing application-specific JavaScript to be tested
foo.js - Some module `foo`
tests/ - Intern tests for `app`
foo.js - Tests for `foo`
intern.js - Intern configuration for `app`
node_modules/
intern/
This, of course, will vary depending upon how your application is already architected, but from a “starting from scratch” approach, this is our generally recommended directory structure.
来源:https://stackoverflow.com/questions/16325697/testing-custom-javascript-not-node-module-with-intern