【问题标题】:Getting chai to play nice with requirejs让 chai 与 requirejs 一起玩得很好
【发布时间】:2015-01-22 15:44:42
【问题描述】:

我正在尝试将 Karma/Mocha/Chai 设置到我的 Backbone 项目中,该项目使用 requirejs,但运气不佳。

首先,这是我的设置:

- app/
  - js/
- bower_components/
- node_modules/
- test/
  - test-main.js
- karma.conf.js


// relevant bits of karma.conf.js
frameworks: ['mocha', 'requirejs', 'chai'],
files: [
    'test/test-main.js',
    {pattern: 'bower_components/requirejs-text/text.js', included: false},
    {pattern: 'bower_components/jquery/dist/jquery.js', included: false},
    {pattern: 'bower_components/underscore/underscore.js', included: false},
    {pattern: 'bower_components/backbone/backbone.js', included: false},
    {pattern: 'app/js/**/*.js', included: false},
    {pattern: 'test/**/*Spec.js', included: falase}
],
exclude: [ 'app/js/requireConfig.js', 'app/js/main.js' ],
preprocessors: { '**/*.html': [] },


// test-main.js
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$\i;
var pathToModules = function(path) {
    return path.replace(/^\/base\//, '').replace(/\.js$/, '');
}
Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.text(file)) {
        allTestFiles.push(pathToModule(file));
    }
});

require.config({
    baseUrl: '/base/app/js',
    paths: {
        text: '../../bower_components/requirejs-text/text',
        jquery: '../../bower_components/jquery/dist/jquery',
        underscore: '../../bower_components/underscore/underscore',
        backbone: '../../bower_components/backbone/backbone',
        test: '../../test',
    },
    deps: allTestFiles,
    callback: window.__karma__.start;
});

当我运行业力时,我得到:

错误:不匹配的匿名定义()模块:函数(模块){
-- text.js 的全部内容 --

我尝试将“框架”的顺序更改为frameworks: ['mocha', 'chai', requirejs'],这使得不匹配错误消失了,但随后得到:

TypeError: 'undefined' 不是一个对象(评估 'window.chai.should')

这是known issue,建议在chai之前保留requirejs

有没有人有让 requirejs-text 工作的经验?谢谢。

【问题讨论】:

标签: requirejs mocha.js karma-runner chai requirejs-text


【解决方案1】:

Welp,很抱歉在 SO 上浪费了一个问题。我从头开始,上面的配置(除了需要“{pattern: 'app/js/**/*.html', included: false}”)运行良好。

为了完整起见,这里是更新的配置:

- app/
  - js/
- bower_components/
- node_modules/
- test/
  - test-main.js
- karma.conf.js


// ---- relevant bits of karma.conf.js ----

// "requirejs" must come before "chai" or Chai will not load properly.
// Sidenote: Karma loads the listed frameworks backwards.
frameworks: ['mocha', 'requirejs', 'chai'],

// Contrary to what a few stackoverflow and github issue responses
// suggested, the order of files do not appear to matter at all.
files: [
  'test/test-main.js',

  // app files
  {pattern: 'app/js/**/*.html', included: false},
  {pattern: 'app/js/**/*.js', included: false},

  // tests
  {pattern: 'test/**/*Spec.js', included: false},

  // libraries
  {pattern: 'bower_components/jquery/dist/jquery.js', included:false, watching: false},
  {pattern: 'bower_components/underscore/underscore.js', included:false, watching: false},
  {pattern: 'bower_components/backbone/backbone.js', included:false, watching: false},
  {pattern: 'bower_components/requirejs-text/text.js', included:false, watching: false},
],

exclude: [ 'app/js/requireConfig.js', 'app/js/main.js' ],

preprocessors: {},


// ---- test-main.js ----
var allTestFiles = [];
for (var file in window.__karma__.files) {
    if (window.__karma__.files.hasOwnProperty(file)) {
        if (/Spec\.js$/.test(file)) {
            allTestFiles.push(file);
        }
    }
}

require.config({
    baseUrl: '/base/app/js',
    paths: {
        jquery: '../../bower_components/jquery/dist/jquery',
        underscore: '../../bower_components/underscore/underscore',
        backbone: '../../bower_components/backbone/backbone',
        text: '../../bower_components/requirejs-text/text',
    },
    deps: allTestFiles,
    callback: window.__karma__.start;
});

【讨论】:

  • 你的 package.json 看起来怎么样?
猜你喜欢
  • 2014-10-02
  • 2013-10-23
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 2012-11-27
  • 2015-04-30
  • 2011-03-16
相关资源
最近更新 更多