Skip to content

Commit b377450

Browse files
committed
Add tests for REPL mode
- This require introducing a way of having "require.main" undefined in our module. The require function is different between the test module and the module under test, so we can't just do `require.main = null` in the test function, as it won't affect the "require" function of the prod code. So we need to introduce a helper function for evaluating require.main, and injact a flag in the test module when running the tests.
1 parent 2223dc0 commit b377450

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ function addPath (path) {
8282
if (modulePaths.indexOf(path) === -1) {
8383
modulePaths.push(path)
8484
// Enable the search path for the current top-level module
85-
if (require.main) {
86-
addPathHelper(path, require.main.paths)
85+
var mainModule = getMainModule()
86+
if (mainModule) {
87+
addPathHelper(path, mainModule.paths)
8788
}
8889
parent = module.parent
8990

9091
// Also modify the paths of the module that was used to load the
9192
// app-module-paths module and all of it's parents
92-
while (parent && parent !== require.main) {
93+
while (parent && parent !== mainModule) {
9394
addPathHelper(path, parent.paths)
9495
parent = parent.parent
9596
}
@@ -115,10 +116,12 @@ function addAlias (alias, target) {
115116
* The function is undocumented and for testing purposes only
116117
*/
117118
function reset () {
119+
var mainModule = getMainModule()
120+
118121
// Reset all changes in paths caused by addPath function
119122
modulePaths.forEach(function (path) {
120-
if (require.main) {
121-
removePathHelper(path, require.main.paths)
123+
if (mainModule) {
124+
removePathHelper(path, mainModule.paths)
122125
}
123126

124127
// Delete from require.cache if the module has been required before.
@@ -130,7 +133,7 @@ function reset () {
130133
})
131134

132135
var parent = module.parent
133-
while (parent && parent !== require.main) {
136+
while (parent && parent !== mainModule) {
134137
removePathHelper(path, parent.paths)
135138
parent = parent.parent
136139
}
@@ -209,6 +212,10 @@ function init (options) {
209212
}
210213
}
211214

215+
function getMainModule () {
216+
return require.main._simulateRepl ? undefined : require.main
217+
}
218+
212219
module.exports = init
213220
module.exports.addPath = addPath
214221
module.exports.addAlias = addAlias

test/specs.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ describe('module-alias', function () {
158158
})
159159
})
160160

161+
context('when used from the REPL', function () {
162+
before(function () {
163+
require.main._simulateRepl = true
164+
})
165+
166+
after(function () {
167+
delete require.main._simulateRepl
168+
})
169+
170+
it('should addPath', function () {
171+
moduleAlias.addPath('some-path')
172+
})
173+
174+
it('should reset', function () {
175+
moduleAlias.reset()
176+
})
177+
})
178+
161179
it('should support forked modules', function () {
162180
expect(typeof require('hello-world-classic')).to.equal('function')
163181
})

0 commit comments

Comments
 (0)