【问题标题】:Can't require Underscore with CasperJSCasperJS 不能要求下划线
【发布时间】:2023-03-20 12:11:01
【问题描述】:

我正在使用 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: 找不到模块下划线

【问题讨论】:

    标签: javascript node.js phantomjs casperjs


    【解决方案1】:

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

    ./proxies/underscore.js:

    module.exports = require('underscore');
    

    ./tests/test.js

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

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多