不能使用CasperJS的Underscore

时间:2013-12-19 20:23:00

标签: javascript node.js phantomjs casperjs

我正在使用CasperJS来运行自动前端测试,但在我的测试中遇到了使用其他npm模块的问题。我知道patchRequire但是我认为只有在测试环境中调用,因为测试运行器补丁需要自动调用。我确实包含了它,但结果是一样的。它说它找不到模块。我已确认下划线模块已安装在项目根文件夹的node_modules中。

代码

'use strict'

_ = require 'underscore'

testConfig =
    testPageUrl: ''
    testSearchTerm: 'the'

config = _.extend testConfig, require 'common/config'

Javascript中的代码

'use strict';

_ = require('underscore');

testConfig = {
  testPageUrl: '',
  testSearchTerm: 'the'
};

config = _.extend(testConfig, require('common/config'));

错误

  

CasperError:找不到模块下划线

1 个答案:

答案 0 :(得分:6)

我最终找到的解决方案是创建代理模块,导入npm模块并将其导出到casper脚本。

./proxies/underscore.js:

module.exports = require('underscore');

<强> ./tests/test.js

var _ = require('../proxies/underscore');