From 3e3bc3062632f42fb0ccd40a3d919bb4d8a38de4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 17 Apr 2024 10:04:27 +0100 Subject: [PATCH] chore: reduce internal typing of `any` --- src/config.ts | 2 +- src/core/setup/jest.ts | 2 +- src/core/types.ts | 2 +- src/environments/vitest/env/happy-dom.ts | 8 +++----- src/experimental.ts | 4 ++-- src/module.ts | 6 +++--- src/module/plugins/mock.ts | 9 +++++---- src/playwright.ts | 1 + src/runtime-utils/mock.ts | 2 +- src/utils.ts | 3 +++ 10 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/config.ts b/src/config.ts index f890d181e..ac93f2eaa 100644 --- a/src/config.ts +++ b/src/config.ts @@ -88,7 +88,7 @@ export async function getVitestConfigFromNuxt( }) } - options.viteConfig.plugins = (options.viteConfig.plugins || []).filter(p => !excludedPlugins.includes((p as any)?.name)) + options.viteConfig.plugins = (options.viteConfig.plugins || []).filter(p => !p || !('name' in p) || !excludedPlugins.includes(p.name)) const resolvedConfig = defu( // overrides diff --git a/src/core/setup/jest.ts b/src/core/setup/jest.ts index dd587c559..ea0ed8f0e 100644 --- a/src/core/setup/jest.ts +++ b/src/core/setup/jest.ts @@ -3,7 +3,7 @@ import type { TestHooks } from '../types' export default async function setupJest(hooks: TestHooks) { const { jest, test, beforeEach, afterAll, afterEach } = await import('@jest/globals') - hooks.ctx.mockFn = jest.fn + hooks.ctx.mockFn = jest.fn as (...args: unknown[]) => unknown test('setup', hooks.setup, hooks.ctx.options.setupTimeout) beforeEach(hooks.beforeEach) diff --git a/src/core/types.ts b/src/core/types.ts index 734268339..c8a3a6bb2 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -32,7 +32,7 @@ export interface TestContext { browser?: Browser url?: string serverProcess?: ExecaChildProcess - mockFn?: (...args: any[]) => unknown + mockFn?: (...args: unknown[]) => unknown /** * Functions to run on the vitest `afterAll` hook. * Useful for removing anything created during the test. diff --git a/src/environments/vitest/env/happy-dom.ts b/src/environments/vitest/env/happy-dom.ts index a5d649f42..11d8e8c0f 100644 --- a/src/environments/vitest/env/happy-dom.ts +++ b/src/environments/vitest/env/happy-dom.ts @@ -1,11 +1,9 @@ import { importModule } from 'local-pkg' -import type { EnvironmentNuxt } from '../types' +import type { EnvironmentNuxt, NuxtWindow } from '../types' export default async function (_, { happyDom = {} }) { - const { Window, GlobalWindow } = (await importModule( - 'happy-dom', - )) as typeof import('happy-dom') - const window = new (GlobalWindow || Window)(happyDom) as any + const { Window, GlobalWindow } = (await importModule('happy-dom')) as typeof import('happy-dom') + const window = new (GlobalWindow || Window)(happyDom) as InstanceType & NuxtWindow return { window, diff --git a/src/experimental.ts b/src/experimental.ts index 95042d094..90aeac80c 100644 --- a/src/experimental.ts +++ b/src/experimental.ts @@ -8,11 +8,11 @@ import { $fetch } from './core/server' /** * This is a function to render a component directly with the Nuxt server. */ -export function $fetchComponent(filepath: string, props?: Record) { +export function $fetchComponent(filepath: string, props?: Record) { return $fetch(componentTestUrl(filepath, props)) } -export function componentTestUrl(filepath: string, props?: Record) { +export function componentTestUrl(filepath: string, props?: Record) { const ctx = useTestContext() filepath = resolve(ctx.options.rootDir, filepath) const path = stringifyQuery({ diff --git a/src/module.ts b/src/module.ts index 1c390de55..2abd96ba0 100644 --- a/src/module.ts +++ b/src/module.ts @@ -70,7 +70,7 @@ export default defineNuxtModule({ }) let loaded = false - let promise: Promise | undefined + let promise: Promise | undefined let ctx: Vitest = undefined! let testFiles: File[] | null = null @@ -85,8 +85,8 @@ export default defineNuxtModule({ const viteConfig = await getVitestConfigFromNuxt({ nuxt, viteConfig: defu({ test: options.vitestConfig }, rawViteConfig) }) - viteConfig.plugins = (viteConfig.plugins || []).filter((p: any) => { - return !vitePluginBlocklist.includes(p?.name) + viteConfig.plugins = (viteConfig.plugins || []).filter((p) => { + return !p || !('name' in p) || !vitePluginBlocklist.includes(p.name) }) process.env.__NUXT_VITEST_RESOLVED__ = 'true' diff --git a/src/module/plugins/mock.ts b/src/module/plugins/mock.ts index b10e5c214..c4eab6509 100644 --- a/src/module/plugins/mock.ts +++ b/src/module/plugins/mock.ts @@ -55,7 +55,8 @@ export const createMockPlugin = (ctx: MockPluginContext) => createUnplugin(() => const mocksComponent: MockComponentInfo[] = [] const importPathsList: Set = new Set() - walk(ast as any, { + // @ts-expect-error mismatch between acorn/estree types + walk(ast, { enter: (node, parent) => { // find existing vi import if (isImportDeclaration(node)) { @@ -259,7 +260,7 @@ export const createMockPlugin = (ctx: MockPluginContext) => createUnplugin(() => const plugins = (config.plugins || []) as Plugin[] // `vite:mocks` was a typo in Vitest before v0.34.0 - const vitestPlugins = plugins.filter(p => (p.name === 'vite:mocks' || p.name.startsWith('vitest:')) && (p.enforce || (p as any).order) === 'post') + const vitestPlugins = plugins.filter(p => (p.name === 'vite:mocks' || p.name.startsWith('vitest:')) && (p.enforce || ('order' in p && p.order === 'post'))) const lastNuxt = findLastIndex( plugins, i => i.name?.startsWith('nuxt:'), @@ -305,8 +306,8 @@ function isExpressionStatement(node: Node | null): node is ExpressionStatement { } // TODO: need to fix in rollup types, probably function startOf(node: Node) { - return 'range' in node && node.range ? node.range[0] : (node as any).start as number + return 'range' in node && node.range ? node.range[0] : ('start' in node ? node.start as number : undefined as never) } function endOf(node: Node) { - return 'range' in node && node.range ? node.range[1] : (node as any).end as number + return 'range' in node && node.range ? node.range[1] : ('end' in node ? node.end as number : undefined as never) } diff --git a/src/playwright.ts b/src/playwright.ts index 9c667a45d..1fa81f0f3 100644 --- a/src/playwright.ts +++ b/src/playwright.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { test as base } from '@playwright/test' import type { Page, Response } from 'playwright-core' import type { GotoOptions, TestOptions as SetupOptions, TestHooks } from './e2e' diff --git a/src/runtime-utils/mock.ts b/src/runtime-utils/mock.ts index 02a7fa649..a9c5f2793 100644 --- a/src/runtime-utils/mock.ts +++ b/src/runtime-utils/mock.ts @@ -217,7 +217,7 @@ export function mockComponent< > > ): void -export function mockComponent(_path: string, _component: any): void { +export function mockComponent(_path: string, _component: unknown): void { throw new Error( 'mockComponent() is a macro and it did not get transpiled. This may be an internal bug of @nuxt/test-utils.', ) diff --git a/src/utils.ts b/src/utils.ts index b221ad767..c522b0e14 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,9 @@ // TODO: export these // https://github.com/unjs/nitro/tree/main/src/runtime/utils.env.ts +// TODO: improve types upstream +/* eslint-disable @typescript-eslint/no-explicit-any */ + import destr from 'destr' import { snakeCase } from 'scule'