Skip to content

Commit

Permalink
chore(deps): update dependency @antfu/eslint-config to v3 (#6435)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com>
  • Loading branch information
renovate[bot] and sheremet-va authored Sep 2, 2024
1 parent c3cc497 commit 84a066a
Show file tree
Hide file tree
Showing 37 changed files with 736 additions and 879 deletions.
5 changes: 3 additions & 2 deletions docs/api/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -1141,9 +1141,10 @@ This assertion checks if a value satisfies a certain predicate.

```ts
import { describe, expect, it } from 'vitest'
describe('toSatisfy()', () => {
const isOdd = (value: number) => value % 2 !== 0

const isOdd = (value: number) => value % 2 !== 0

describe('toSatisfy()', () => {
it('pass with 0', () => {
expect(1).toSatisfy(isOdd)
})
Expand Down
8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ export default antfu(
'no-restricted-globals': 'off',
},
},
{
files: [
`test/${GLOB_SRC}`,
],
rules: {
'unicorn/consistent-function-scoping': 'off',
},
},
{
files: [`packages/vite-node/${GLOB_SRC}`],
rules: {
Expand Down
8 changes: 4 additions & 4 deletions examples/lit/test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import '../src/my-button.js'

describe('Button with increment', async () => {
function getInsideButton(): HTMLElement | null | undefined {
return document.body.querySelector('my-button')?.shadowRoot?.querySelector('button')
}

beforeEach(() => {
document.body.innerHTML = '<my-button name="World"></my-button>'
})
Expand All @@ -31,3 +27,7 @@ describe('Button with increment', async () => {
expect(spyClick).toHaveBeenCalled()
})
})

function getInsideButton(): HTMLElement | null | undefined {
return document.body.querySelector('my-button')?.shadowRoot?.querySelector('button')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test:browser:playwright": "pnpm -C test/browser run test:playwright"
},
"devDependencies": {
"@antfu/eslint-config": "^2.25.1",
"@antfu/eslint-config": "^3.0.0",
"@antfu/ni": "^0.22.1",
"@playwright/test": "^1.46.0",
"@rollup/plugin-commonjs": "^26.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/client/tester/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ If needed, mock the \`${name}\` call manually like:
import { expect, vi } from "vitest"
vi.spyOn(window, "${name}")${
defaultValue ? `.mockReturnValue(${JSON.stringify(defaultValue)})` : ''
}
defaultValue ? `.mockReturnValue(${JSON.stringify(defaultValue)})` : ''
}
${name}(${formatedParams})
expect(${name}).toHaveBeenCalledWith(${formatedParams})
\`\`\``)
Expand Down
96 changes: 53 additions & 43 deletions packages/browser/src/client/tester/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,6 @@ export function setupConsoleLogSpy() {
count,
countReset,
} = console
const formatInput = (input: unknown) => {
if (typeof input === 'object') {
return stringify(input, undefined, {
printBasicPrototype: false,
escapeString: false,
})
}
return format(input)
}
const processLog = (args: unknown[]) => args.map(formatInput).join(' ')
const sendLog = (
type: 'stdout' | 'stderr',
content: string,
disableStack?: boolean,
) => {
if (content.startsWith('[vite]')) {
return
}
const unknownTestId = '__vitest__unknown_test__'
// @ts-expect-error untyped global
const taskId = globalThis.__vitest_worker__?.current?.id ?? unknownTestId
const origin
= getConfig().printConsoleTrace && !disableStack
? new Error('STACK_TRACE').stack?.split('\n').slice(1).join('\n')
: undefined
rpc().sendLog({
origin,
content,
browser: true,
time: Date.now(),
taskId,
type,
size: content.length,
})
}
const stdout = (base: (...args: unknown[]) => void) => (...args: unknown[]) => {
base(...args)
sendLog('stdout', processLog(args))
}
const stderr = (base: (...args: unknown[]) => void) => (...args: unknown[]) => {
base(...args)
sendLog('stderr', processLog(args))
}
console.log = stdout(log)
console.debug = stdout(debug)
console.info = stdout(info)
Expand Down Expand Up @@ -136,3 +93,56 @@ export function setupConsoleLogSpy() {
countLabels[label] = 0
}
}

function stdout(base: (...args: unknown[]) => void) {
return (...args: unknown[]) => {
base(...args)
sendLog('stdout', processLog(args))
}
}
function stderr(base: (...args: unknown[]) => void) {
return (...args: unknown[]) => {
base(...args)
sendLog('stderr', processLog(args))
}
}

function formatInput(input: unknown) {
if (typeof input === 'object') {
return stringify(input, undefined, {
printBasicPrototype: false,
escapeString: false,
})
}
return format(input)
}

function processLog(args: unknown[]) {
return args.map(formatInput).join(' ')
}

function sendLog(
type: 'stdout' | 'stderr',
content: string,
disableStack?: boolean,
) {
if (content.startsWith('[vite]')) {
return
}
const unknownTestId = '__vitest__unknown_test__'
// @ts-expect-error untyped global
const taskId = globalThis.__vitest_worker__?.current?.id ?? unknownTestId
const origin
= getConfig().printConsoleTrace && !disableStack
? new Error('STACK_TRACE').stack?.split('\n').slice(1).join('\n')
: undefined
rpc().sendLog({
origin,
content,
browser: true,
time: Date.now(),
taskId,
type,
size: content.length,
})
}
8 changes: 4 additions & 4 deletions packages/browser/src/client/tester/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ function createCdp() {

const listeners: Record<string, Function[]> = {}

const error = (err: unknown) => {
window.dispatchEvent(new ErrorEvent('error', { error: err }))
}

const cdp = {
send(method: string, params?: Record<string, any>) {
return rpc().sendCdpEvent(contextId, method, params)
Expand Down Expand Up @@ -113,3 +109,7 @@ function createCdp() {

return cdp
}

function error(err: unknown) {
window.dispatchEvent(new ErrorEvent('error', { error: err }))
}
33 changes: 17 additions & 16 deletions packages/browser/src/node/commands/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ export const keyboard: UserEventCommand<(text: string, state: KeyboardState) =>
text,
state,
) => {
function focusIframe() {
if (
!document.activeElement
|| document.activeElement.ownerDocument !== document
|| document.activeElement === document.body
) {
window.focus()
}
}

if (context.provider instanceof PlaywrightBrowserProvider) {
const frame = await context.frame()
await frame.evaluate(focusIframe)
Expand All @@ -40,12 +30,6 @@ export const keyboard: UserEventCommand<(text: string, state: KeyboardState) =>
context.contextId,
text,
async () => {
function selectAll() {
const element = document.activeElement as HTMLInputElement
if (element && element.select) {
element.select()
}
}
if (context.provider instanceof PlaywrightBrowserProvider) {
const frame = await context.frame()
await frame.evaluate(selectAll)
Expand Down Expand Up @@ -165,3 +149,20 @@ export async function keyboardImplementation(
pressed,
}
}

function focusIframe() {
if (
!document.activeElement
|| document.activeElement.ownerDocument !== document
|| document.activeElement === document.body
) {
window.focus()
}
}

function selectAll() {
const element = document.activeElement as HTMLInputElement
if (element && element.select) {
element.select()
}
}
20 changes: 10 additions & 10 deletions packages/browser/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { createDebugger } from 'vitest/node'

const debug = createDebugger('vitest:browser:pool')

async function waitForTests(
method: 'run' | 'collect',
contextId: string,
project: WorkspaceProject,
files: string[],
) {
const context = project.browser!.state.createAsyncContext(method, contextId, files)
return await context
}

export function createBrowserPool(ctx: Vitest): ProcessPool {
const providers = new Set<BrowserProvider>()

const waitForTests = async (
method: 'run' | 'collect',
contextId: string,
project: WorkspaceProject,
files: string[],
) => {
const context = project.browser!.state.createAsyncContext(method, contextId, files)
return await context
}

const executeTests = async (method: 'run' | 'collect', project: WorkspaceProject, files: string[]) => {
ctx.state.clearFiles(project, files)
const browser = project.browser!
Expand Down
Loading

0 comments on commit 84a066a

Please sign in to comment.