-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.cjs
More file actions
47 lines (38 loc) · 1.42 KB
/
test.cjs
File metadata and controls
47 lines (38 loc) · 1.42 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
'use strict';
const t = require('libtap');
const getPackageType = require('.');
const cache = require('./cache.cjs');
process.chdir(__dirname);
const pathInfo = (directory, type) => ({
[`${directory}/file.js`]: type,
[`${directory}/file.cjs`]: type,
[`${directory}/file.mjs`]: type
});
const checks = {
...pathInfo('.', 'module'),
...pathInfo('fixtures', 'module'),
...pathInfo('fixtures/node_modules', 'commonjs'),
...pathInfo('fixtures/node_modules/no-type', 'commonjs'),
...pathInfo('fixtures/node_modules/no-package-json', 'commonjs'),
...pathInfo('fixtures/node_modules/type-commonjs', 'commonjs'),
'fixtures/node_modules/type-module': 'commonjs',
...pathInfo('fixtures/node_modules/type-module', 'module'),
...pathInfo('fixtures/node_modules/type-module/subdir', 'commonjs'),
...pathInfo('', 'commonjs')
};
t.test('promise cache', async t => {
// This test hits the path where a value is not cached but a promise is
const p1 = getPackageType('./index.cjs');
const p2 = getPackageType('./index.mjs');
t.same(await p1, await p2);
t.same(await p1, 'module');
});
t.test('check directories', async t => {
for (const [directory, type] of Object.entries(checks)) {
t.same(await getPackageType(directory), type, `async: ${directory} type is ${type}`);
}
cache.clear();
for (const [directory, type] of Object.entries(checks)) {
t.same(getPackageType.sync(directory), type, `sync: ${directory} type is ${type}`);
}
});