我们有一个基于 amd/requirejs 的网络应用程序。我们想在我们的代码中添加一个单元测试。谷歌搜索返回 squireJs 是最好的基于 amd-require 的模拟库。安装后,问题是我们无法解决 squirejs 依赖。这个问题看起来很基本,但我们尝试使用 Mocha 和 Jasmine 来使用它,尽管我们似乎忘记了配置:
我们要测试的 js 基本上看起来像:
define(function employeeAndTagsPage(require) {
var global = require('common/global');
var customMessageWindow = require('../../customMessageWindow/customMessageWindow');
var currentTab;
var navigate = null;
// and other BL
});
包.json:
{
"name": "jasmine.test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"devDependencies": {
"amd-loader": "0.0.8",
"jasmine-node": "^3.0.0",
"requirejs": "^2.3.6",
"squirejs": "^0.2.1"
}
}
Jasmine_examples PlayerSpec.js:
describe("Player", function () {
require('amd-loader');
var Player = require('../../lib/jasmine_examples/Player');
var Song = require('../../lib/jasmine_examples/Song');
var requirejs = require('requirejs');
var squire = requirejs('squirejs'); // here it fails
var player, song, mock;
beforeEach(function() {
player = new Player();
song = new Song();
mock = new squire();
});
//unit tesitng
});
我们得到的错误:
ReferenceError: requirejs is not defined
Stack:
at <Jasmine>
at getContext (c:\Synel\TESTS\eHarmonyNew-Jasmine\eHarmony\App\Test\node_modules\squirejs\src\Squire.js:58:5)
at Squire.configure (c:\Synel\TESTS\eHarmonyNew-Jasmine\eHarmony\App\Test\node_modules\squirejs\src\Squire.js:107:15)
可以在这里找到 squirejs 库代码:
https://github.com/iammerrick/Squire.js/blob/master/src/Squire.js
图书馆失败的原因是什么?我们忘记了什么?任何协助或推荐都将得到认可