Skip to content

Commit 2dedde3

Browse files
committed
[Tests] add tests with packages withe exports and ignoreExportsField = true
1 parent d0815eb commit 2dedde3

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

test/exports/node_modules/mix-conditionals/index.js

Whitespace-only changes.

test/exports/node_modules/valid-config/main.js

Whitespace-only changes.

test/exports/node_modules/valid-config/package.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/exports_disabled.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
var path = require('path');
2+
var test = require('tape');
3+
var resolve = require('../');
4+
5+
test('exports (disabled)', function (t) {
6+
t.plan(34);
7+
var dir = path.join(__dirname, '/exports');
8+
9+
resolve('mix-conditionals', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
10+
t.ifErr(err);
11+
t.equal(res, path.join(dir, 'node_modules/mix-conditionals/index.js'));
12+
});
13+
14+
resolve('invalid-config/with-node_modules', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
15+
t.notOk(res);
16+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
17+
t.match(err && err.message, /Cannot find module 'invalid-config\/with-node_modules'/);
18+
});
19+
20+
resolve('invalid-config/outside-package', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
21+
t.notOk(res);
22+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
23+
t.match(err && err.message, /Cannot find module 'invalid-config\/outside-package'/);
24+
});
25+
26+
resolve('invalid-config/not-with-dot', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
27+
t.notOk(res);
28+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
29+
t.match(err && err.message, /Cannot find module 'invalid-config\/not-with-dot'/);
30+
});
31+
32+
resolve('valid-config', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
33+
t.ifError(err);
34+
t.equal(res, path.join(dir, 'node_modules/valid-config/main.js'));
35+
});
36+
37+
resolve('valid-config/package.json', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
38+
t.ifError(err);
39+
t.equal(res, path.join(dir, 'node_modules/valid-config/package.json'));
40+
});
41+
42+
resolve('valid-config/remapped', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
43+
t.notOk(res);
44+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
45+
t.match(err && err.message, /Cannot find module 'valid-config\/remapped'/);
46+
});
47+
48+
resolve('valid-config/remapped/exists.js', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
49+
t.notOk(res);
50+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
51+
t.match(err && err.message, /Cannot find module 'valid-config\/remapped\/exists.js'/);
52+
});
53+
54+
resolve('valid-config/remapped/doesnt-exist.js', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
55+
t.notOk(res);
56+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
57+
t.match(err && err.message, /Cannot find module 'valid-config\/remapped\/doesnt-exist\.js'/);
58+
});
59+
60+
resolve('valid-config/array', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
61+
t.notOk(res);
62+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
63+
t.match(err && err.message, /Cannot find module 'valid-config\/array'/);
64+
});
65+
66+
resolve('valid-config/with-env', { basedir: dir, ignoreExportsField: true }, function (err, res, pkg) {
67+
t.notOk(res);
68+
t.equal(err && err.code, 'MODULE_NOT_FOUND');
69+
t.match(err && err.message, /Cannot find module 'valid-config\/with-env'/);
70+
});
71+
72+
function iterateWithoutModifyingSubpath(request, start, getNodeModulesDirs, opts) {
73+
return [path.join(opts.basedir, 'other_modules', request)];
74+
}
75+
resolve('other-module-dir', { basedir: dir, ignoreExportsField: true, packageIterator: iterateWithoutModifyingSubpath }, function (err, res, pkg) {
76+
t.ifErr(err);
77+
t.equal(res, path.join(dir, 'other_modules/other-module-dir/index.js'));
78+
});
79+
80+
function iterateModifyingSubpath(request, start, getNodeModulesDirs, opts) {
81+
return [path.join(opts.basedir, 'other_modules', request, 'index')];
82+
}
83+
resolve('other-module-dir', { basedir: dir, ignoreExportsField: true, packageIterator: iterateModifyingSubpath }, function (err, res, pkg) {
84+
t.ifErr(err);
85+
t.equal(res, path.join(dir, 'other_modules/other-module-dir/index.js'));
86+
});
87+
});
88+
89+
test('exports sync (disabled)', function (t) {
90+
var dir = path.join(__dirname, '/exports');
91+
92+
t.equal(resolve.sync('mix-conditionals', { basedir: dir, ignoreExportsField: true }), path.join(dir, 'node_modules/mix-conditionals/index.js'));
93+
94+
try {
95+
resolve.sync('invalid-config/with-node_modules', { basedir: dir, ignoreExportsField: true });
96+
t.fail();
97+
} catch (err) {
98+
t.equal(err.code, 'MODULE_NOT_FOUND');
99+
t.match(err.message, /Cannot find module 'invalid-config\/with-node_modules'/);
100+
}
101+
102+
try {
103+
resolve.sync('invalid-config/outside-package', { basedir: dir, ignoreExportsField: true });
104+
t.fail();
105+
} catch (err) {
106+
t.equal(err.code, 'MODULE_NOT_FOUND');
107+
t.match(err.message, /Cannot find module 'invalid-config\/outside-package'/);
108+
}
109+
110+
try {
111+
resolve.sync('invalid-config/not-with-dot', { basedir: dir, ignoreExportsField: true });
112+
t.fail();
113+
} catch (err) {
114+
t.equal(err.code, 'MODULE_NOT_FOUND');
115+
t.match(err.message, /Cannot find module 'invalid-config\/not-with-dot'/);
116+
}
117+
118+
t.equal(resolve.sync('valid-config', { basedir: dir, ignoreExportsField: true }), path.join(dir, 'node_modules/valid-config/main.js'));
119+
120+
t.equal(resolve.sync('valid-config/package.json', { basedir: dir, ignoreExportsField: true }), path.join(dir, 'node_modules/valid-config/package.json'));
121+
122+
try {
123+
resolve.sync('valid-config/remapped', { basedir: dir, ignoreExportsField: true });
124+
t.fail();
125+
} catch (err) {
126+
t.equal(err.code, 'MODULE_NOT_FOUND');
127+
t.match(err.message, /Cannot find module 'valid-config\/remapped'/);
128+
}
129+
130+
try {
131+
resolve.sync('valid-config/remapped/exists.js', { basedir: dir, ignoreExportsField: true });
132+
t.fail();
133+
} catch (err) {
134+
t.equal(err.code, 'MODULE_NOT_FOUND');
135+
t.match(err.message, /Cannot find module 'valid-config\/remapped\/exists.js'/);
136+
}
137+
138+
try {
139+
resolve.sync('valid-config/remapped/doesnt-exist.js', { basedir: dir, ignoreExportsField: true });
140+
t.fail();
141+
} catch (err) {
142+
t.equal(err.code, 'MODULE_NOT_FOUND');
143+
t.match(err.message, /Cannot find module 'valid-config\/remapped\/doesnt-exist\.js'/);
144+
}
145+
146+
try {
147+
resolve.sync('valid-config/array', { basedir: dir, ignoreExportsField: true });
148+
t.fail();
149+
} catch (err) {
150+
t.equal(err.code, 'MODULE_NOT_FOUND');
151+
t.match(err.message, /Cannot find module 'valid-config\/array'/);
152+
}
153+
154+
try {
155+
resolve.sync('valid-config/with-env', { basedir: dir, ignoreExportsField: true });
156+
t.fail();
157+
} catch (err) {
158+
t.equal(err.code, 'MODULE_NOT_FOUND');
159+
t.match(err.message, /Cannot find module 'valid-config\/with-env'/);
160+
}
161+
162+
function iterateWithoutModifyingSubpath(request, start, getNodeModulesDirs, opts) {
163+
return [path.join(opts.basedir, 'other_modules', request)];
164+
}
165+
t.equal(resolve.sync('other-module-dir', { basedir: dir, ignoreExportsField: true, packageIterator: iterateWithoutModifyingSubpath }), path.join(dir, 'other_modules/other-module-dir/index.js'));
166+
167+
function iterateModifyingSubpath(request, start, getNodeModulesDirs, opts) {
168+
return [path.join(opts.basedir, 'other_modules', request, 'index')];
169+
}
170+
t.equal(resolve.sync('other-module-dir', { basedir: dir, ignoreExportsField: true, packageIterator: iterateModifyingSubpath }), path.join(dir, 'other_modules/other-module-dir/index.js'));
171+
172+
t.end();
173+
});
174+

0 commit comments

Comments
 (0)