Skip to content

Commit

Permalink
Update find-up, add tests for undefined result (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored and sindresorhus committed May 21, 2019
1 parent 1f5dc16 commit aba58d7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -4,7 +4,7 @@ const findUp = require('find-up');

const pkgDir = async cwd => {
const filePath = await findUp('package.json', {cwd});
return filePath ? path.dirname(filePath) : undefined;
return filePath && path.dirname(filePath);
};

module.exports = pkgDir;
Expand All @@ -13,5 +13,5 @@ module.exports.default = pkgDir;

module.exports.sync = cwd => {
const filePath = findUp.sync('package.json', {cwd});
return filePath ? path.dirname(filePath) : undefined;
return filePath && path.dirname(filePath);
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -45,10 +45,11 @@
"path"
],
"dependencies": {
"find-up": "^3.0.0"
"find-up": "^4.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tempy": "^0.3.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
Expand Down
13 changes: 13 additions & 0 deletions test.js
@@ -1,11 +1,24 @@
import fs from 'fs';
import path from 'path';
import test from 'ava';
import tempy from 'tempy';
import pkgDir from '.';

// Create a disjoint directory, used for the not-found tests
test.beforeEach(t => {
t.context.disjoint = tempy.directory();
});

test.afterEach(t => {
fs.rmdirSync(t.context.disjoint);
});

test('async', async t => {
t.is(await pkgDir(path.join(__dirname, 'fixture')), __dirname);
t.is(await pkgDir(t.context.disjoint), undefined);
});

test('sync', t => {
t.is(pkgDir.sync(path.join(__dirname, 'fixture')), __dirname);
t.is(pkgDir.sync(t.context.disjoint), undefined);
});

0 comments on commit aba58d7

Please sign in to comment.