Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove setInterval from the presets #1035

Merged
merged 1 commit into from
Dec 7, 2024
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
15 changes: 14 additions & 1 deletion examples/app-vitest-full/tests/nuxt/auto-import.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'

describe('auto-imports', () => {
it('can use core nuxt composables within test file', () => {
Expand All @@ -17,4 +17,17 @@ describe('auto-imports', () => {
expect(useAutoImportedTarget()).toMatchInlineSnapshot('"the original"')
expect(useAutoImportedNonTarget()).toMatchInlineSnapshot('"the original"')
})

it('setInterval is not auto-imported', () => {
vi.useFakeTimers()

let triggerd = false
setInterval(() => {
triggerd = true
}, 1000)

vi.advanceTimersByTime(1000)

expect(triggerd).toBe(true)
})
})
8 changes: 8 additions & 0 deletions src/module/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export function setupImportMocking() {
ctx.components = _
})

nuxt.hook('imports:sources', (presets) => {
// because the native setInterval cannot be mocked
const idx = presets.findIndex(p => p.imports.includes('setInterval'))
if (idx !== -1) {
presets.splice(idx, 1)
}
})

// We want to run Nuxt plugins on test files
nuxt.options.ignore = nuxt.options.ignore.filter(i => i !== '**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}')
if (nuxt._ignore) {
Expand Down
Loading