| 1 |
/*jslint nomen: true */ |
| 2 |
var hook = require('../../lib/hook'), |
| 3 |
currentHook, |
| 4 |
matcher = function (file) { return file.indexOf('foo.js') > 0; }, |
| 5 |
matcher2 = function (file) { return file.indexOf('bar.es6') > 0; }, |
| 6 |
transformer = function () { return 'module.exports.bar = function () { return "bar"; };'; }, |
| 7 |
transformer2 = function () { return 'module.exports.blah = function () { return "blah"; };'; }, |
| 8 |
badTransformer = function () { throw "Boo!"; }, |
| 9 |
scriptTransformer = function () { return '(function () { return 42; }());'; }; |
| 10 |
|
| 11 |
module.exports = { |
| 12 |
"when require is hooked": { |
| 13 |
setUp: function (cb) { |
| 14 |
currentHook = require('module')._extensions['.js']; |
| 15 |
hook.hookRequire(matcher, transformer, { verbose: true }); |
| 16 |
cb(); |
| 17 |
}, |
| 18 |
tearDown: function (cb) { |
| 19 |
hook.unloadRequireCache(matcher); |
| 20 |
require('module')._extensions['.js'] = currentHook; |
| 21 |
cb(); |
| 22 |
}, |
| 23 |
|
| 24 |
"foo should be transformed": function (test) { |
| 25 |
var foo = require('./data/foo'); |
| 26 |
test.ok(foo.bar); |
| 27 |
test.equals('bar', foo.bar()); |
| 28 |
test.done(); |
| 29 |
}, |
| 30 |
|
| 31 |
"but baz should be skipped": function (test) { |
| 32 |
var foo = require('./data/baz'); |
| 33 |
test.ok(foo.baz); |
| 34 |
test.equals('baz', foo.baz()); |
| 35 |
test.done(); |
| 36 |
}, |
| 37 |
|
| 38 |
"twice it should save original code": function (test) { |
| 39 |
hook.hookRequire(matcher, transformer, { verbose: true }); |
| 40 |
hook.unhookRequire(); |
| 41 |
var foo = require('./data/foo'); |
| 42 |
test.ok(foo.foo); |
| 43 |
test.equals('foo', foo.foo()); |
| 44 |
test.done(); |
| 45 |
}, |
| 46 |
|
| 47 |
"postLoadHook should be called": function (test) { |
| 48 |
var called = null, |
| 49 |
opts = { postLoadHook: function (file) { called = file; }}, |
| 50 |
foo; |
| 51 |
|
| 52 |
hook.unhookRequire(); |
| 53 |
hook.hookRequire(matcher, transformer, opts); |
| 54 |
foo = require('./data/foo'); |
| 55 |
test.ok(called.match(/foo\.js/)); |
| 56 |
test.done(); |
| 57 |
}, |
| 58 |
|
| 59 |
"and cache should be un- and reloaded": function (test) { |
| 60 |
hook.unhookRequire(); |
| 61 |
hook.hookRequire(matcher, transformer2); |
| 62 |
var foo = require('./data/foo'); |
| 63 |
test.ok(foo.blah); |
| 64 |
test.equals('blah', foo.blah()); |
| 65 |
test.done(); |
| 66 |
}, |
| 67 |
|
| 68 |
"bad transformer should return original code": function (test) { |
| 69 |
hook.unhookRequire(); |
| 70 |
hook.hookRequire(matcher, badTransformer); |
| 71 |
var foo = require('./data/foo'); |
| 72 |
test.ok(foo.foo); |
| 73 |
test.equals('foo', foo.foo()); |
| 74 |
test.done(); |
| 75 |
} |
| 76 |
}, |
| 77 |
"when extensions are passed to hookRequire": { |
| 78 |
setUp: function(cb) { |
| 79 |
hook.hookRequire(matcher2, transformer2, { verbose: true, extensions: ['.es6'] }); |
| 80 |
cb(); |
| 81 |
}, |
| 82 |
tearDown: function(cb) { |
| 83 |
hook.unloadRequireCache(matcher2); |
| 84 |
delete require('module')._extensions['.es6']; |
| 85 |
cb(); |
| 86 |
}, |
| 87 |
"bar should be transformed": function(test) { |
| 88 |
var bar = require('./data/bar'); |
| 89 |
test.ok(bar.blah); |
| 90 |
test.equals('blah', bar.blah()); |
| 91 |
test.done(); |
| 92 |
}, |
| 93 |
|
| 94 |
"but foo should be skipped": function (test) { |
| 95 |
var foo = require('./data/foo'); |
| 96 |
test.ok(foo.foo); |
| 97 |
test.equals('foo', foo.foo()); |
| 98 |
test.done(); |
| 99 |
}, |
| 100 |
"bad transformer should return original code": function (test) { |
| 101 |
hook.unhookRequire(); |
| 102 |
hook.hookRequire(matcher2, badTransformer, { verbose: true, extensions: ['.es6'] }); |
| 103 |
var bar = require('./data/bar'); |
| 104 |
test.ok(bar.bar); |
| 105 |
test.equals('bar', bar.bar()); |
| 106 |
test.done(); |
| 107 |
} |
| 108 |
}, |
| 109 |
"when createScript is hooked": { |
| 110 |
setUp: function (cb) { |
| 111 |
currentHook = require('vm').createScript; |
| 112 |
cb(); |
| 113 |
}, |
| 114 |
tearDown: function (cb) { |
| 115 |
require('vm').createScript = currentHook; |
| 116 |
cb(); |
| 117 |
}, |
| 118 |
"foo should be transformed (without any options)": function (test) { |
| 119 |
var s; |
| 120 |
hook.hookCreateScript(matcher, scriptTransformer); |
| 121 |
s = require('vm').createScript('(function () { return 10; }());', '/bar/foo.js'); |
| 122 |
test.equals(42, s.runInThisContext()); |
| 123 |
hook.unhookCreateScript(); |
| 124 |
s = require('vm').createScript('(function () { return 10; }());', '/bar/foo.js'); |
| 125 |
test.equals(10, s.runInThisContext()); |
| 126 |
test.done(); |
| 127 |
} |
| 128 |
}, |
| 129 |
"when runInThisContext is hooked": { |
| 130 |
setUp: function (cb) { |
| 131 |
currentHook = require('vm').runInThisContext; |
| 132 |
cb(); |
| 133 |
}, |
| 134 |
tearDown: function (cb) { |
| 135 |
require('vm').runInThisContext = currentHook; |
| 136 |
cb(); |
| 137 |
}, |
| 138 |
"foo should be transformed": function (test) { |
| 139 |
var s; |
| 140 |
hook.hookRunInThisContext(matcher, scriptTransformer); |
| 141 |
s = require('vm').runInThisContext('(function () { return 10; }());', '/bar/foo.js'); |
| 142 |
test.equals(42, s); |
| 143 |
hook.unhookRunInThisContext(); |
| 144 |
s = require('vm').runInThisContext('(function () { return 10; }());', '/bar/foo.js'); |
| 145 |
test.equals(10, s); |
| 146 |
test.done(); |
| 147 |
}, |
| 148 |
"code with no filename should not be transformed": function (test) { |
| 149 |
var s; |
| 150 |
hook.hookRunInThisContext(matcher, scriptTransformer); |
| 151 |
s = require('vm').runInThisContext('(function () { return 10; }());'); |
| 152 |
test.equals(10, s); |
| 153 |
hook.unhookCreateScript(); |
| 154 |
test.done(); |
| 155 |
}, |
| 156 |
"code with non-string filename should not be transformed": function (test) { |
| 157 |
var s; |
| 158 |
hook.hookRunInThisContext(matcher, scriptTransformer); |
| 159 |
s = require('vm').runInThisContext('(function () { return 10; }());', {}); |
| 160 |
test.equals(10, s); |
| 161 |
hook.unhookCreateScript(); |
| 162 |
test.done(); |
| 163 |
} |
| 164 |
}, |
| 165 |
}; |