不能使用CasperJS的Underscore

Kyl*_*lee 4 javascript node.js phantomjs casperjs

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

'use strict'

_ = require 'underscore'

testConfig =
    testPageUrl: ''
    testSearchTerm: 'the'

config = _.extend testConfig, require 'common/config'
Run Code Online (Sandbox Code Playgroud)

Javascript中的代码

'use strict';

_ = require('underscore');

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

config = _.extend(testConfig, require('common/config'));
Run Code Online (Sandbox Code Playgroud)

错误

CasperError:找不到模块下划线

Kyl*_*lee 6

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

./proxies/underscore.js:

module.exports = require('underscore');
Run Code Online (Sandbox Code Playgroud)

./tests/test.js

var _ = require('../proxies/underscore');
Run Code Online (Sandbox Code Playgroud)