Skip to content

Commit

Permalink
chore: reduce internal typing of any
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 17, 2024
1 parent 1b18250 commit 3e3bc30
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/setup/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 3 additions & 5 deletions src/environments/vitest/env/happy-dom.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { importModule } from 'local-pkg'
import type { EnvironmentNuxt } from '../types'
import type { EnvironmentNuxt, NuxtWindow } from '../types'

export default <EnvironmentNuxt> 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<typeof GlobalWindow> & NuxtWindow

return {
window,
Expand Down
4 changes: 2 additions & 2 deletions src/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>) {
export function $fetchComponent(filepath: string, props?: Record<string, unknown>) {
return $fetch(componentTestUrl(filepath, props))
}

export function componentTestUrl(filepath: string, props?: Record<string, any>) {
export function componentTestUrl(filepath: string, props?: Record<string, unknown>) {
const ctx = useTestContext()
filepath = resolve(ctx.options.rootDir, filepath)
const path = stringifyQuery({
Expand Down
6 changes: 3 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default defineNuxtModule<NuxtVitestOptions>({
})

let loaded = false
let promise: Promise<any> | undefined
let promise: Promise<void> | undefined
let ctx: Vitest = undefined!
let testFiles: File[] | null = null

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

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'
Expand Down
9 changes: 5 additions & 4 deletions src/module/plugins/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export const createMockPlugin = (ctx: MockPluginContext) => createUnplugin(() =>
const mocksComponent: MockComponentInfo[] = []
const importPathsList: Set<string> = 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)) {
Expand Down Expand Up @@ -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:'),
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions src/playwright.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime-utils/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
)
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down

0 comments on commit 3e3bc30

Please sign in to comment.