@@ -9,6 +9,7 @@ import * as assert from 'uvu/assert';
99import { build , watch } from '../src/index.js' ;
1010import { load_config } from '../src/config.js' ;
1111import { rimraf , walk } from '../src/filesystem.js' ;
12+ import { _create_validator } from '../src/validate.js' ;
1213
1314const __filename = fileURLToPath ( import . meta. url ) ;
1415const __dirname = join ( __filename , '..' ) ;
@@ -214,4 +215,89 @@ if (!process.env.CI) {
214215 } ) ;
215216}
216217
218+ /**
219+ * @param {string[] } actual
220+ * @param {string[] } expected
221+ */
222+ function has_warnings ( actual , expected ) {
223+ assert . equal ( actual . length , expected . length ) ;
224+ assert . equal (
225+ actual . filter ( ( warning ) => expected . some ( ( str ) => warning . startsWith ( str ) ) ) . length ,
226+ expected . length
227+ ) ;
228+ }
229+
230+ test ( 'validates package (1)' , ( ) => {
231+ const { analyse_code, validate } = _create_validator ( {
232+ config : { } ,
233+ cwd : '' ,
234+ input : '' ,
235+ output : '' ,
236+ types : true
237+ } ) ;
238+ analyse_code ( 'src/lib/index.js' , 'export const a = 1;import.meta.env;' ) ;
239+ analyse_code ( 'src/lib/C.svelte' , '' ) ;
240+ const warnings = validate ( { } ) ;
241+
242+ has_warnings ( warnings , [
243+ 'No `exports` field found in `package.json`, please provide one.' ,
244+ 'Avoid usage of `import.meta.env` in your code' ,
245+ 'You are using Svelte components or Svelte-specific imports in your code, but you have not declared a dependency on `svelte` in your `package.json`. '
246+ ] ) ;
247+ } ) ;
248+
249+ test ( 'validates package (2)' , ( ) => {
250+ const { analyse_code, validate } = _create_validator ( {
251+ config : { } ,
252+ cwd : '' ,
253+ input : '' ,
254+ output : '' ,
255+ types : true
256+ } ) ;
257+ analyse_code ( 'src/lib/C.svelte' , '' ) ;
258+ const warnings = validate ( {
259+ exports : { '.' : './dist/C.svelte' } ,
260+ peerDependencies : { svelte : '^3.55.0' }
261+ } ) ;
262+
263+ has_warnings ( warnings , [
264+ 'You are using Svelte files, but did not declare a `svelte` condition in one of your `exports` in your `package.json`. '
265+ ] ) ;
266+ } ) ;
267+
268+ test ( 'validates package (all ok 1)' , ( ) => {
269+ const { analyse_code, validate } = _create_validator ( {
270+ config : { } ,
271+ cwd : '' ,
272+ input : '' ,
273+ output : '' ,
274+ types : true
275+ } ) ;
276+ analyse_code ( 'src/lib/C.svelte' , '' ) ;
277+ const warnings = validate ( {
278+ exports : { '.' : { svelte : './dist/C.svelte' } } ,
279+ peerDependencies : { svelte : '^3.55.0' }
280+ } ) ;
281+
282+ assert . equal ( warnings . length , 0 ) ;
283+ } ) ;
284+
285+ test ( 'validates package (all ok 2)' , ( ) => {
286+ const { analyse_code, validate } = _create_validator ( {
287+ config : { } ,
288+ cwd : '' ,
289+ input : '' ,
290+ output : '' ,
291+ types : true
292+ } ) ;
293+ analyse_code ( 'src/lib/C.svelte' , '' ) ;
294+ const warnings = validate ( {
295+ exports : { '.' : { svelte : './dist/C.svelte' } } ,
296+ peerDependencies : { svelte : '^3.55.0' } ,
297+ svelte : './dist/C.svelte'
298+ } ) ;
299+
300+ assert . equal ( warnings . length , 0 ) ;
301+ } ) ;
302+
217303test . run ( ) ;
0 commit comments