0

I'm working on improving the tests for jquery-csv (jquery plugin).

I can currently run a full suite of tests (ie mocha/chai) from the command line with no problems. I'm having issues figuring out how to use require.js to load dependencies so I can extend the test runner to work with mochaphantomjs tests.

The HTML used to load RequireJS:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
</head>
<body>
  <div id="mocha"></div>
  <script data-main="scripts/app" src="scripts/require.js"></script>
</body>
</html>

The RequireJS module:

require.config({
  baseUrl: '/',
  paths: {
    'jquery'        : '../../node_modules/jquery/dist/jquery',
    'jquery-csv'    : '../../src/jquery.csv',
    'mocha'         : '../../node_modules/mocha/mocha',
    'chai'          : '../../node_modules/chai/chai',
  },
  shim: {
    'mocha': { 
      exports: 'mocha'
    },
    'chai': {
      exports: 'chai'
    },
    'jquery-csv' : {
      deps: ['jquery'],
      exports: 'jQuery.fn.csv',
    }
  },
});

define(function(require) {
  require('jquery');
  require('jquery-csv');

  // chai setup
  var chai = require('chai');
  var expect = chai.expect();
  var should = chai.should();

  // mocha setup
  var mocha = require('mocha');
  mocha.setup('bdd');
  mocha.reporter('html');
  mocha.bail(false);

  require(['test.js'], function(require) {
    if (window.mochaPhantomJS) {
      mochaPhantomJS.run();
    }
    else {
      mocha.run();
    }
  });
});

Note: The define function is using the CommmonJS style.

The error I'm getting is:

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

AFAIK, the shim should have fixed this issue by loading jQuery first and attaching the plugin to it.

I'm pretty new to RequireJS, is there something obvious I'm missing?

4

1 回答 1

2

尝试将您的“jquery-csv”添加为依赖项:

require(['test.js', 'jquery', 'jquery-csv'], function(require, $) {
    if (window.mochaPhantomJS) {
       mochaPhantomJS.run();
    }
    else {
      mocha.run();
    }
});
于 2015-02-20T13:56:57.240 回答