11import { Filename , ppath , xfs } from '@yarnpkg/fslib' ;
2+ import * as loaderFlags from '@yarnpkg/pnp/sources/esm-loader/loaderFlags' ;
23
34describe ( `Plug'n'Play - ESM` , ( ) => {
45 test (
@@ -195,8 +196,8 @@ describe(`Plug'n'Play - ESM`, () => {
195196 ) ,
196197 ) ;
197198
198- test (
199- `it should not resolve JSON files ` ,
199+ ( loaderFlags . HAS_UNFLAGGED_JSON_MODULES === false ? test : test . skip ) (
200+ `it should not resolve JSON modules without --experimental-json-modules ` ,
200201 makeTemporaryEnv (
201202 {
202203 type : `module` ,
@@ -218,6 +219,55 @@ describe(`Plug'n'Play - ESM`, () => {
218219 ) ,
219220 ) ;
220221
222+ ( loaderFlags . HAS_UNFLAGGED_JSON_MODULES ? test : test . skip ) (
223+ `it should not resolve JSON modules without an import assertion` ,
224+ makeTemporaryEnv (
225+ {
226+ type : `module` ,
227+ } ,
228+ async ( { path, run, source} ) => {
229+ await expect ( run ( `install` ) ) . resolves . toMatchObject ( { code : 0 } ) ;
230+
231+ await xfs . writeFilePromise (
232+ ppath . join ( path , `index.js` as Filename ) ,
233+ `import './foo.json';` ,
234+ ) ;
235+ await xfs . writeFilePromise ( ppath . join ( path , `foo.json` as Filename ) , `{"name": "foo"}` ) ;
236+
237+ await expect ( run ( `node` , `./index.js` ) ) . rejects . toMatchObject ( {
238+ code : 1 ,
239+ stderr : expect . stringContaining ( `ERR_IMPORT_ASSERTION_TYPE_MISSING` ) ,
240+ } ) ;
241+ } ,
242+ ) ,
243+ ) ;
244+
245+ ( loaderFlags . HAS_UNFLAGGED_JSON_MODULES ? test : test . skip ) (
246+ `it should resolve JSON modules with an import assertion` ,
247+ makeTemporaryEnv (
248+ {
249+ type : `module` ,
250+ } ,
251+ async ( { path, run, source} ) => {
252+ await expect ( run ( `install` ) ) . resolves . toMatchObject ( { code : 0 } ) ;
253+
254+ await xfs . writeFilePromise (
255+ ppath . join ( path , `index.js` as Filename ) ,
256+ `
257+ import foo from './foo.json' assert { type: 'json' };
258+ console.log(foo.name);
259+ ` ,
260+ ) ;
261+ await xfs . writeFilePromise ( ppath . join ( path , `foo.json` as Filename ) , `{"name": "foo"}` ) ;
262+
263+ await expect ( run ( `node` , `./index.js` ) ) . resolves . toMatchObject ( {
264+ code : 0 ,
265+ stdout : `foo\n` ,
266+ } ) ;
267+ } ,
268+ ) ,
269+ ) ;
270+
221271 test (
222272 `it should respect exports` ,
223273 makeTemporaryEnv (
0 commit comments