Skip to main content
On this page

deno test

Deno 附带了一个内置的测试运行器,使用 Deno.test() API。要了解如何编写测试,请参见 测试基础指南。有关断言,请参见 @std/assert@std/expect

运行测试 Jump to heading

运行当前目录及其子目录中的所有测试:

>_
deno test

运行特定文件中的测试:

>_
deno test src/fetch_test.ts src/signal_test.ts

运行匹配 glob 模式的测试:

>_
deno test src/*.test.ts

运行名称匹配字符串或模式的测试:

>_
deno test --filter "database"
deno test --filter "/^connect.*/"

跳过类型检查:

>_
deno test --no-check

权限 Jump to heading

测试会以与 deno run 相同的权限模型运行。 为你的测试套件授予权限:

>_
deno test --allow-read --allow-net

监听模式 Jump to heading

在文件更改时自动重新运行测试:

>_
deno test --watch

并行执行 Jump to heading

在多个 worker 线程之间运行测试文件:

>_
deno test --parallel

默认情况下,--parallel 使用可用 CPU 的数量。使用 DENO_JOBS=<N> 来控制线程数量:

>_
DENO_JOBS=4 deno test --parallel

代码覆盖率 Jump to heading

收集覆盖率数据并生成报告:

>_
deno test --coverage

这会将原始覆盖率数据写入 coverage/ 目录。要基于已有的覆盖率数据生成摘要, 请使用 deno coverage

>_
deno coverage coverage/

你也可以输出 lcov 报告以供外部工具使用:

>_
deno coverage --lcov coverage/ > coverage.lcov

报告器 Jump to heading

使用 --reporter 选择输出格式:

>_
deno test --reporter=dot
deno test --reporter=tap

为 CI 系统写入 JUnit XML 报告:

>_
deno test --junit-path=report.xml

随机化顺序 Jump to heading

打乱测试运行顺序,以发现测试之间隐藏的依赖关系:

>_
deno test --shuffle

泄漏检测 Jump to heading

追踪泄漏的异步操作、计时器或资源的来源:

>_
deno test --trace-leaks

在文档中测试代码 Jump to heading

将 JSDoc 和 Markdown 文件中的代码块作为测试执行:

>_
deno test --doc

有关详细信息,请参见文档中的代码测试

Type checking options Jump to heading

--check Jump to heading

Set type-checking behavior. This subcommand type-checks local modules by default, so adding --check is redundant If the value of "all" is supplied, remote modules will be included. Alternatively, the 'deno check' subcommand can be used.

--no-check Jump to heading

Skip type-checking. If the value of "remote" is supplied, diagnostic errors from remote modules will be ignored.

Dependency management options Jump to heading

--cached-only Jump to heading

Require that remote dependencies are already cached.

--frozen Jump to heading

Error out if lockfile is out of date.

--import-map Jump to heading

Load import map file from local file or remote URL.

--lock Jump to heading

Check the specified lock file. (If value is not provided, defaults to "./deno.lock").

--no-lock Jump to heading

Disable auto discovery of the lock file.

--no-npm Jump to heading

Do not resolve npm modules.

--no-remote Jump to heading

Do not resolve remote modules.

--node-modules-dir Jump to heading

Sets the node modules management mode for npm packages.

--reload Jump to heading

Short flag: -r

Reload source code cache (recompile TypeScript) no value Reload everything jsr:@std/http/file-server,jsr:@std/assert/assert-equals Reloads specific modules npm: Reload all npm modules npm:chalk Reload specific npm module.

--vendor Jump to heading

Toggles local vendor folder usage for remote modules and a node_modules folder for npm packages.

Options Jump to heading

--allow-scripts Jump to heading

Allow running npm lifecycle scripts for the given packages Note: Scripts will only be executed when using a node_modules directory (--node-modules-dir).

--cert Jump to heading

Load certificate authority from PEM encoded file.

--conditions Jump to heading

Use this argument to specify custom conditions for npm package exports. You can also use DENO_CONDITIONS env var. .

--config Jump to heading

Short flag: -c

Configure different aspects of deno including TypeScript, linting, and code formatting. Typically the configuration file will be called deno.json or deno.jsonc and automatically detected; in that case this flag is not necessary.

--env-file Jump to heading

Load environment variables from local file Only the first environment variable with a given key is used. Existing process environment variables are not overwritten, so if variables with the same names already exist in the environment, their values will be preserved. Where multiple declarations for the same environment variable exist in your .env file, the first one encountered is applied. This is determined by the order of the files you pass as arguments.

--ext Jump to heading

Set content type of the supplied file.

--hide-stacktraces Jump to heading

Hide stack traces for errors in failure test results.

--ignore Jump to heading

Ignore files.

--location Jump to heading

Value of globalThis.location used by some web APIs.

--minimum-dependency-age Jump to heading

(Unstable) The age in minutes, ISO-8601 duration or RFC3339 absolute timestamp (e.g. '120' for two hours, 'P2D' for two days, '2025-09-16' for cutoff date, '2025-09-16T12:00:00+00:00' for cutoff time, '0' to disable).

--no-config Jump to heading

Disable automatic loading of the configuration file.

--parallel Jump to heading

Run test modules in parallel. Parallelism defaults to the number of available CPUs or the value of the DENO_JOBS environment variable.

--preload Jump to heading

A list of files that will be executed before the main module.

--require Jump to heading

A list of CommonJS modules that will be executed before the main module.

--seed Jump to heading

Set the random number generator seed.

--v8-flags Jump to heading

To see a list of all available flags use --v8-flags=--help Flags can also be set via the DENO_V8_FLAGS environment variable. Any flags set with this flag are appended after the DENO_V8_FLAGS environment variable.

Debugging options Jump to heading

--inspect Jump to heading

Activate inspector on host:port [default: 127.0.0.1:9229]. Host and port are optional. Using port 0 will assign a random free port.

--inspect-brk Jump to heading

Activate inspector on host:port, wait for debugger to connect and break at the start of user script.

--inspect-wait Jump to heading

Activate inspector on host:port and wait for debugger to connect before running user code.

Testing options Jump to heading

--clean Jump to heading

Empty the temporary coverage profile data directory before running tests. Note: running multiple deno test --clean`` calls in series or parallel for the same coverage directory may cause race conditions.

--coverage Jump to heading

Collect coverage profile data into DIR. If DIR is not specified, it uses 'coverage/'. This option can also be set via the DENO_COVERAGE_DIR environment variable.

--coverage-raw-data-only Jump to heading

Only collect raw coverage data, without generating a report.

--doc Jump to heading

Evaluate code blocks in JSDoc and Markdown.

--fail-fast Jump to heading

Stop after N errors. Defaults to stopping after first failure.

--filter Jump to heading

Run tests with this string or regexp pattern in the test name.

--junit-path Jump to heading

Write a JUnit XML test report to PATH. Use '-' to write to stdout which is the default when PATH is not provided.

--no-run Jump to heading

Cache test modules, but don't run tests.

--permit-no-files Jump to heading

Don't return an error code if no files were found.

--reporter Jump to heading

Select reporter to use. Default to 'pretty'.

--shuffle Jump to heading

Shuffle the order in which the tests are run.

--trace-leaks Jump to heading

Enable tracing of leaks. Useful when debugging leaking ops in test, but impacts test execution time.

File watching options Jump to heading

--no-clear-screen Jump to heading

Do not clear terminal screen when under watch mode.

--watch Jump to heading

Watch for file changes and restart process automatically. Local files from entry point module graph are watched by default. Additional paths might be watched by passing them as arguments to this flag.

--watch-exclude Jump to heading

Exclude provided files/patterns from watch mode.

Last updated on

你找到了你需要的东西吗?

编辑此页面
隐私政策