0
我的節點應用程序有一個相當大的測試套件,取決於ShouldJS模塊。問題是庫阻塞了在對象上分配.should
屬性,但是我的測試套件需要啓動一個需要可嘲弄的服務器(即運行在與我的測試套件相同的進程中),並動態構建需要設置的ElasticSearch查詢對象上的屬性.should
。不需要的節點ShouldJS
我試圖不要求ShouldJS使用方法described here。但是這並沒有幫助 - 請參閱下面的示例腳本。有解決方法或替代方法嗎?
實施例:
require('should');
var objBefore = {};
objBefore.should = 'should1'; // doesn't get assigned!
objBefore['should'] = 'should2'; // doesn't get assigned!
console.log('Before:', objBefore);
Object.keys(require.cache).forEach(function(path) {
if (path.indexOf('/node_modules/should/') === -1) return;
console.log('Un-requiring path', path);
delete require.cache[path];
});
var objAfter = {};
objAfter.should = 'should1'; // doesn't get assigned!
objAfter['should'] = 'should2'; // doesn't get assigned!
console.log('After:', objAfter);
// still has shouldJS stuff
console.log('objAfter.should:', objAfter.should);
其給出這樣的:
Before: {}
Un-requiring path /my/path/node_modules/should/index.js
Un-requiring path /my/path/node_modules/should/lib/should.js
...
Un-requiring path /my/path/node_modules/should/lib/ext/contain.js
Un-requiring path /my/path/node_modules/should/lib/ext/promise.js
After: {}
objAfter.should: Assertion { obj: {}, anyOne: false, negate: false, params: { actual: {} } }
的.should
屬性都未得到分配。