Skip to content

Commit

Permalink
fix: Fix async call with depth 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Leunen committed Aug 20, 2016
1 parent e19b427 commit ddb684f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const PACKAGE_FILENAME = 'package.json';
const nullConf = { file: null, config: null };

function asyncFind(resolve, dir, depth) {
if (!depth) resolve(nullConf);
if (depth < 0) {
return resolve(nullConf);
}

const babelrc = path.join(dir, BABELRC_FILENAME);
return pathExists(babelrc).then(exists => {
Expand Down
57 changes: 37 additions & 20 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const findBabelConfig = require('../src');
describe('find-babel-config', () => {
describe('async', () => {
describe('babelrc', () => {
it('should return the config in the specified directory', () =>
it('should return the config in the direct directory', () =>
findBabelConfig('test/data/babelrc').then(({ file, config }) => {
expect(file).to.equal(path.join(process.cwd(), 'test/data/babelrc/.babelrc'));
expect(config).to.eql({ presets: ['fake-preset-babelrc'] });
Expand All @@ -36,7 +36,7 @@ describe('find-babel-config', () => {
});

describe('package.json', () => {
it('should return the config in the specified directory', () =>
it('should return the config in the direct directory', () =>
findBabelConfig('test/data/packagejson').then(({ file, config }) => {
expect(file).to.equal(path.join(process.cwd(), 'test/data/packagejson/package.json'));
expect(config).to.eql({ presets: ['fake-preset-packagejson'] });
Expand Down Expand Up @@ -81,30 +81,39 @@ describe('find-babel-config', () => {
);
});

describe('no config found...', () => {
it('should return null when the path is empty', () =>
findBabelConfig('').then(({ file, config }) => {
describe('with depth', () => {
it('should check only the direct directory with a depth 0', () =>
findBabelConfig('test/data/babelrc/dir1/dir2/dir3/dir4', 0).then(({ file, config }) => {
expect(file).to.equal(null);
expect(config).to.equal(null);
})
);

it('should return null when no config is found until / is reached', () =>
findBabelConfig('/sth/else/that/doesnt/exist').then(({ file, config }) => {
it('should return null when depth is reached without finding a babelrc file', () =>
findBabelConfig('test/data/babelrc/dir1/dir2/dir3/dir4/dir5', 1).then(({ file, config }) => {
expect(file).to.equal(null);
expect(config).to.equal(null);
})
);

it('should return null when depth is found without finding a babelrc file', () =>
findBabelConfig('test/data/babelrc/dir1/dir2/dir3/dir4/dir5', 1).then(({ file, config }) => {
it('should return null when depth is reached without finding package.json', () =>
findBabelConfig('test/data/packagejson/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8', 3).then(({ file, config }) => {
expect(file).to.equal(null);
expect(config).to.equal(null);
})
);
});

it('should return null when depth is found without finding package.json', () =>
findBabelConfig('test/data/packagejson/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8', 3).then(({ file, config }) => {
describe('edge cases...', () => {
it('should return null when the path is empty', () =>
findBabelConfig('').then(({ file, config }) => {
expect(file).to.equal(null);
expect(config).to.equal(null);
})
);

it('should return null when no config is found until / is reached', () =>
findBabelConfig('/sth/else/that/doesnt/exist').then(({ file, config }) => {
expect(file).to.equal(null);
expect(config).to.equal(null);
})
Expand Down Expand Up @@ -173,27 +182,35 @@ describe('find-babel-config', () => {
});
});

describe('no config found...', () => {
it('should return null when the path is empty', () => {
const { file, config } = findBabelConfig.sync('');
describe('with depth', () => {
it('should check only the direct directory with a depth 0', () => {
const { file, config } = findBabelConfig.sync('test/data/babelrc/dir1/dir2/dir3/dir4', 0);
expect(file).to.equal(null);
expect(config).to.equal(null);
});

it('should return null when no config is found until / is reached', () => {
const { file, config } = findBabelConfig.sync('/sth/else/that/doesnt/exist');
it('should return null when depth is reached without finding a babelrc file', () => {
const { file, config } = findBabelConfig.sync('test/data/babelrc/dir1/dir2/dir3/dir4/dir5', 1);
expect(file).to.equal(null);
expect(config).to.equal(null);
});

it('should return null when depth is found without finding a babelrc file', () => {
const { file, config } = findBabelConfig.sync('test/data/babelrc/dir1/dir2/dir3/dir4/dir5', 1);
it('should return null when depth is reached without finding package.json', () => {
const { file, config } = findBabelConfig.sync('test/data/packagejson/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8', 3);
expect(file).to.equal(null);
expect(config).to.equal(null);
});
});

it('should return null when depth is found without finding package.json', () => {
const { file, config } = findBabelConfig.sync('test/data/packagejson/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8', 3);
describe('edge cases...', () => {
it('should return null when the path is empty', () => {
const { file, config } = findBabelConfig.sync('');
expect(file).to.equal(null);
expect(config).to.equal(null);
});

it('should return null when no config is found until / is reached', () => {
const { file, config } = findBabelConfig.sync('/sth/else/that/doesnt/exist');
expect(file).to.equal(null);
expect(config).to.equal(null);
});
Expand Down

0 comments on commit ddb684f

Please sign in to comment.