Testing custom JavaScript (not Node module) with Intern

爱⌒轻易说出口 提交于 2019-12-12 15:05:26

问题


  1. 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?

  2. 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?

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!