javascript - mocha karma 测试运行程序出错

标签 javascript testing mocha.js karma-runner

我的目录结构是:

karma.conf.js
GruntFile.js
src
  -MyModule.js
lib
  -bar.js
test
  -MyModule.test.js
  -test-main.js

这是我的 karma.conf.js 文件

// Karma configuration
// Generated on Sat Aug 08 2015 08:09:40 GMT+0530 (IST)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'requirejs','chai'],


// list of files / patterns to load in the browser
files: [
  {pattern: 'lib/**/*.js', included: false},
  {pattern: 'src/**/*.js', included: false},
  {pattern: 'test/**/*.test.js', included: false},

  'test/test-main.js',
],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: [],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
})
}

这是 lib/bar.js

module.exports = function (a, b) {
  return a+b;
}

这是 src/MyModule.js

var bar = require("../lib/bar.js");

module.exports={
  add:function add (a, b) {
    return bar(a, b);
  }
}

这是 test/test-main.js

var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
    allTestFiles.push(normalizedTestModule);
  }
});

require.config({
  baseUrl: '/base',
  paths: {
    '../src/MyModule.js': '../src/MyModule.js',
    '../lib/bar.js': '../lib/bar.js'
  },
  deps: allTestFiles,
  callback: window.__karma__.start
});

这是测试/MyModule.test.js

var MyModule = require('../src/MyModule.js');
describe('A test suite', function () {
var expect = window.expect;

beforeEach(function () {

});

afterEach(function () {

});

it('should add the two numbers', function () {
    var  abc = MyModule.add(1, 3);
    expect(abc).to.be.equal(4);
});
});

当我运行测试时出现错误

Error: Module name "../src/MyModule.js" has not been loaded yet for context: _. Use require([])

我哪里错了?

最佳答案

函数 add() 需要在测试的本地范围内以某种方式访问​​,例如:

describe('A test suite', function() {

   /* ... */

   var add = function(a, b) {return a+b;};

   /* ... */

});

或者,require() 函数可用于加载更多脚本。

关于javascript - mocha karma 测试运行程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31944384/

相关文章:

node.js - GitLab-CI 和 node.js - 如何启动本地服务器然后运行测试?

javascript - .show() 不会改变效果或方向

javascript - 使用 w3data.js 并想要删除外部 DIV

ruby-on-rails - Cucumber:创建比较两个类别的步骤定义

unit-testing - 使用 Mocha 和 Enzyme 测试使用 React CSS 模块的 React 组件

javascript - Mocha 测试框架的 test.describe 返回值

javascript - Firestore : Multiple conditional where clauses

javascript - React.js 上的条件渲染

java - Junit - 您应该在课前还是课前创建数据库连接?

testing - 哪种工具适用于测试人员测试中的缺陷跟踪/记录?