简体   繁体   English

Mocha测试以及对“ require”模块的后续调用

[英]Mocha test and subsequent calls to 'require' modules

I have a project that uses Mocha test runner via the commandline ( mocha -- test/test-dir ) and Browserify. 我有一个项目,该项目通过命令行( mocha -- test/test-dir )和Browserify使用Mocha测试运行程序。 Currently the unit tests are very simple and we just require the source javascript (unbrowserified) to run the tests on. 当前,单元测试非常简单,我们只需要源JavaScript(未浏览)就可以运行测试。 So far it has worked fine. 到目前为止,它运行良好。

//test/test-dir/my-test.js

var foo = require('../../js/src/foo.js');
var assert = require('assert');

describe('foo.getBar', function() {
...

We recently wanted to add another dependency to foo.js , typeahead.js . 最近,我们希望其他的依赖增加foo.jstypeahead.js

//js/src/foo.js

var jQuery = require("jquery");
var typeahead = require("typeahead.js");

...

Now our simple test fails with a TypeError: _.mixin is not a function . 现在,我们的简单测试因TypeError: _.mixin is not a function失败TypeError: _.mixin is not a function . TypeError: _.mixin is not a function This _.mixin is defined in typeahead.js but I'm suspecting that the nested require in foo.js is not being evaluated by Mocha? 这个_.mixin是在_.mixin定义的 ,但是我怀疑foo.js中的嵌套需求不会被Mocha评估吗?

Not too familiar with Mocha or Browserify, I thought that switching the test to use the browserified version of the foo.js would help as then the required modules would be built and available in foo.min.js , although I'm seeing the same error. 我对Mocha或Browserify不太熟悉,我认为切换测试以使用foo.js版本会有所帮助,因为随后将在foo.min.js构建并提供所需的模块,尽管我看到了相同的内容错误。 I know that typeahead supports CommonJS packages so I'm not sure what is wrong with these tests? 我知道typeahead支持CommonJS软件包,所以我不确定这些测试有什么问题吗? Any help would be very much appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

I added mochify to handle what I believe to be some commonJS issues with Mocha. 我添加了mochify来处理我认为与Mocha有关的一些commonJS问题。 I'd love to know why this works but the tests pass now using mochify -- test/test-dir/**/*.js now. 我很想知道为什么这行得通,但是现在可以使用mochify -- test/test-dir/**/*.js通过测试mochify -- test/test-dir/**/*.js现在通过mochify -- test/test-dir/**/*.js yay. 好极了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM