Skip to content

Commit 3e3bc30

Browse files
committedApr 17, 2024
chore: reduce internal typing of any
1 parent 1b18250 commit 3e3bc30

File tree

10 files changed

+21
-18
lines changed

10 files changed

+21
-18
lines changed
 

‎src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export async function getVitestConfigFromNuxt(
8888
})
8989
}
9090

91-
options.viteConfig.plugins = (options.viteConfig.plugins || []).filter(p => !excludedPlugins.includes((p as any)?.name))
91+
options.viteConfig.plugins = (options.viteConfig.plugins || []).filter(p => !p || !('name' in p) || !excludedPlugins.includes(p.name))
9292

9393
const resolvedConfig = defu(
9494
// overrides

‎src/core/setup/jest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { TestHooks } from '../types'
33
export default async function setupJest(hooks: TestHooks) {
44
const { jest, test, beforeEach, afterAll, afterEach } = await import('@jest/globals')
55

6-
hooks.ctx.mockFn = jest.fn
6+
hooks.ctx.mockFn = jest.fn as (...args: unknown[]) => unknown
77

88
test('setup', hooks.setup, hooks.ctx.options.setupTimeout)
99
beforeEach(hooks.beforeEach)

‎src/core/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface TestContext {
3232
browser?: Browser
3333
url?: string
3434
serverProcess?: ExecaChildProcess
35-
mockFn?: (...args: any[]) => unknown
35+
mockFn?: (...args: unknown[]) => unknown
3636
/**
3737
* Functions to run on the vitest `afterAll` hook.
3838
* Useful for removing anything created during the test.

‎src/environments/vitest/env/happy-dom.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { importModule } from 'local-pkg'
2-
import type { EnvironmentNuxt } from '../types'
2+
import type { EnvironmentNuxt, NuxtWindow } from '../types'
33

44
export default <EnvironmentNuxt> async function (_, { happyDom = {} }) {
5-
const { Window, GlobalWindow } = (await importModule(
6-
'happy-dom',
7-
)) as typeof import('happy-dom')
8-
const window = new (GlobalWindow || Window)(happyDom) as any
5+
const { Window, GlobalWindow } = (await importModule('happy-dom')) as typeof import('happy-dom')
6+
const window = new (GlobalWindow || Window)(happyDom) as InstanceType<typeof GlobalWindow> & NuxtWindow
97

108
return {
119
window,

‎src/experimental.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { $fetch } from './core/server'
88
/**
99
* This is a function to render a component directly with the Nuxt server.
1010
*/
11-
export function $fetchComponent(filepath: string, props?: Record<string, any>) {
11+
export function $fetchComponent(filepath: string, props?: Record<string, unknown>) {
1212
return $fetch(componentTestUrl(filepath, props))
1313
}
1414

15-
export function componentTestUrl(filepath: string, props?: Record<string, any>) {
15+
export function componentTestUrl(filepath: string, props?: Record<string, unknown>) {
1616
const ctx = useTestContext()
1717
filepath = resolve(ctx.options.rootDir, filepath)
1818
const path = stringifyQuery({

‎src/module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default defineNuxtModule<NuxtVitestOptions>({
7070
})
7171

7272
let loaded = false
73-
let promise: Promise<any> | undefined
73+
let promise: Promise<void> | undefined
7474
let ctx: Vitest = undefined!
7575
let testFiles: File[] | null = null
7676

@@ -85,8 +85,8 @@ export default defineNuxtModule<NuxtVitestOptions>({
8585

8686
const viteConfig = await getVitestConfigFromNuxt({ nuxt, viteConfig: defu({ test: options.vitestConfig }, rawViteConfig) })
8787

88-
viteConfig.plugins = (viteConfig.plugins || []).filter((p: any) => {
89-
return !vitePluginBlocklist.includes(p?.name)
88+
viteConfig.plugins = (viteConfig.plugins || []).filter((p) => {
89+
return !p || !('name' in p) || !vitePluginBlocklist.includes(p.name)
9090
})
9191

9292
process.env.__NUXT_VITEST_RESOLVED__ = 'true'

‎src/module/plugins/mock.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export const createMockPlugin = (ctx: MockPluginContext) => createUnplugin(() =>
5555
const mocksComponent: MockComponentInfo[] = []
5656
const importPathsList: Set<string> = new Set()
5757

58-
walk(ast as any, {
58+
// @ts-expect-error mismatch between acorn/estree types
59+
walk(ast, {
5960
enter: (node, parent) => {
6061
// find existing vi import
6162
if (isImportDeclaration(node)) {
@@ -259,7 +260,7 @@ export const createMockPlugin = (ctx: MockPluginContext) => createUnplugin(() =>
259260
const plugins = (config.plugins || []) as Plugin[]
260261

261262
// `vite:mocks` was a typo in Vitest before v0.34.0
262-
const vitestPlugins = plugins.filter(p => (p.name === 'vite:mocks' || p.name.startsWith('vitest:')) && (p.enforce || (p as any).order) === 'post')
263+
const vitestPlugins = plugins.filter(p => (p.name === 'vite:mocks' || p.name.startsWith('vitest:')) && (p.enforce || ('order' in p && p.order === 'post')))
263264
const lastNuxt = findLastIndex(
264265
plugins,
265266
i => i.name?.startsWith('nuxt:'),
@@ -305,8 +306,8 @@ function isExpressionStatement(node: Node | null): node is ExpressionStatement {
305306
}
306307
// TODO: need to fix in rollup types, probably
307308
function startOf(node: Node) {
308-
return 'range' in node && node.range ? node.range[0] : (node as any).start as number
309+
return 'range' in node && node.range ? node.range[0] : ('start' in node ? node.start as number : undefined as never)
309310
}
310311
function endOf(node: Node) {
311-
return 'range' in node && node.range ? node.range[1] : (node as any).end as number
312+
return 'range' in node && node.range ? node.range[1] : ('end' in node ? node.end as number : undefined as never)
312313
}

‎src/playwright.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import { test as base } from '@playwright/test'
23
import type { Page, Response } from 'playwright-core'
34
import type { GotoOptions, TestOptions as SetupOptions, TestHooks } from './e2e'

‎src/runtime-utils/mock.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export function mockComponent<
217217
>
218218
>
219219
): void
220-
export function mockComponent(_path: string, _component: any): void {
220+
export function mockComponent(_path: string, _component: unknown): void {
221221
throw new Error(
222222
'mockComponent() is a macro and it did not get transpiled. This may be an internal bug of @nuxt/test-utils.',
223223
)

‎src/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// TODO: export these
22
// https://github.com/unjs/nitro/tree/main/src/runtime/utils.env.ts
33

4+
// TODO: improve types upstream
5+
/* eslint-disable @typescript-eslint/no-explicit-any */
6+
47
import destr from 'destr'
58
import { snakeCase } from 'scule'
69

0 commit comments

Comments
 (0)
Please sign in to comment.