Skip to content

Commit 07a6b80

Browse files
authored
Fix "reset": also reset module alias names (#75)
Fix "reset": also reset module alias names
2 parents 23619af + fd391dc commit 07a6b80

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function reset () {
134134

135135
modulePaths = []
136136
moduleAliases = {}
137+
moduleAliasNames = []
137138
}
138139

139140
/**

test/specs.js

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ var exec = require('child_process').exec
44
var path = require('path')
55
var fs = require('fs')
66
var semver = require('semver')
7-
var moduleAlias
87

98
describe('module-alias', function () {
10-
beforeEach(function () { moduleAlias = require('..') })
9+
var moduleAlias
10+
11+
before(function () { moduleAlias = require('..') })
1112

1213
afterEach(function () {
1314
moduleAlias.reset()
@@ -136,19 +137,21 @@ describe('module-alias', function () {
136137
'module-alias'
137138
)
138139
var moduleAliasLocation = path.resolve(moduleAliasDir, 'index.js')
140+
var linkedModuleAlias
139141

140-
beforeEach(function () {
142+
before(function () {
141143
var indexJs = fs.readFileSync(path.resolve('.', 'index.js'))
142144
fs.writeFileSync(moduleAliasLocation, indexJs)
145+
linkedModuleAlias = require(moduleAliasDir)
143146
})
144147

145-
afterEach(function () {
148+
after(function () {
149+
linkedModuleAlias.reset()
146150
fs.unlinkSync(moduleAliasLocation)
147151
})
148152

149153
it('should import default settings from ../../package.json', function () {
150-
moduleAlias = require(moduleAliasDir)
151-
moduleAlias()
154+
linkedModuleAlias()
152155

153156
expectAliasesToBeImported()
154157
})
@@ -203,53 +206,53 @@ describe('module-alias', function () {
203206
expect(baz).to.have.string(path.join('bar', 'baz', 'index.js'))
204207
})
205208
}
206-
})
207209

208-
describe('Custom handler function', function () {
209-
it('should addAlias', function () {
210-
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
211-
expect(fromPath).to.equal(__filename)
212-
expect(request).to.equal('@src/baz')
213-
expect(alias).to.equal('@src')
214-
return path.join(__dirname, 'src/bar')
215-
})
216-
expect(require('@src/baz')).to.equal('Hello from baz')
217-
})
218-
219-
it('should addAliases', function () {
220-
moduleAlias.addAliases({
221-
'@src': function (fromPath, request, alias) {
210+
describe('Custom handler function', function () {
211+
it('should addAlias', function () {
212+
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
222213
expect(fromPath).to.equal(__filename)
223214
expect(request).to.equal('@src/baz')
224215
expect(alias).to.equal('@src')
225216
return path.join(__dirname, 'src/bar')
226-
},
227-
'@bar': function (fromPath, request, alias) {
228-
expect(fromPath).to.equal(__filename)
229-
expect(request).to.equal('@bar/index.js')
230-
expect(alias).to.equal('@bar')
231-
return path.join(__dirname, 'src/foo')
232-
}
217+
})
218+
expect(require('@src/baz')).to.equal('Hello from baz')
233219
})
234-
expect(require('@src/baz')).to.equal('Hello from baz')
235-
expect(require('@bar/index.js')).to.equal('Hello from foo')
236-
})
237220

238-
it('should return npm package', function () {
239-
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
240-
expect(fromPath).to.equal(__filename)
241-
expect(request).to.equal('@src')
242-
expect(alias).to.equal('@src')
243-
return 'hello-world-classic'
221+
it('should addAliases', function () {
222+
moduleAlias.addAliases({
223+
'@src': function (fromPath, request, alias) {
224+
expect(fromPath).to.equal(__filename)
225+
expect(request).to.equal('@src/baz')
226+
expect(alias).to.equal('@src')
227+
return path.join(__dirname, 'src/bar')
228+
},
229+
'@bar': function (fromPath, request, alias) {
230+
expect(fromPath).to.equal(__filename)
231+
expect(request).to.equal('@bar/index.js')
232+
expect(alias).to.equal('@bar')
233+
return path.join(__dirname, 'src/foo')
234+
}
235+
})
236+
expect(require('@src/baz')).to.equal('Hello from baz')
237+
expect(require('@bar/index.js')).to.equal('Hello from foo')
238+
})
239+
240+
it('should return npm package', function () {
241+
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
242+
expect(fromPath).to.equal(__filename)
243+
expect(request).to.equal('@src')
244+
expect(alias).to.equal('@src')
245+
return 'hello-world-classic'
246+
})
247+
expect(typeof require('@src')).to.equal('function')
244248
})
245-
expect(typeof require('@src')).to.equal('function')
246-
})
247249

248-
it('should throw when no path returned', function () {
249-
expect(function () {
250-
moduleAlias.addAlias('@src', function () {})
251-
require('@src')
250+
it('should throw when no path returned', function () {
251+
expect(function () {
252+
moduleAlias.addAlias('@src', function () {})
253+
require('@src')
254+
})
255+
.to.throw('[module-alias] Expecting custom handler function to return path.')
252256
})
253-
.to.throw('[module-alias] Expecting custom handler function to return path.')
254257
})
255258
})

0 commit comments

Comments
 (0)