不能要求CasperJS使用Underscore [英] Can't require Underscore with CasperJS

查看:94
本文介绍了不能要求CasperJS使用Underscore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using CasperJS to run automated frontend tests but have run in to an issue with using other npm modules in my tests. I'm aware of patchRequire however I believe that is only to be called outside of the test environment as the test runner patches require automatically. I did include it but the results were the same. It says it can't find the module. I have confirmed the underscore module is installed in node_modules in the projects root folder.

代码

'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:找不到模块下划线

CasperError: Can't find module underscore

推荐答案

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

So solution I eventually found was to create proxy modules that import the npm module and export it out to the casper script.

./proxies/underscore.js:

./proxies/underscore.js:

module.exports = require('underscore');

./tests/test.js

./tests/test.js

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

这篇关于不能要求CasperJS使用Underscore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆