Skip to content

Commit

Permalink
Fix "reset": also reset module alias names (#75)
Browse files Browse the repository at this point in the history
Fix "reset": also reset module alias names
  • Loading branch information
ilearnio authored Sep 30, 2019
2 parents 23619af + fd391dc commit 07a6b80
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function reset () {

modulePaths = []
moduleAliases = {}
moduleAliasNames = []
}

/**
Expand Down
91 changes: 47 additions & 44 deletions test/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ var exec = require('child_process').exec
var path = require('path')
var fs = require('fs')
var semver = require('semver')
var moduleAlias

describe('module-alias', function () {
beforeEach(function () { moduleAlias = require('..') })
var moduleAlias

before(function () { moduleAlias = require('..') })

afterEach(function () {
moduleAlias.reset()
Expand Down Expand Up @@ -136,19 +137,21 @@ describe('module-alias', function () {
'module-alias'
)
var moduleAliasLocation = path.resolve(moduleAliasDir, 'index.js')
var linkedModuleAlias

beforeEach(function () {
before(function () {
var indexJs = fs.readFileSync(path.resolve('.', 'index.js'))
fs.writeFileSync(moduleAliasLocation, indexJs)
linkedModuleAlias = require(moduleAliasDir)
})

afterEach(function () {
after(function () {
linkedModuleAlias.reset()
fs.unlinkSync(moduleAliasLocation)
})

it('should import default settings from ../../package.json', function () {
moduleAlias = require(moduleAliasDir)
moduleAlias()
linkedModuleAlias()

expectAliasesToBeImported()
})
Expand Down Expand Up @@ -203,53 +206,53 @@ describe('module-alias', function () {
expect(baz).to.have.string(path.join('bar', 'baz', 'index.js'))
})
}
})

describe('Custom handler function', function () {
it('should addAlias', function () {
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
expect(fromPath).to.equal(__filename)
expect(request).to.equal('@src/baz')
expect(alias).to.equal('@src')
return path.join(__dirname, 'src/bar')
})
expect(require('@src/baz')).to.equal('Hello from baz')
})

it('should addAliases', function () {
moduleAlias.addAliases({
'@src': function (fromPath, request, alias) {
describe('Custom handler function', function () {
it('should addAlias', function () {
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
expect(fromPath).to.equal(__filename)
expect(request).to.equal('@src/baz')
expect(alias).to.equal('@src')
return path.join(__dirname, 'src/bar')
},
'@bar': function (fromPath, request, alias) {
expect(fromPath).to.equal(__filename)
expect(request).to.equal('@bar/index.js')
expect(alias).to.equal('@bar')
return path.join(__dirname, 'src/foo')
}
})
expect(require('@src/baz')).to.equal('Hello from baz')
})
expect(require('@src/baz')).to.equal('Hello from baz')
expect(require('@bar/index.js')).to.equal('Hello from foo')
})

it('should return npm package', function () {
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
expect(fromPath).to.equal(__filename)
expect(request).to.equal('@src')
expect(alias).to.equal('@src')
return 'hello-world-classic'
it('should addAliases', function () {
moduleAlias.addAliases({
'@src': function (fromPath, request, alias) {
expect(fromPath).to.equal(__filename)
expect(request).to.equal('@src/baz')
expect(alias).to.equal('@src')
return path.join(__dirname, 'src/bar')
},
'@bar': function (fromPath, request, alias) {
expect(fromPath).to.equal(__filename)
expect(request).to.equal('@bar/index.js')
expect(alias).to.equal('@bar')
return path.join(__dirname, 'src/foo')
}
})
expect(require('@src/baz')).to.equal('Hello from baz')
expect(require('@bar/index.js')).to.equal('Hello from foo')
})

it('should return npm package', function () {
moduleAlias.addAlias('@src', function (fromPath, request, alias) {
expect(fromPath).to.equal(__filename)
expect(request).to.equal('@src')
expect(alias).to.equal('@src')
return 'hello-world-classic'
})
expect(typeof require('@src')).to.equal('function')
})
expect(typeof require('@src')).to.equal('function')
})

it('should throw when no path returned', function () {
expect(function () {
moduleAlias.addAlias('@src', function () {})
require('@src')
it('should throw when no path returned', function () {
expect(function () {
moduleAlias.addAlias('@src', function () {})
require('@src')
})
.to.throw('[module-alias] Expecting custom handler function to return path.')
})
.to.throw('[module-alias] Expecting custom handler function to return path.')
})
})

0 comments on commit 07a6b80

Please sign in to comment.