Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@nuxt/icon": "^2.2.1",
"@nuxt/module-builder": "^1.0.2",
"@nuxt/schema": "^4.3.1",
"@nuxt/test-utils": "^3.23.0",
"@nuxt/test-utils": "^4.0.0",
"@shikijs/transformers": "^3.22.0",
"@types/node": "^24.10.13",
"@vue/test-utils": "^2.4.6",
Expand Down
67 changes: 50 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/basic/.nuxtrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
setups.@nuxt/test-utils="3.23.0"
setups.@nuxt/test-utils="4.0.0"
51 changes: 27 additions & 24 deletions test/runtime/third-party.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import { useNuxtApp } from '#imports'
import { describe, vi, it, expect, beforeEach } from 'vitest'
import { describe, vi, it, expect, beforeEach, beforeAll } from 'vitest'
import plugin from './../../src/runtime/third-party-scripts/plugin.client'

const triggerFn = vi.fn()
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node.nodeName === 'SCRIPT') {
setTimeout(() => {
triggerFn()
}, 15)

describe('third-party', () => {
let callHookSpy: ReturnType<typeof vi.spyOn>
beforeAll(() => {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node.nodeName === 'SCRIPT') {
setTimeout(() => {
triggerFn()
}, 15)
}
}
}
}
}
}
})
observer.observe(document.documentElement, {
childList: true,
subtree: true,
})
})
observer.observe(document.documentElement, {
childList: true,
subtree: true,
})

describe('third-party', () => {
const callHookSpy = vi.spyOn(useNuxtApp(), 'callHook')
plugin(useNuxtApp())
callHookSpy = vi.spyOn(useNuxtApp(), 'callHook')
})

beforeEach(() => {
callHookSpy.mockClear()
triggerFn.mockClear()
})

plugin(useNuxtApp())

it('should call hook on script added', async () => {
const script = document.createElement('script')
script.src = 'https://example.com/third-party.js'
Expand All @@ -39,8 +42,8 @@ describe('third-party', () => {

await vi.waitFor(() => expect(triggerFn).toHaveBeenCalled())

expect(callHookSpy).toHaveBeenCalled()
expect(callHookSpy).toHaveBeenCalledWith('hints:scripts:added', script)
expect(useNuxtApp().callHook).toHaveBeenCalled()
expect(useNuxtApp().callHook).toHaveBeenCalledWith('hints:scripts:added', script)
document.body.removeChild(script)
})

Expand All @@ -51,7 +54,7 @@ describe('third-party', () => {
// wait for mutation observer to trigger
await vi.waitFor(() => expect(triggerFn).toHaveBeenCalled())

expect(callHookSpy).not.toHaveBeenCalledWith('hints:scripts:added', script)
expect(useNuxtApp().callHook).not.toHaveBeenCalledWith('hints:scripts:added', script)
document.body.removeChild(script)
})

Expand All @@ -64,7 +67,7 @@ describe('third-party', () => {
// wait for mutation observer to trigger
await vi.waitFor(() => expect(triggerFn).toHaveBeenCalled())

expect(callHookSpy).not.toHaveBeenCalledWith('hints:scripts:added', expect.anything())
expect(useNuxtApp().callHook).not.toHaveBeenCalledWith('hints:scripts:added', expect.anything())
document.body.removeChild(script)
})
})
Loading