How to generate unit tests from a JS module automatically?

≯℡__Kan透↙ 提交于 2020-07-08 20:40:40

问题


I have inherited a project with multiple JavaScript files. Each of them has a bunch of functions; the files are defined in AMD style.

For instance:

math.js

define([], function () {

  return {

    func1: function (a, b) { 
      return a + b;
    },

    func2: function (c, d) {
      return c + d;
    },

  };
});

I would like to generate a new file in the tests folder with the name (math.js) that will contain the boilerplate code for the unit tests in tdd style for the intern framework.

I have used the intern-generator, a Yeoman generator that will do scaffolding and generate a test file with a specified name and path, however, this one won't let me create unit tests that refer to the functions from the JS file.

So, for the math.js source file, I'd like to create a test file (automatically):

define(function (require) {
  var tdd = require('intern!tdd');
  var assert = require('intern/chai!assert');
  var math = require('src/app/math');

  tdd.suite('Suite name', function () {

    tdd.test1('Test foo', function () {
      math.func1('breaks');
    });

    tdd.test('Test bar', function () {
      math.func2('breaks');

    });
  });
});

This way, I can start with all unit tests files pre-created and I know that all my unit tests will initially break and I will make them pass one by one. Also, this way I can be sure that I got all functions from my source script files into tests.

Naturally, I would like to generate those tests .js files for all my source .js files. Is there any generator or a library/script that will let me do this?

来源:https://stackoverflow.com/questions/43492094/how-to-generate-unit-tests-from-a-js-module-automatically

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