-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
51 lines (41 loc) · 1.27 KB
/
test.js
File metadata and controls
51 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
const {dirname, join, parse} = require('path');
const {mkdir, symlink, writeFile} = require('fs').promises;
const test = require('tape');
const findPkgDir = require('.');
const rmfr = require('rmfr');
test('findPkgDir()', async t => {
t.equal(
findPkgDir(require.resolve('eslint/bin/eslint.js')),
dirname(require.resolve('eslint/package.json')),
'should find a directory where the closest package.json exists from a given path.'
);
const tmp = join(__dirname, 'tmp');
await rmfr(tmp);
await mkdir(join(tmp, 'package.json'), {recursive: true});
await Promise.all([
symlink(process.cwd(), join(tmp, 'symlink')),
writeFile(join(tmp, 'package.json', 'index.js'), '')
]);
t.equal(
findPkgDir(join(tmp, 'package.json', 'index.js')),
process.cwd(),
'should check if `package.json` is actually a directory or not.'
);
t.equal(
findPkgDir(join(tmp, 'symlink')),
process.cwd(),
'should resolve symlinks.'
);
t.equal(
findPkgDir(parse(__dirname).root),
null,
'should return null when all the ancestor directories doesn\'t have package.json.'
);
t.throws(
() => findPkgDir(1),
/^TypeError.*Expected a directory path.*, but got a non-string value 1 \(number\)\./u,
'should throw an error when it takes a non-string argument.'
);
t.end();
});