resolver_sync.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. var path = require('path');
  2. var fs = require('fs');
  3. var test = require('tape');
  4. var resolve = require('../');
  5. var sync = require('../sync');
  6. var requireResolveSupportsPaths = require.resolve.length > 1
  7. && !(/^v12\.[012]\./).test(process.version); // broken in v12.0-12.2, see https://github.com/nodejs/node/issues/27794
  8. var requireResolveDefaultPathsBroken = (/^v8\.9\.|^v9\.[01]\.0|^v9\.2\./).test(process.version);
  9. // broken in node v8.9.x, v9.0, v9.1, v9.2.x. see https://github.com/nodejs/node/pull/17113
  10. test('`./sync` entry point', function (t) {
  11. t.equal(resolve.sync, sync, '`./sync` entry point is the same as `.sync` on `main`');
  12. t.end();
  13. });
  14. test('foo', function (t) {
  15. var dir = path.join(__dirname, 'resolver');
  16. t.equal(
  17. resolve.sync('./foo', { basedir: dir }),
  18. path.join(dir, 'foo.js'),
  19. './foo'
  20. );
  21. if (requireResolveSupportsPaths) {
  22. t.equal(
  23. resolve.sync('./foo', { basedir: dir }),
  24. require.resolve('./foo', { paths: [dir] }),
  25. './foo: resolve.sync === require.resolve'
  26. );
  27. }
  28. t.equal(
  29. resolve.sync('./foo.js', { basedir: dir }),
  30. path.join(dir, 'foo.js'),
  31. './foo.js'
  32. );
  33. if (requireResolveSupportsPaths) {
  34. t.equal(
  35. resolve.sync('./foo.js', { basedir: dir }),
  36. require.resolve('./foo.js', { paths: [dir] }),
  37. './foo.js: resolve.sync === require.resolve'
  38. );
  39. }
  40. t.equal(
  41. resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
  42. path.join(dir, 'foo.js')
  43. );
  44. t.throws(function () {
  45. resolve.sync('foo', { basedir: dir });
  46. });
  47. // Test that filename is reported as the "from" value when passed.
  48. t.throws(
  49. function () {
  50. resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
  51. },
  52. {
  53. name: 'Error',
  54. message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
  55. }
  56. );
  57. t.end();
  58. });
  59. test('bar', function (t) {
  60. var dir = path.join(__dirname, 'resolver');
  61. var basedir = path.join(dir, 'bar');
  62. t.equal(
  63. resolve.sync('foo', { basedir: basedir }),
  64. path.join(dir, 'bar/node_modules/foo/index.js'),
  65. 'foo in bar'
  66. );
  67. if (!requireResolveDefaultPathsBroken && requireResolveSupportsPaths) {
  68. t.equal(
  69. resolve.sync('foo', { basedir: basedir }),
  70. require.resolve('foo', { paths: [basedir] }),
  71. 'foo in bar: resolve.sync === require.resolve'
  72. );
  73. }
  74. t.end();
  75. });
  76. test('baz', function (t) {
  77. var dir = path.join(__dirname, 'resolver');
  78. t.equal(
  79. resolve.sync('./baz', { basedir: dir }),
  80. path.join(dir, 'baz/quux.js'),
  81. './baz'
  82. );
  83. if (requireResolveSupportsPaths) {
  84. t.equal(
  85. resolve.sync('./baz', { basedir: dir }),
  86. require.resolve('./baz', { paths: [dir] }),
  87. './baz: resolve.sync === require.resolve'
  88. );
  89. }
  90. t.end();
  91. });
  92. test('biz', function (t) {
  93. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  94. t.equal(
  95. resolve.sync('./grux', { basedir: dir }),
  96. path.join(dir, 'grux/index.js')
  97. );
  98. if (requireResolveSupportsPaths) {
  99. t.equal(
  100. resolve.sync('./grux', { basedir: dir }),
  101. require.resolve('./grux', { paths: [dir] }),
  102. './grux: resolve.sync === require.resolve'
  103. );
  104. }
  105. var tivDir = path.join(dir, 'grux');
  106. t.equal(
  107. resolve.sync('tiv', { basedir: tivDir }),
  108. path.join(dir, 'tiv/index.js')
  109. );
  110. if (!requireResolveDefaultPathsBroken && requireResolveSupportsPaths) {
  111. t.equal(
  112. resolve.sync('tiv', { basedir: tivDir }),
  113. require.resolve('tiv', { paths: [tivDir] }),
  114. 'tiv: resolve.sync === require.resolve'
  115. );
  116. }
  117. var gruxDir = path.join(dir, 'tiv');
  118. t.equal(
  119. resolve.sync('grux', { basedir: gruxDir }),
  120. path.join(dir, 'grux/index.js')
  121. );
  122. if (!requireResolveDefaultPathsBroken && requireResolveSupportsPaths) {
  123. t.equal(
  124. resolve.sync('grux', { basedir: gruxDir }),
  125. require.resolve('grux', { paths: [gruxDir] }),
  126. 'grux: resolve.sync === require.resolve'
  127. );
  128. }
  129. t.end();
  130. });
  131. test('normalize', function (t) {
  132. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  133. t.equal(
  134. resolve.sync('../grux', { basedir: dir }),
  135. path.join(dir, 'index.js')
  136. );
  137. if (requireResolveSupportsPaths) {
  138. t.equal(
  139. resolve.sync('../grux', { basedir: dir }),
  140. require.resolve('../grux', { paths: [dir] }),
  141. '../grux: resolve.sync === require.resolve'
  142. );
  143. }
  144. t.end();
  145. });
  146. test('cup', function (t) {
  147. var dir = path.join(__dirname, 'resolver');
  148. t.equal(
  149. resolve.sync('./cup', {
  150. basedir: dir,
  151. extensions: ['.js', '.coffee']
  152. }),
  153. path.join(dir, 'cup.coffee'),
  154. './cup -> ./cup.coffee'
  155. );
  156. t.equal(
  157. resolve.sync('./cup.coffee', { basedir: dir }),
  158. path.join(dir, 'cup.coffee'),
  159. './cup.coffee'
  160. );
  161. t.throws(function () {
  162. resolve.sync('./cup', {
  163. basedir: dir,
  164. extensions: ['.js']
  165. });
  166. });
  167. if (requireResolveSupportsPaths) {
  168. t.equal(
  169. resolve.sync('./cup.coffee', { basedir: dir, extensions: ['.js', '.coffee'] }),
  170. require.resolve('./cup.coffee', { paths: [dir] }),
  171. './cup.coffee: resolve.sync === require.resolve'
  172. );
  173. }
  174. t.end();
  175. });
  176. test('mug', function (t) {
  177. var dir = path.join(__dirname, 'resolver');
  178. t.equal(
  179. resolve.sync('./mug', { basedir: dir }),
  180. path.join(dir, 'mug.js'),
  181. './mug -> ./mug.js'
  182. );
  183. if (requireResolveSupportsPaths) {
  184. t.equal(
  185. resolve.sync('./mug', { basedir: dir }),
  186. require.resolve('./mug', { paths: [dir] }),
  187. './mug: resolve.sync === require.resolve'
  188. );
  189. }
  190. t.equal(
  191. resolve.sync('./mug', {
  192. basedir: dir,
  193. extensions: ['.coffee', '.js']
  194. }),
  195. path.join(dir, 'mug.coffee'),
  196. './mug -> ./mug.coffee'
  197. );
  198. t.equal(
  199. resolve.sync('./mug', {
  200. basedir: dir,
  201. extensions: ['.js', '.coffee']
  202. }),
  203. path.join(dir, 'mug.js'),
  204. './mug -> ./mug.js'
  205. );
  206. t.end();
  207. });
  208. test('other path', function (t) {
  209. var resolverDir = path.join(__dirname, 'resolver');
  210. var dir = path.join(resolverDir, 'bar');
  211. var otherDir = path.join(resolverDir, 'other_path');
  212. t.equal(
  213. resolve.sync('root', {
  214. basedir: dir,
  215. paths: [otherDir]
  216. }),
  217. path.join(resolverDir, 'other_path/root.js')
  218. );
  219. t.equal(
  220. resolve.sync('lib/other-lib', {
  221. basedir: dir,
  222. paths: [otherDir]
  223. }),
  224. path.join(resolverDir, 'other_path/lib/other-lib.js')
  225. );
  226. t.throws(function () {
  227. resolve.sync('root', { basedir: dir });
  228. });
  229. t.throws(function () {
  230. resolve.sync('zzz', {
  231. basedir: dir,
  232. paths: [otherDir]
  233. });
  234. });
  235. t.end();
  236. });
  237. test('path iterator', function (t) {
  238. var resolverDir = path.join(__dirname, 'resolver');
  239. var exactIterator = function (x, start, getPackageCandidates, opts) {
  240. return [path.join(resolverDir, x)];
  241. };
  242. t.equal(
  243. resolve.sync('baz', { packageIterator: exactIterator }),
  244. path.join(resolverDir, 'baz/quux.js')
  245. );
  246. t.end();
  247. });
  248. test('incorrect main', function (t) {
  249. var resolverDir = path.join(__dirname, 'resolver');
  250. var dir = path.join(resolverDir, 'incorrect_main');
  251. t.equal(
  252. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  253. path.join(dir, 'index.js')
  254. );
  255. if (requireResolveSupportsPaths) {
  256. t.equal(
  257. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  258. require.resolve('./incorrect_main', { paths: [resolverDir] }),
  259. './incorrect_main: resolve.sync === require.resolve'
  260. );
  261. }
  262. t.end();
  263. });
  264. test('missing index', function (t) {
  265. t.plan(requireResolveSupportsPaths ? 2 : 1);
  266. var resolverDir = path.join(__dirname, 'resolver');
  267. try {
  268. resolve.sync('./missing_index', { basedir: resolverDir });
  269. t.fail('did not fail');
  270. } catch (err) {
  271. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  272. }
  273. if (requireResolveSupportsPaths) {
  274. try {
  275. require.resolve('./missing_index', { basedir: resolverDir });
  276. t.fail('require.resolve did not fail');
  277. } catch (err) {
  278. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  279. }
  280. }
  281. });
  282. test('missing main', function (t) {
  283. var resolverDir = path.join(__dirname, 'resolver');
  284. try {
  285. resolve.sync('./missing_main', { basedir: resolverDir });
  286. t.fail('require.resolve did not fail');
  287. } catch (err) {
  288. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  289. }
  290. if (requireResolveSupportsPaths) {
  291. try {
  292. resolve.sync('./missing_main', { basedir: resolverDir });
  293. t.fail('require.resolve did not fail');
  294. } catch (err) {
  295. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  296. }
  297. }
  298. t.end();
  299. });
  300. test('null main', function (t) {
  301. var resolverDir = path.join(__dirname, 'resolver');
  302. try {
  303. resolve.sync('./null_main', { basedir: resolverDir });
  304. t.fail('require.resolve did not fail');
  305. } catch (err) {
  306. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  307. }
  308. if (requireResolveSupportsPaths) {
  309. try {
  310. resolve.sync('./null_main', { basedir: resolverDir });
  311. t.fail('require.resolve did not fail');
  312. } catch (err) {
  313. t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
  314. }
  315. }
  316. t.end();
  317. });
  318. test('main: false', function (t) {
  319. var basedir = path.join(__dirname, 'resolver');
  320. var dir = path.join(basedir, 'false_main');
  321. t.equal(
  322. resolve.sync('./false_main', { basedir: basedir }),
  323. path.join(dir, 'index.js'),
  324. '`"main": false`: resolves to `index.js`'
  325. );
  326. if (requireResolveSupportsPaths) {
  327. t.equal(
  328. resolve.sync('./false_main', { basedir: basedir }),
  329. require.resolve('./false_main', { paths: [basedir] }),
  330. '`"main": false`: resolve.sync === require.resolve'
  331. );
  332. }
  333. t.end();
  334. });
  335. var stubStatSync = function stubStatSync(fn) {
  336. var statSync = fs.statSync;
  337. try {
  338. fs.statSync = function () {
  339. throw new EvalError('Unknown Error');
  340. };
  341. return fn();
  342. } finally {
  343. fs.statSync = statSync;
  344. }
  345. };
  346. test('#79 - re-throw non ENOENT errors from stat', function (t) {
  347. var dir = path.join(__dirname, 'resolver');
  348. stubStatSync(function () {
  349. t.throws(function () {
  350. resolve.sync('foo', { basedir: dir });
  351. }, /Unknown Error/);
  352. });
  353. t.end();
  354. });
  355. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  356. var dir = path.join(__dirname, 'resolver');
  357. var basedir = path.join(dir, 'same_names');
  358. t.equal(
  359. resolve.sync('./foo', { basedir: basedir }),
  360. path.join(dir, 'same_names/foo.js')
  361. );
  362. if (requireResolveSupportsPaths) {
  363. t.equal(
  364. resolve.sync('./foo', { basedir: basedir }),
  365. require.resolve('./foo', { paths: [basedir] }),
  366. './foo: resolve.sync === require.resolve'
  367. );
  368. }
  369. t.equal(
  370. resolve.sync('./foo/', { basedir: basedir }),
  371. path.join(dir, 'same_names/foo/index.js')
  372. );
  373. if (requireResolveSupportsPaths) {
  374. t.equal(
  375. resolve.sync('./foo/', { basedir: basedir }),
  376. require.resolve('./foo/', { paths: [basedir] }),
  377. './foo/: resolve.sync === require.resolve'
  378. );
  379. }
  380. t.end();
  381. });
  382. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  383. var dir = path.join(__dirname, 'resolver');
  384. var basedir = path.join(dir, 'same_names/foo');
  385. t.equal(
  386. resolve.sync('./', { basedir: basedir }),
  387. path.join(dir, 'same_names/foo/index.js'),
  388. './'
  389. );
  390. if (requireResolveSupportsPaths) {
  391. t.equal(
  392. resolve.sync('./', { basedir: basedir }),
  393. require.resolve('./', { paths: [basedir] }),
  394. './: resolve.sync === require.resolve'
  395. );
  396. }
  397. t.equal(
  398. resolve.sync('.', { basedir: basedir }),
  399. path.join(dir, 'same_names/foo/index.js'),
  400. '.'
  401. );
  402. if (requireResolveSupportsPaths) {
  403. t.equal(
  404. resolve.sync('.', { basedir: basedir }),
  405. require.resolve('.', { paths: [basedir] }),
  406. '.: resolve.sync === require.resolve',
  407. { todo: true }
  408. );
  409. }
  410. t.end();
  411. });
  412. test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
  413. var testFile = path.basename(__filename);
  414. t.test('sanity check', function (st) {
  415. st.equal(
  416. resolve.sync('./' + testFile),
  417. __filename,
  418. 'sanity check'
  419. );
  420. st.equal(
  421. resolve.sync('./' + testFile),
  422. require.resolve('./' + testFile),
  423. 'sanity check: resolve.sync === require.resolve'
  424. );
  425. st.end();
  426. });
  427. t.test('with a fake directory', function (st) {
  428. function run() { return resolve.sync('./' + testFile + '/blah'); }
  429. st.throws(run, 'throws an error');
  430. try {
  431. run();
  432. } catch (e) {
  433. st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  434. st.equal(
  435. e.message,
  436. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  437. 'can not find nonexistent module'
  438. );
  439. }
  440. st.end();
  441. });
  442. t.end();
  443. });
  444. test('sync dot main', function (t) {
  445. var start = new Date();
  446. t.equal(
  447. resolve.sync('./resolver/dot_main'),
  448. path.join(__dirname, 'resolver/dot_main/index.js'),
  449. './resolver/dot_main'
  450. );
  451. t.equal(
  452. resolve.sync('./resolver/dot_main'),
  453. require.resolve('./resolver/dot_main'),
  454. './resolver/dot_main: resolve.sync === require.resolve'
  455. );
  456. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  457. t.end();
  458. });
  459. test('sync dot slash main', function (t) {
  460. var start = new Date();
  461. t.equal(
  462. resolve.sync('./resolver/dot_slash_main'),
  463. path.join(__dirname, 'resolver/dot_slash_main/index.js')
  464. );
  465. t.equal(
  466. resolve.sync('./resolver/dot_slash_main'),
  467. require.resolve('./resolver/dot_slash_main'),
  468. './resolver/dot_slash_main: resolve.sync === require.resolve'
  469. );
  470. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  471. t.end();
  472. });
  473. test('not a directory', function (t) {
  474. var path = './foo';
  475. try {
  476. resolve.sync(path, { basedir: __filename });
  477. t.fail();
  478. } catch (err) {
  479. t.ok(err, 'a non-directory errors');
  480. t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
  481. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  482. }
  483. t.end();
  484. });
  485. test('non-string "main" field in package.json', function (t) {
  486. var dir = path.join(__dirname, 'resolver');
  487. try {
  488. var result = resolve.sync('./invalid_main', { basedir: dir });
  489. t.equal(result, undefined, 'result should not exist');
  490. t.fail('should not get here');
  491. } catch (err) {
  492. t.ok(err, 'errors on non-string main');
  493. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  494. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  495. }
  496. t.end();
  497. });
  498. test('non-string "main" field in package.json', function (t) {
  499. var dir = path.join(__dirname, 'resolver');
  500. try {
  501. var result = resolve.sync('./invalid_main', { basedir: dir });
  502. t.equal(result, undefined, 'result should not exist');
  503. t.fail('should not get here');
  504. } catch (err) {
  505. t.ok(err, 'errors on non-string main');
  506. t.equal(err.message, 'package “invalid_main” `main` must be a string');
  507. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  508. }
  509. t.end();
  510. });
  511. test('browser field in package.json', function (t) {
  512. var dir = path.join(__dirname, 'resolver');
  513. var res = resolve.sync('./browser_field', {
  514. basedir: dir,
  515. packageFilter: function packageFilter(pkg) {
  516. if (pkg.browser) {
  517. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  518. delete pkg.browser; // eslint-disable-line no-param-reassign
  519. }
  520. return pkg;
  521. }
  522. });
  523. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  524. t.end();
  525. });
  526. test('absolute paths', function (t) {
  527. var extensionless = __filename.slice(0, -path.extname(__filename).length);
  528. t.equal(
  529. resolve.sync(__filename),
  530. __filename,
  531. 'absolute path to this file resolves'
  532. );
  533. t.equal(
  534. resolve.sync(__filename),
  535. require.resolve(__filename),
  536. 'absolute path to this file: resolve.sync === require.resolve'
  537. );
  538. t.equal(
  539. resolve.sync(extensionless),
  540. __filename,
  541. 'extensionless absolute path to this file resolves'
  542. );
  543. t.equal(
  544. resolve.sync(__filename),
  545. require.resolve(__filename),
  546. 'absolute path to this file: resolve.sync === require.resolve'
  547. );
  548. t.equal(
  549. resolve.sync(__filename, { basedir: process.cwd() }),
  550. __filename,
  551. 'absolute path to this file with a basedir resolves'
  552. );
  553. if (requireResolveSupportsPaths) {
  554. t.equal(
  555. resolve.sync(__filename, { basedir: process.cwd() }),
  556. require.resolve(__filename, { paths: [process.cwd()] }),
  557. 'absolute path to this file + basedir: resolve.sync === require.resolve'
  558. );
  559. }
  560. t.equal(
  561. resolve.sync(extensionless, { basedir: process.cwd() }),
  562. __filename,
  563. 'extensionless absolute path to this file with a basedir resolves'
  564. );
  565. if (requireResolveSupportsPaths) {
  566. t.equal(
  567. resolve.sync(extensionless, { basedir: process.cwd() }),
  568. require.resolve(extensionless, { paths: [process.cwd()] }),
  569. 'extensionless absolute path to this file + basedir: resolve.sync === require.resolve'
  570. );
  571. }
  572. t.end();
  573. });
  574. var malformedDir = path.join(__dirname, 'resolver/malformed_package_json');
  575. test('malformed package.json', { skip: !fs.existsSync(malformedDir) }, function (t) {
  576. t.plan(5 + (requireResolveSupportsPaths ? 1 : 0));
  577. var basedir = malformedDir;
  578. var expected = path.join(basedir, 'index.js');
  579. t.equal(
  580. resolve.sync('./index.js', { basedir: basedir }),
  581. expected,
  582. 'malformed package.json is silently ignored'
  583. );
  584. if (requireResolveSupportsPaths) {
  585. t.equal(
  586. resolve.sync('./index.js', { basedir: basedir }),
  587. require.resolve('./index.js', { paths: [basedir] }),
  588. 'malformed package.json: resolve.sync === require.resolve'
  589. );
  590. }
  591. var res1 = resolve.sync(
  592. './index.js',
  593. {
  594. basedir: basedir,
  595. packageFilter: function (pkg, pkgfile, dir) {
  596. t.fail('should not reach here');
  597. }
  598. }
  599. );
  600. t.equal(
  601. res1,
  602. expected,
  603. 'with packageFilter: malformed package.json is silently ignored'
  604. );
  605. var res2 = resolve.sync(
  606. './index.js',
  607. {
  608. basedir: basedir,
  609. readPackageSync: function (readFileSync, pkgfile) {
  610. t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
  611. var result = String(readFileSync(pkgfile));
  612. try {
  613. return JSON.parse(result);
  614. } catch (e) {
  615. t.ok(e instanceof SyntaxError, 'readPackageSync: malformed package.json parses as a syntax error');
  616. }
  617. }
  618. }
  619. );
  620. t.equal(
  621. res2,
  622. expected,
  623. 'with readPackageSync: malformed package.json is silently ignored'
  624. );
  625. });