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: don't use global timers #1026

Merged
merged 1 commit into from
Mar 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
}
]
}
},
{
"files": "packages/vitest/**/*.*",
"rules": {
"no-restricted-globals": [
"error",
"setTimeout",
"clearTimeout",
"setInterval",
"clearInterval",
"performance"
]
}
}
]
}
2 changes: 1 addition & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import c from 'picocolors'
import { ViteNodeServer } from 'vite-node/server'
import type { ArgumentsType, Reporter, ResolvedConfig, UserConfig } from '../types'
import { SnapshotManager } from '../integrations/snapshot/manager'
import { deepMerge, hasFailed, noop, slash, toArray } from '../utils'
import { clearTimeout, deepMerge, hasFailed, noop, setTimeout, slash, toArray } from '../utils'
import { cleanCoverage, reportCoverage } from '../integrations/coverage'
import { ReportersMap } from './reporters'
import { createPool } from './pool'
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/reporters/dot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { UserConsoleLog } from '../../types'
import { setTimeout } from '../../utils'
import { BaseReporter } from './base'
import { createDotRenderer } from './renderers/dotRenderer'
import type { createListRenderer } from './renderers/listRenderer'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLogUpdate } from 'log-update'
import c from 'picocolors'
import type { Task } from '../../../types'
import { getTests } from '../../../utils'
import { clearInterval, getTests, setInterval } from '../../../utils'

export interface DotRendererOptions {
outputStream: NodeJS.WritableStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import c from 'picocolors'
import cliTruncate from 'cli-truncate'
import stripAnsi from 'strip-ansi'
import type { SuiteHooks, Task } from '../../../types'
import { getTests } from '../../../utils'
import { clearInterval, getTests, setInterval } from '../../../utils'
import { F_RIGHT } from '../../../utils/figures'
import { getCols, getHookStateSymbol, getStateSymbol } from './utils'

Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/runtime/collect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { performance } from 'perf_hooks'
import { createHash } from 'crypto'
import { relative } from 'pathe'
import type { File, ResolvedConfig, Suite, TaskBase } from '../types'
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Awaitable, DoneCallback, RuntimeContext, SuiteCollector, TestFunction } from '../types'
import { getWorkerState } from '../utils'
import { clearTimeout, getWorkerState, setTimeout } from '../utils'

export const context: RuntimeContext = {
tasks: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { performance } from 'perf_hooks'
import type { File, HookListener, ResolvedConfig, Suite, SuiteHooks, Task, TaskResult, TaskState, Test } from '../types'
import { vi } from '../integrations/vi'
import { getSnapshotClient } from '../integrations/snapshot/chai'
import { getFullName, getWorkerState, hasFailed, hasTests, partitionSuiteChildren } from '../utils'
import { clearTimeout, getFullName, getWorkerState, hasFailed, hasTests, partitionSuiteChildren, setTimeout } from '../utils'
import { getState, setState } from '../integrations/chai/jest-expect'
import { takeCoverage } from '../integrations/coverage'
import { getFn, getHooks } from './map'
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Console } from 'console'
import { Writable } from 'stream'
import { environments } from '../integrations/env'
import type { ResolvedConfig } from '../types'
import { getWorkerState, toArray } from '../utils'
import { clearTimeout, getWorkerState, setTimeout, toArray } from '../utils'
import * as VitestIndex from '../index'
import { resetRunOnceCounter } from '../integrations/run-once'
import { RealDate } from '../integrations/mockdate'
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './tasks'
export * from './path'
export * from './base'
export * from './global'
export * from './timers'

export const isWindows = process.platform === 'win32'

Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/utils/timers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const setTimeout = globalThis.setTimeout
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about importing them form timers module, but then it would be hard to mock them in browser env, I think

export const setInterval = globalThis.setInterval
export const clearInterval = globalThis.clearInterval
export const clearTimeout = globalThis.clearTimeout