2013-12-19 107 views
4

我使用CasperJS來運行自動化前端測試,但遇到了在我的測試中使用其他npm模塊的問題。我知道patchRequire但是我相信只有在測試環境之外才會自動調用測試運行器補丁。我確實包括了它,但結果是一樣的。它說它找不到該模塊。我已經確認下劃線模塊安裝在node_modules項目的根文件夾中。不能要求帶CasperJS的下劃線

代碼

'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: Can't find module underscore

回答

6

所以解決辦法,我終於找到了是創建用於導入npm模塊並將其導出到casper腳本的代理模塊。

./proxies/underscore.js:

module.exports = require('underscore'); 

./tests/test.js

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