Comparing version 0.5.2 to 0.5.3
var Module = require('module') | ||
var path = require('path') | ||
var resolve = require('resolve') | ||
var _ = { | ||
@@ -8,2 +9,3 @@ compact: require('lodash/fp/compact'), | ||
find: require('lodash/fp/find'), | ||
ooFind: require('lodash/find'), | ||
flow: require('lodash/fp/flow'), | ||
@@ -63,2 +65,8 @@ invokeMap: require('lodash/fp/invokeMap'), | ||
} | ||
var absolutePath = absolutePathFor(relativePath, parentFileName) | ||
var resolvedPath = nodeResolve(absolutePath) | ||
return resolvedPath || absolutePath | ||
} | ||
var absolutePathFor = function (relativePath, parentFileName) { | ||
if (_.startsWith(relativePath, '/') || /^(\w|@)/.test(relativePath)) { | ||
@@ -75,5 +83,6 @@ return relativePath | ||
} | ||
var stubbing = stubbingThatMatchesRequest(request) | ||
if (quibbles.hasOwnProperty(request)) { | ||
return quibbles[request].stub | ||
if (stubbing) { | ||
return stubbing.stub | ||
} else if (requireWasCalledFromAFileThatHasQuibbledStuff()) { | ||
@@ -87,2 +96,8 @@ return doWithoutCache(request, parent, function () { | ||
} | ||
var stubbingThatMatchesRequest = function (request) { | ||
return _.ooFind(quibbles, function (stubbing, stubbedPath) { | ||
if (request === stubbedPath) return true | ||
if (nodeResolve(request) === stubbedPath) return true | ||
}, quibbles) | ||
} | ||
@@ -121,2 +136,8 @@ var requireWasCalledFromAFileThatHasQuibbledStuff = function () { | ||
var nodeResolve = function (request) { | ||
try { | ||
return resolve.sync(request) | ||
} catch (e) {} | ||
} | ||
var hackErrorStackToGetCallerFile = function (includeGlobalIgnores) { | ||
@@ -123,0 +144,0 @@ if (includeGlobalIgnores == null) { |
{ | ||
"name": "quibble", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "Makes it easy to replace require'd dependencies.", | ||
@@ -12,4 +12,4 @@ "homepage": "https://github.com/testdouble/quibble", | ||
"test:smells": "./test/require-smell-test.sh", | ||
"test:ci": "yarn test && yarn run style && yarn run test:example && yarn run test:smells && yarn run test:dependents", | ||
"preversion": "git pull --rebase && yarn run test:ci", | ||
"test:ci": "yarn test && yarn style && yarn test:example && yarn test:smells && yarn test:dependents", | ||
"preversion": "git pull --rebase && yarn test:ci", | ||
"postversion": "git push && git push --tags && npm publish" | ||
@@ -23,3 +23,4 @@ }, | ||
"dependencies": { | ||
"lodash": "^4.17.2" | ||
"lodash": "^4.17.2", | ||
"resolve": "^1.5.0" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
@@ -5,10 +5,71 @@ const quibble = require('../../lib/quibble') | ||
'basic behavior': function () { | ||
const stubbing = quibble('./../fixtures/a-function', function () { return 'kek' }) | ||
const stubbing = quibble('../fixtures/a-function', function () { return 'kek' }) | ||
assert.equal(stubbing(), 'kek') | ||
assert.equal(require('./../fixtures/a-function')(), 'kek') | ||
assert.equal(require('../fixtures/a-function')(), 'kek') | ||
assert.equal(require('../fixtures/a-function')(), 'kek') | ||
assert.equal(require('../../test/fixtures/a-function')(), 'kek') | ||
assert.equal(require('./../fixtures/b-function')(), 'b function') | ||
assert.equal(require('../fixtures/b-function')(), 'b function') | ||
}, | ||
'mismatched extensions': { | ||
'resolves specific quibbling with resolve-compatible require': function () { | ||
quibble('../fixtures/a-function.js', function () { return 'woo' }) | ||
const result = require('../fixtures/a-function')() | ||
assert.equal(result, 'woo') | ||
}, | ||
'resolves extensionless quibbling just as node itself would': function () { | ||
quibble('../fixtures/a-function', function () { return '!' }) | ||
assert.equal(require('../fixtures/a-function')(), '!') | ||
assert.equal(require('../fixtures/a-function.js')(), '!') | ||
assert.deepEqual(require('../fixtures/a-function.json'), {wups: 'lol'}) | ||
}, | ||
'general->specific stubbing matches specific': function () { | ||
quibble('../fixtures/a-function', function () { return 'A' }) | ||
quibble('../fixtures/a-function.js', function () { return 'B' }) | ||
quibble('../fixtures/a-function.json', {C: true}) | ||
assert.equal(require('../fixtures/a-function')(), 'B') | ||
assert.equal(require('../fixtures/a-function.js')(), 'B') | ||
assert.deepEqual(require('../fixtures/a-function.json'), {C: true}) | ||
}, | ||
'specific->general stubbing matches when node resolve does': function () { | ||
quibble('../fixtures/a-function.js', function () { return 'B' }) | ||
quibble('../fixtures/a-function.json', {C: true}) | ||
quibble('../fixtures/a-function', function () { return 'A' }) | ||
assert.equal(require('../fixtures/a-function')(), 'A') | ||
assert.equal(require('../fixtures/a-function.js')(), 'A') | ||
assert.deepEqual(require('../fixtures/a-function.json'), {C: true}) | ||
}, | ||
'non-existant files need to be exact since resolve will ¯\\_(ツ)_/¯ ': function () { | ||
quibble('../fixtures/fake-file.js', function () { return 'B' }) | ||
quibble('../fixtures/fake-file.json', {C: true}) | ||
quibble('../fixtures/fake-file', function () { return 'A' }) | ||
assert.equal(require('../fixtures/fake-file')(), 'A') | ||
assert.equal(require('../fixtures/fake-file.js')(), 'B') | ||
assert.deepEqual(require('../fixtures/fake-file.json'), {C: true}) | ||
} | ||
}, | ||
'last-in wins': function () { | ||
quibble('../fixtures/a-function', function () { return 'loser' }) | ||
quibble('../fixtures/a-function', function () { return 'loser!' }) | ||
quibble('../fixtures/a-function', function () { return 'winner' }) | ||
assert.equal(require('../fixtures/a-function')(), 'winner') | ||
}, | ||
'works when file is not resolvable': function () { | ||
quibble('../fixtures/not-a-real-file', function () { return 'hi' }) | ||
assert.equal(require('../fixtures/not-a-real-file')(), 'hi') | ||
}, | ||
'does not screw up symlinks': function () { | ||
quibble('../fixtures/a-symlinked-function', function () { return 'A' }) | ||
assert.equal(require('../fixtures/a-symlinked-function')(), 'A') | ||
assert.equal(require('../fixtures/a-function')(), 'the real function') | ||
}, | ||
'.config': { | ||
@@ -22,4 +83,2 @@ 'defaultFakeCreator': function () { | ||
assert.equal(require('./lol'), 'lol') | ||
// TODO why doesn't this test reset? | ||
} | ||
@@ -50,3 +109,3 @@ }, | ||
'without a reset': function () { | ||
quibble('./../fixtures/a-function', function () { return 'ha' }) | ||
quibble('../fixtures/a-function', function () { return 'ha' }) | ||
quibble('./some-other-thing') | ||
@@ -60,3 +119,3 @@ | ||
require('../fixtures/requires-a-function') | ||
quibble('./../fixtures/a-function', function () { return 'a fake function' }) | ||
quibble('../fixtures/a-function', function () { return 'a fake function' }) | ||
const quibbledRequiresAFunction = require('../fixtures/requires-a-function') | ||
@@ -63,0 +122,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
97121
27
371
0
2
+ Addedresolve@^1.5.0
+ Addedfunction-bind@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)