index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. 'use strict'
  2. const { join } = require('path')
  3. const { test } = require('tap')
  4. const { when } = require('nonsynchronous')
  5. const { chmodSync } = require('fs')
  6. const isFileEsm = require('..')
  7. const {
  8. ERR_PATH_MUST_BE_STRING,
  9. ERR_PATH_MUST_BE_ABSOLUTE,
  10. ERR_PATH_MUST_EXIST,
  11. ERR_PATH_MUST_HAVE_VALID_EXT
  12. } = isFileEsm.constants
  13. const ERR_BAD_TYPE = 'package.json type "bad" not recognized'
  14. const ERR_PERMISSION_DENIED = /EACCES: permission denied/
  15. const fixtures = join(__dirname, 'fixtures')
  16. test('path must be string (promise)', async ({ rejects }) => {
  17. await rejects(() => isFileEsm(), Error(ERR_PATH_MUST_BE_STRING))
  18. await rejects(() => isFileEsm({}), Error(ERR_PATH_MUST_BE_STRING))
  19. })
  20. test('path must be absolute (promise)', async ({ rejects }) => {
  21. await rejects(() => isFileEsm('../foo.js'), Error(ERR_PATH_MUST_BE_ABSOLUTE))
  22. await rejects(() => isFileEsm('./bar/foo.mjs'), Error(ERR_PATH_MUST_BE_ABSOLUTE))
  23. })
  24. test('path must have ext (promise)', async ({ rejects }) => {
  25. await rejects(() => isFileEsm('/bar/foo'), Error(ERR_PATH_MUST_HAVE_VALID_EXT))
  26. })
  27. test('path must have valid ext (promise)', async ({ rejects }) => {
  28. await rejects(() => isFileEsm('/bar/foo.ext'), Error(ERR_PATH_MUST_HAVE_VALID_EXT))
  29. })
  30. test('path must exist (promise)', async ({ rejects }) => {
  31. await rejects(() => isFileEsm(join(fixtures, 'bar', 'foo.js')), Error(ERR_PATH_MUST_EXIST))
  32. })
  33. test('type-commonjs-cjs promise', async ({ is }) => {
  34. const inputPath = join(fixtures, 'type-commonjs-cjs', 'file.cjs')
  35. const expectedPkgPath = join(fixtures, 'type-commonjs-cjs', 'package.json')
  36. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  37. is(esm, false)
  38. is(path, inputPath)
  39. is(expectedPkgPath, pkgPath)
  40. is(type, 'commonjs')
  41. is(extType, 'c')
  42. })
  43. test('type-commonjs-js promise', async ({ is }) => {
  44. const inputPath = join(fixtures, 'type-commonjs-js', 'file.js')
  45. const expectedPkgPath = join(fixtures, 'type-commonjs-js', 'package.json')
  46. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  47. is(esm, false)
  48. is(path, inputPath)
  49. is(expectedPkgPath, pkgPath)
  50. is(type, 'commonjs')
  51. is(extType, 'j')
  52. })
  53. test('type-commonjs-mjs promise', async ({ is }) => {
  54. const inputPath = join(fixtures, 'type-commonjs-mjs', 'file.mjs')
  55. const expectedPkgPath = join(fixtures, 'type-commonjs-mjs', 'package.json')
  56. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  57. is(esm, true)
  58. is(path, inputPath)
  59. is(expectedPkgPath, pkgPath)
  60. is(type, 'commonjs')
  61. is(extType, 'm')
  62. })
  63. test('type-module-cjs promise', async ({ is }) => {
  64. const inputPath = join(fixtures, 'type-module-cjs', 'file.cjs')
  65. const expectedPkgPath = join(fixtures, 'type-module-cjs', 'package.json')
  66. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  67. is(esm, false)
  68. is(path, inputPath)
  69. is(expectedPkgPath, pkgPath)
  70. is(type, 'module')
  71. is(extType, 'c')
  72. })
  73. test('type-module-js promise', async ({ is }) => {
  74. const inputPath = join(fixtures, 'type-module-js', 'file.js')
  75. const expectedPkgPath = join(fixtures, 'type-module-js', 'package.json')
  76. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  77. is(esm, true)
  78. is(path, inputPath)
  79. is(expectedPkgPath, pkgPath)
  80. is(type, 'module')
  81. is(extType, 'j')
  82. })
  83. test('type-module-mjs promise', async ({ is }) => {
  84. const inputPath = join(fixtures, 'type-module-mjs', 'file.mjs')
  85. const expectedPkgPath = join(fixtures, 'type-module-mjs', 'package.json')
  86. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  87. is(esm, true)
  88. is(path, inputPath)
  89. is(expectedPkgPath, pkgPath)
  90. is(type, 'module')
  91. is(extType, 'm')
  92. })
  93. test('type-undefined-cjs promise', async ({ is }) => {
  94. const inputPath = join(fixtures, 'type-undefined-cjs', 'file.cjs')
  95. const expectedPkgPath = join(fixtures, 'type-undefined-cjs', 'package.json')
  96. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  97. is(esm, false)
  98. is(path, inputPath)
  99. is(expectedPkgPath, pkgPath)
  100. is(type, undefined)
  101. is(extType, 'c')
  102. })
  103. test('type-undefined-js promise', async ({ is }) => {
  104. const inputPath = join(fixtures, 'type-undefined-js', 'file.js')
  105. const expectedPkgPath = join(fixtures, 'type-undefined-js', 'package.json')
  106. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  107. is(esm, false)
  108. is(path, inputPath)
  109. is(expectedPkgPath, pkgPath)
  110. is(type, undefined)
  111. is(extType, 'j')
  112. })
  113. test('type-undefined-mjs promise', async ({ is }) => {
  114. const inputPath = join(fixtures, 'type-undefined-mjs', 'file.mjs')
  115. const expectedPkgPath = join(fixtures, 'type-undefined-mjs', 'package.json')
  116. const { esm, path, type, extType, pkgPath } = await isFileEsm(inputPath)
  117. is(esm, true)
  118. is(path, inputPath)
  119. is(expectedPkgPath, pkgPath)
  120. is(type, undefined)
  121. is(extType, 'm')
  122. })
  123. test('type-bad-cjs promise', async ({ is, rejects }) => {
  124. const inputPath = join(fixtures, 'type-bad-cjs', 'file.cjs')
  125. const expectedPkgPath = join(fixtures, 'type-bad-cjs', 'package.json')
  126. let error = null
  127. await rejects(async () => {
  128. try {
  129. await isFileEsm(inputPath)
  130. } catch (err) {
  131. error = err
  132. throw err
  133. }
  134. }, ERR_BAD_TYPE)
  135. const { path, type, extType, pkgPath } = error.meta
  136. is(path, inputPath)
  137. is(expectedPkgPath, pkgPath)
  138. is(type, 'bad')
  139. is(extType, 'c')
  140. })
  141. test('type-bad-js promise', async ({ is, rejects }) => {
  142. const inputPath = join(fixtures, 'type-bad-js', 'file.js')
  143. const expectedPkgPath = join(fixtures, 'type-bad-js', 'package.json')
  144. let error = null
  145. await rejects(async () => {
  146. try {
  147. await isFileEsm(inputPath)
  148. } catch (err) {
  149. error = err
  150. throw err
  151. }
  152. }, ERR_BAD_TYPE)
  153. const { path, type, extType, pkgPath } = error.meta
  154. is(path, inputPath)
  155. is(expectedPkgPath, pkgPath)
  156. is(type, 'bad')
  157. is(extType, 'j')
  158. })
  159. test('type-bad-mjs promise', async ({ is, rejects }) => {
  160. const inputPath = join(fixtures, 'type-bad-mjs', 'file.mjs')
  161. const expectedPkgPath = join(fixtures, 'type-bad-mjs', 'package.json')
  162. let error = null
  163. await rejects(async () => {
  164. try {
  165. await isFileEsm(inputPath)
  166. } catch (err) {
  167. error = err
  168. throw err
  169. }
  170. }, ERR_BAD_TYPE)
  171. const { path, type, extType, pkgPath } = error.meta
  172. is(path, inputPath)
  173. is(expectedPkgPath, pkgPath)
  174. is(type, 'bad')
  175. is(extType, 'm')
  176. })
  177. test('package.json read error propagation (promise)', async ({ rejects, tearDown }) => {
  178. const inputPath = join(fixtures, 'type-bad-js', 'file.js')
  179. const expectedPkgPath = join(fixtures, 'type-bad-js', 'package.json')
  180. tearDown(() => chmodSync(expectedPkgPath, 0o644))
  181. chmodSync(expectedPkgPath, 0o000)
  182. await rejects(() => isFileEsm(inputPath), ERR_PERMISSION_DENIED)
  183. })
  184. test('type-commonjs-cjs sync', async ({ is }) => {
  185. const inputPath = join(fixtures, 'type-commonjs-cjs', 'file.cjs')
  186. const expectedPkgPath = join(fixtures, 'type-commonjs-cjs', 'package.json')
  187. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  188. is(esm, false)
  189. is(path, inputPath)
  190. is(expectedPkgPath, pkgPath)
  191. is(type, 'commonjs')
  192. is(extType, 'c')
  193. })
  194. test('type-commonjs-js sync', async ({ is }) => {
  195. const inputPath = join(fixtures, 'type-commonjs-js', 'file.js')
  196. const expectedPkgPath = join(fixtures, 'type-commonjs-js', 'package.json')
  197. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  198. is(esm, false)
  199. is(path, inputPath)
  200. is(expectedPkgPath, pkgPath)
  201. is(type, 'commonjs')
  202. is(extType, 'j')
  203. })
  204. test('type-commonjs-mjs sync', async ({ is }) => {
  205. const inputPath = join(fixtures, 'type-commonjs-mjs', 'file.mjs')
  206. const expectedPkgPath = join(fixtures, 'type-commonjs-mjs', 'package.json')
  207. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  208. is(esm, true)
  209. is(path, inputPath)
  210. is(expectedPkgPath, pkgPath)
  211. is(type, 'commonjs')
  212. is(extType, 'm')
  213. })
  214. test('type-module-cjs sync', async ({ is }) => {
  215. const inputPath = join(fixtures, 'type-module-cjs', 'file.cjs')
  216. const expectedPkgPath = join(fixtures, 'type-module-cjs', 'package.json')
  217. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  218. is(esm, false)
  219. is(path, inputPath)
  220. is(expectedPkgPath, pkgPath)
  221. is(type, 'module')
  222. is(extType, 'c')
  223. })
  224. test('type-module-js sync', async ({ is }) => {
  225. const inputPath = join(fixtures, 'type-module-js', 'file.js')
  226. const expectedPkgPath = join(fixtures, 'type-module-js', 'package.json')
  227. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  228. is(esm, true)
  229. is(path, inputPath)
  230. is(expectedPkgPath, pkgPath)
  231. is(type, 'module')
  232. is(extType, 'j')
  233. })
  234. test('type-module-mjs sync', async ({ is }) => {
  235. const inputPath = join(fixtures, 'type-module-mjs', 'file.mjs')
  236. const expectedPkgPath = join(fixtures, 'type-module-mjs', 'package.json')
  237. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  238. is(esm, true)
  239. is(path, inputPath)
  240. is(expectedPkgPath, pkgPath)
  241. is(type, 'module')
  242. is(extType, 'm')
  243. })
  244. test('type-undefined-cjs sync', async ({ is }) => {
  245. const inputPath = join(fixtures, 'type-undefined-cjs', 'file.cjs')
  246. const expectedPkgPath = join(fixtures, 'type-undefined-cjs', 'package.json')
  247. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  248. is(esm, false)
  249. is(path, inputPath)
  250. is(expectedPkgPath, pkgPath)
  251. is(type, undefined)
  252. is(extType, 'c')
  253. })
  254. test('type-undefined-js sync', async ({ is }) => {
  255. const inputPath = join(fixtures, 'type-undefined-js', 'file.js')
  256. const expectedPkgPath = join(fixtures, 'type-undefined-js', 'package.json')
  257. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  258. is(esm, false)
  259. is(path, inputPath)
  260. is(expectedPkgPath, pkgPath)
  261. is(type, undefined)
  262. is(extType, 'j')
  263. })
  264. test('type-undefined-mjs sync', async ({ is }) => {
  265. const inputPath = join(fixtures, 'type-undefined-mjs', 'file.mjs')
  266. const expectedPkgPath = join(fixtures, 'type-undefined-mjs', 'package.json')
  267. const { esm, path, type, extType, pkgPath } = isFileEsm.sync(inputPath)
  268. is(esm, true)
  269. is(path, inputPath)
  270. is(expectedPkgPath, pkgPath)
  271. is(type, undefined)
  272. is(extType, 'm')
  273. })
  274. test('path must be string (sync)', async ({ throws }) => {
  275. throws(() => isFileEsm.sync(), Error(ERR_PATH_MUST_BE_STRING))
  276. throws(() => isFileEsm.sync({}), Error(ERR_PATH_MUST_BE_STRING))
  277. })
  278. test('path must be absolute (sync)', async ({ throws }) => {
  279. throws(() => isFileEsm.sync('../foo.js'), Error(ERR_PATH_MUST_BE_ABSOLUTE))
  280. throws(() => isFileEsm.sync('./bar/foo.mjs'), Error(ERR_PATH_MUST_BE_ABSOLUTE))
  281. })
  282. test('path must have ext (sync)', async ({ throws }) => {
  283. throws(() => isFileEsm.sync('/bar/foo'), Error(ERR_PATH_MUST_HAVE_VALID_EXT))
  284. })
  285. test('path must have valid ext (sync)', async ({ throws }) => {
  286. throws(() => isFileEsm.sync('/bar/foo.ext'), Error(ERR_PATH_MUST_HAVE_VALID_EXT))
  287. })
  288. test('path must exist (promise)', async ({ throws }) => {
  289. throws(() => isFileEsm.sync(join(fixtures, 'bar', 'foo.js')), Error(ERR_PATH_MUST_EXIST))
  290. })
  291. test('type-bad-cjs sync', async ({ is, throws }) => {
  292. const inputPath = join(fixtures, 'type-bad-cjs', 'file.cjs')
  293. const expectedPkgPath = join(fixtures, 'type-bad-cjs', 'package.json')
  294. let error = null
  295. throws(() => {
  296. try {
  297. isFileEsm.sync(inputPath)
  298. } catch (err) {
  299. error = err
  300. throw err
  301. }
  302. }, ERR_BAD_TYPE)
  303. const { path, type, extType, pkgPath } = error.meta
  304. is(path, inputPath)
  305. is(expectedPkgPath, pkgPath)
  306. is(type, 'bad')
  307. is(extType, 'c')
  308. })
  309. test('type-bad-js sync', async ({ is, throws }) => {
  310. const inputPath = join(fixtures, 'type-bad-js', 'file.js')
  311. const expectedPkgPath = join(fixtures, 'type-bad-js', 'package.json')
  312. let error = null
  313. throws(() => {
  314. try {
  315. isFileEsm.sync(inputPath)
  316. } catch (err) {
  317. error = err
  318. throw err
  319. }
  320. }, ERR_BAD_TYPE)
  321. const { path, type, extType, pkgPath } = error.meta
  322. is(path, inputPath)
  323. is(expectedPkgPath, pkgPath)
  324. is(type, 'bad')
  325. is(extType, 'j')
  326. })
  327. test('type-bad-mjs sync', async ({ is, throws }) => {
  328. const inputPath = join(fixtures, 'type-bad-mjs', 'file.mjs')
  329. const expectedPkgPath = join(fixtures, 'type-bad-mjs', 'package.json')
  330. let error = null
  331. throws(() => {
  332. try {
  333. isFileEsm.sync(inputPath)
  334. } catch (err) {
  335. error = err
  336. throw err
  337. }
  338. }, ERR_BAD_TYPE)
  339. const { path, type, extType, pkgPath } = error.meta
  340. is(path, inputPath)
  341. is(expectedPkgPath, pkgPath)
  342. is(type, 'bad')
  343. is(extType, 'm')
  344. })
  345. test('package.json read error propagation (sync)', async ({ throws, tearDown }) => {
  346. const inputPath = join(fixtures, 'type-bad-js', 'file.js')
  347. const expectedPkgPath = join(fixtures, 'type-bad-js', 'package.json')
  348. tearDown(() => chmodSync(expectedPkgPath, 0o644))
  349. chmodSync(expectedPkgPath, 0o000)
  350. throws(() => isFileEsm.sync(inputPath), ERR_PERMISSION_DENIED)
  351. })
  352. test('type-commonjs-cjs callback', async ({ is, error }) => {
  353. const inputPath = join(fixtures, 'type-commonjs-cjs', 'file.cjs')
  354. const expectedPkgPath = join(fixtures, 'type-commonjs-cjs', 'package.json')
  355. const until = when()
  356. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  357. error(err)
  358. is(esm, false)
  359. is(path, inputPath)
  360. is(expectedPkgPath, pkgPath)
  361. is(type, 'commonjs')
  362. is(extType, 'c')
  363. until()
  364. })
  365. await until.done()
  366. })
  367. test('type-commonjs-js callback', async ({ is, error }) => {
  368. const inputPath = join(fixtures, 'type-commonjs-js', 'file.js')
  369. const expectedPkgPath = join(fixtures, 'type-commonjs-js', 'package.json')
  370. const until = when()
  371. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  372. error(err)
  373. is(esm, false)
  374. is(path, inputPath)
  375. is(expectedPkgPath, pkgPath)
  376. is(type, 'commonjs')
  377. is(extType, 'j')
  378. until()
  379. })
  380. await until.done()
  381. })
  382. test('type-commonjs-mjs callback', async ({ is, error }) => {
  383. const inputPath = join(fixtures, 'type-commonjs-mjs', 'file.mjs')
  384. const expectedPkgPath = join(fixtures, 'type-commonjs-mjs', 'package.json')
  385. const until = when()
  386. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  387. error(err)
  388. is(esm, true)
  389. is(path, inputPath)
  390. is(expectedPkgPath, pkgPath)
  391. is(type, 'commonjs')
  392. is(extType, 'm')
  393. until()
  394. })
  395. await until.done()
  396. })
  397. test('type-module-cjs callback', async ({ is, error }) => {
  398. const inputPath = join(fixtures, 'type-module-cjs', 'file.cjs')
  399. const expectedPkgPath = join(fixtures, 'type-module-cjs', 'package.json')
  400. const until = when()
  401. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  402. error(err)
  403. is(esm, false)
  404. is(path, inputPath)
  405. is(expectedPkgPath, pkgPath)
  406. is(type, 'module')
  407. is(extType, 'c')
  408. until()
  409. })
  410. await until.done()
  411. })
  412. test('type-module-js callback', async ({ is, error }) => {
  413. const inputPath = join(fixtures, 'type-module-js', 'file.js')
  414. const expectedPkgPath = join(fixtures, 'type-module-js', 'package.json')
  415. const until = when()
  416. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  417. error(err)
  418. is(esm, true)
  419. is(path, inputPath)
  420. is(expectedPkgPath, pkgPath)
  421. is(type, 'module')
  422. is(extType, 'j')
  423. until()
  424. })
  425. await until.done()
  426. })
  427. test('type-module-mjs callback', async ({ is, error }) => {
  428. const inputPath = join(fixtures, 'type-module-mjs', 'file.mjs')
  429. const expectedPkgPath = join(fixtures, 'type-module-mjs', 'package.json')
  430. const until = when()
  431. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  432. error(err)
  433. is(esm, true)
  434. is(path, inputPath)
  435. is(expectedPkgPath, pkgPath)
  436. is(type, 'module')
  437. is(extType, 'm')
  438. until()
  439. })
  440. await until.done()
  441. })
  442. test('type-undefined-cjs callback', async ({ is, error }) => {
  443. const inputPath = join(fixtures, 'type-undefined-cjs', 'file.cjs')
  444. const expectedPkgPath = join(fixtures, 'type-undefined-cjs', 'package.json')
  445. const until = when()
  446. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  447. error(err)
  448. is(esm, false)
  449. is(path, inputPath)
  450. is(expectedPkgPath, pkgPath)
  451. is(type, undefined)
  452. is(extType, 'c')
  453. until()
  454. })
  455. await until.done()
  456. })
  457. test('type-undefined-js callback', async ({ is, error }) => {
  458. const inputPath = join(fixtures, 'type-undefined-js', 'file.js')
  459. const expectedPkgPath = join(fixtures, 'type-undefined-js', 'package.json')
  460. const until = when()
  461. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  462. error(err)
  463. is(esm, false)
  464. is(path, inputPath)
  465. is(expectedPkgPath, pkgPath)
  466. is(type, undefined)
  467. is(extType, 'j')
  468. until()
  469. })
  470. await until.done()
  471. })
  472. test('type-undefined-mjs callback', async ({ is, error }) => {
  473. const inputPath = join(fixtures, 'type-undefined-mjs', 'file.mjs')
  474. const expectedPkgPath = join(fixtures, 'type-undefined-mjs', 'package.json')
  475. const until = when()
  476. isFileEsm(inputPath, (err, { esm, path, type, extType, pkgPath }) => {
  477. error(err)
  478. is(esm, true)
  479. is(path, inputPath)
  480. is(expectedPkgPath, pkgPath)
  481. is(type, undefined)
  482. is(extType, 'm')
  483. until()
  484. })
  485. await until.done()
  486. })
  487. test('path must be string (callback)', async ({ match }) => {
  488. const until1 = when()
  489. isFileEsm(undefined, (err) => {
  490. match(err, Error(ERR_PATH_MUST_BE_STRING))
  491. until1()
  492. })
  493. const until2 = when()
  494. isFileEsm({}, (err) => {
  495. match(err, Error(ERR_PATH_MUST_BE_STRING))
  496. until2()
  497. })
  498. await Promise.all([until1.done(), until2.done()])
  499. })
  500. test('path must be absolute (callback)', async ({ match }) => {
  501. const until1 = when()
  502. isFileEsm('../foo.js', (err) => {
  503. match(err, Error(ERR_PATH_MUST_BE_ABSOLUTE))
  504. until1()
  505. })
  506. const until2 = when()
  507. isFileEsm('./bar/foo.mjs', (err) => {
  508. match(err, Error(ERR_PATH_MUST_BE_ABSOLUTE))
  509. until2()
  510. })
  511. await Promise.all([until1.done(), until2.done()])
  512. })
  513. test('path must have ext (callback)', async ({ match }) => {
  514. const until = when()
  515. isFileEsm('/bar/foo', (err) => {
  516. match(err, Error(ERR_PATH_MUST_HAVE_VALID_EXT))
  517. until()
  518. })
  519. await until.done()
  520. })
  521. test('path must have valid ext (callback)', async ({ match }) => {
  522. const until = when()
  523. isFileEsm('/bar/foo.ext', (err) => {
  524. match(err, Error(ERR_PATH_MUST_HAVE_VALID_EXT))
  525. until()
  526. })
  527. await until.done()
  528. })
  529. test('path must exist (callback)', async ({ match }) => {
  530. const until = when()
  531. isFileEsm(join(fixtures, 'bar', 'foo.js'), (err) => {
  532. match(err, Error(ERR_PATH_MUST_EXIST))
  533. until()
  534. })
  535. await until.done()
  536. })
  537. test('type-bad-cjs callback', async ({ is }) => {
  538. const inputPath = join(fixtures, 'type-bad-cjs', 'file.cjs')
  539. const expectedPkgPath = join(fixtures, 'type-bad-cjs', 'package.json')
  540. const until = when()
  541. isFileEsm(inputPath, (err) => {
  542. is(err.message, ERR_BAD_TYPE)
  543. const { path, type, extType, pkgPath } = err.meta
  544. is(path, inputPath)
  545. is(expectedPkgPath, pkgPath)
  546. is(type, 'bad')
  547. is(extType, 'c')
  548. until()
  549. })
  550. await until.done()
  551. })
  552. test('type-bad-js callback', async ({ is }) => {
  553. const inputPath = join(fixtures, 'type-bad-js', 'file.js')
  554. const expectedPkgPath = join(fixtures, 'type-bad-js', 'package.json')
  555. const until = when()
  556. isFileEsm(inputPath, (err) => {
  557. is(err.message, ERR_BAD_TYPE)
  558. const { path, type, extType, pkgPath } = err.meta
  559. is(path, inputPath)
  560. is(expectedPkgPath, pkgPath)
  561. is(type, 'bad')
  562. is(extType, 'j')
  563. until()
  564. })
  565. await until.done()
  566. })
  567. test('type-bad-mjs callback', async ({ is }) => {
  568. const inputPath = join(fixtures, 'type-bad-mjs', 'file.mjs')
  569. const expectedPkgPath = join(fixtures, 'type-bad-mjs', 'package.json')
  570. const until = when()
  571. isFileEsm(inputPath, (err) => {
  572. is(err.message, ERR_BAD_TYPE)
  573. const { path, type, extType, pkgPath } = err.meta
  574. is(path, inputPath)
  575. is(expectedPkgPath, pkgPath)
  576. is(type, 'bad')
  577. is(extType, 'm')
  578. until()
  579. })
  580. await until.done()
  581. })
  582. test('package.json read error propagation (callback)', async ({ match, tearDown }) => {
  583. const inputPath = join(fixtures, 'type-bad-js', 'file.js')
  584. const expectedPkgPath = join(fixtures, 'type-bad-js', 'package.json')
  585. tearDown(() => chmodSync(expectedPkgPath, 0o644))
  586. chmodSync(expectedPkgPath, 0o000)
  587. const until = when()
  588. isFileEsm(inputPath, (err) => {
  589. match(err, ERR_PERMISSION_DENIED)
  590. until()
  591. })
  592. await until.done()
  593. })