@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
2
2
import { parse } from 'acorn-loose'
3
3
import { joinURL , withBase , hasProtocol } from 'ufo'
4
4
import { hash } from 'ohash'
5
+ import { $fetch } from 'ofetch'
5
6
import type { AssetBundlerTransformerOptions } from '../../src/plugins/transform'
6
7
import { NuxtScriptBundleTransformer } from '../../src/plugins/transform'
7
8
import type { IntercomInput } from '~/src/runtime/registry/intercom'
@@ -17,6 +18,15 @@ vi.mock('ohash', async (og) => {
17
18
}
18
19
} )
19
20
21
+ vi . mock ( 'ofetch' , async ( og ) => {
22
+ const mod = ( await og < typeof import ( 'ofetch' ) > ( ) )
23
+ const mock = vi . fn ( mod . $fetch )
24
+ return {
25
+ ...mod ,
26
+ $fetch : mock ,
27
+ }
28
+ } )
29
+
20
30
vi . mock ( 'ufo' , async ( og ) => {
21
31
const mod = ( await og < typeof import ( 'ufo' ) > ( ) )
22
32
const mock = vi . fn ( mod . hasProtocol )
@@ -25,7 +35,9 @@ vi.mock('ufo', async (og) => {
25
35
hasProtocol : mock ,
26
36
}
27
37
} )
28
- vi . stubGlobal ( 'fetch' , vi . fn ( ( ) => Promise . resolve ( { arrayBuffer : vi . fn ( ( ) => Buffer . from ( '' ) ) , ok : true , headers : { get : vi . fn ( ) } } ) ) )
38
+ vi . stubGlobal ( 'fetch' , vi . fn ( ( ) => {
39
+ return Promise . resolve ( { arrayBuffer : vi . fn ( ( ) => Buffer . from ( '' ) ) , ok : true , headers : { get : vi . fn ( ) } } )
40
+ } ) )
29
41
30
42
vi . mock ( '@nuxt/kit' , async ( og ) => {
31
43
const mod = await og < typeof import ( '@nuxt/kit' ) > ( )
@@ -299,9 +311,9 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
299
311
expect ( code . includes ( 'useScript(\'/_scripts/JvFMRwu6zQ.js\', {' ) ) . toBeTruthy ( )
300
312
} )
301
313
302
- describe ( 'fallbackOnSrcOnBundleFail' , ( ) => {
314
+ describe . todo ( 'fallbackOnSrcOnBundleFail' , ( ) => {
303
315
beforeEach ( ( ) => {
304
- vi . mocked ( fetch ) . mockImplementationOnce ( ( ) => Promise . reject ( new Error ( 'fetch error' ) ) )
316
+ vi . mocked ( $ fetch) . mockImplementationOnce ( ( ) => Promise . reject ( new Error ( 'fetch error' ) ) )
305
317
} )
306
318
307
319
const scripts = [ {
@@ -318,15 +330,11 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
318
330
} ,
319
331
} ]
320
332
it ( 'should throw error if bundle fails and fallbackOnSrcOnBundleFail is false' , async ( ) => {
321
- await expect ( async ( ) => await transform ( `const { then } = useScriptNpm({
333
+ await expect ( async ( ) => await transform ( `const instance = useScriptNpm({
322
334
packageName: 'js-confetti',
323
335
file: 'dist/js-confetti.browser.js',
324
- version: '0.12 .0',
336
+ version: '0.15 .0',
325
337
scriptOptions: {
326
- trigger: useScriptTriggerElement({ trigger: 'mouseover', el: mouseOverEl }),
327
- use() {
328
- return { JSConfetti: window.JSConfetti }
329
- },
330
338
bundle: true
331
339
},
332
340
})` , { fallbackOnSrcOnBundleFail : false , scripts } ) ) . rejects . toThrow ( `Failed to fetch` )
@@ -350,7 +358,7 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
350
358
},
351
359
})` , { fallbackOnSrcOnBundleFail : true , scripts } )
352
360
expect ( code ) . toMatchInlineSnapshot ( `
353
- "const instance = useScriptNpm({ scriptInput: { src: 'bundle .js' },
361
+ "const instance = useScriptNpm({ scriptInput: { src: '/_scripts/U6Ua8p1giF .js' },
354
362
packageName: 'js-confetti',
355
363
file: 'dist/js-confetti.browser.js',
356
364
version: '0.12.0',
0 commit comments