我正在使用Webpack来修改我的角度js代码。但是我无法编写基于mocha的单元测试用例bcz gulp-mocha未能“需要”css。以下是有关详细信息的代码段。
Benefits.js angular 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)。 请帮忙
答案 0 :(得分:1)
一种选择是通过mocha-loader触发Mocha并将CSS设置为通过null-loader加载。这应该可以解决您遇到的require
问题。
如果你想在Webpack之外解决它,你需要以某种方式修补require
。在这种情况下,我希望您能使用proxyquire,mockery或rewire之类的内容。