1

我正在使用 Webpack 来捆绑我的 Angular js 代码。但是我无法编写基于 mocha 的单元测试用例 bcz gulp-mocha 未能“要求”css。以下是详细的代码片段。

Benefits.js 角度 js 指令:

var Utilities = require("./../../../../../utilities/Utilities");
var AppUtilities = require("./../../../../../utilities/AppUtilities");
require("./../../../../css/benefits.css");
module.exports = {
    getBenefits: function ($sce) {
        return {
            restrict: "E",
            template: require('html!./../../../../templates/page/Benefits.html'),
            replace: true,
            scope: {
                benefits: '=',
                productPageBenefits: '=',
                divider: '='
            },
            link: function (scope, element, attrs) {

                //get benefit details as per given list of key
                scope.getBenefitsList = function (benefitKeyList, benefits) {
                    return OpensiteUtilities.getBenefitsList(benefitKeyList, benefits);
                };

            }
        };
    }
};

Benefits_test.js 是单元测试用例文件:

describe('The Benefits', function () {
    var Benefits = require('../../../../../../../../application/client-src/js/application/directives/productpage/Benefits.js');

    beforeEach(function () {

    });

    it('Should be an Object', function () {
        Benefits.should.be.a('Object');
        expect(Benefits).to.have.property('getBenefits');       
    });

});

我很高兴跟随错误

/my/path/application/client-src/css/benefits.css:1
(function (exports, require, module, __filename, __dirname) { @media (min-widt
                                                              ^
[16:18:49] { [SyntaxError: Unexpected token ILLEGAL]
  name: 'SyntaxError',
  message: 'Unexpected token ILLEGAL',
  stack: 'SyntaxError: Unexpected token ILLEGAL\n    at Module._compile (module.js:439:25)\n    at Object.Module._extensions..js (module.js:474:10)\n    at Module.load (module.js:356:32)\n    at Function.Module._load (module.js:312:12)\n    at Module.require (module.js:364:17)\n    at require (module.js:380:17)\n    at Object.<anonymous> (/my/path/application/client-src/js/application/directives/productpage/Benefits.js:9:1)\n    at Module._compile (module.js:456:26)\n    at Object.Module._extensions..js (module.js:474:10)\n    at Module.load (module.js:356:32)\n    at Function.Module._load (module.js:312:12)\n    at Module.require (module.js:364:17)\n    at require (module.js:380:17)\n    at Suite.<anonymous> (/my/path/test/application/client-src/js/application/directives/productpage/Benefits_test.js:20:20)\n    at context.describe.context.context (/my/path/node_modules/jumpstart-engine/node_modules/gulp-mocha/node_modules/mocha/lib/interfaces/bdd.js:75:10)\n    at Object.<anonymous> (/my/path/test/application/client-src/js/application/directives/productpage/Benefits_test.js:19:1)',
  showStack: false,
  showProperties: true,
  plugin: 'gulp-mocha' }

我知道它失败的 bcz gulp-mocha 无法理解“css”文件。我如何编写测试用例?require(./../benefits.css)我的指令代码之外是否有移动“ ”?

我想将所有依赖项保留在相同的指令代码中(使用 webpack)。请帮忙

4

1 回答 1

1

一种选择是通过mocha-loader触发 Mocha并将 CSS 设置为通过null-loader 加载。那应该可以解决require您遇到的问题。

如果您想在 Webpack 之外解决它,则需要以require某种方式进行修补。我希望在这种情况下你会想要使用类似proxyquiremockeryrewire的东西。

于 2015-06-19T08:43:27.567 回答