测试 Deno 应用
使用内置测试运行器、断言和模拟功能,为 Deno 应用编写并运行测试,确保边缘代码可靠。
测试 Deno 应用 是 CoddyKit 上的免费 Edge Computing with Cloudflare Workers & Deno 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Edge Computing with Cloudflare Workers & Deno 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Edge Computing with Cloudflare Workers & Deno 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Built-in Testing
Deno ships with a built-in test runner — no extra framework or config needed. Just write tests and run deno test.
Your First Test
Register a test with Deno.test. It takes a name and a function.
Deno.test('adds numbers', () => {
const sum = 2 + 3;
if (sum !== 5) throw new Error('wrong');
});The Assertions Module
Use the standard library's assertions for clearer failures instead of manual throws.
import { assertEquals } from 'jsr:@std/assert';
Deno.test('math', () => {
assertEquals(2 + 3, 5);
});Running Tests
Run all tests in your project. Deno discovers files ending in _test.ts or .test.ts automatically.
deno test
deno test --allow-netAsync Tests
Tests can be async — just return a promise or mark the function async. Deno awaits it before reporting.
Deno.test('fetches data', async () => {
const res = await fetch('https://example.com');
assertEquals(res.status, 200);
});Test Steps
Break a complex test into named steps with t.step for clearer output and partial results.
Deno.test('user flow', async (t) => {
await t.step('signup', () => {});
await t.step('login', () => {});
});Permissions in Tests
Tests obey Deno's permission model. A test needing network access fails without --allow-net, so you test real security boundaries.
Mocking & Spies
The std testing module provides spies, stubs, and mocks to isolate units from external services.
import { spy } from 'jsr:@std/testing/mock';
const log = spy(console, 'log');Snapshot Testing
Deno's assertSnapshot stores expected output and compares future runs against it — great for stable structured responses.
Coverage Reports
Measure how much code your tests exercise with the built-in coverage tools.
deno test --coverage=cov
deno coverage covTesting Edge Logic
Structure handlers as pure functions of a Request so you can test them without a running server — ideal for edge code.
export function handler(req: Request) {
return new Response('ok');
}Quick Check
Test your Deno testing knowledge.
Recap
You learned to write tests with Deno.test, use std assertions, handle async and steps, respect permissions, mock dependencies, and measure coverage — keeping your edge code reliable.
用 AI 导师学习 Edge Computing with Cloudflare Workers & Deno — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 47
常见问题解答
「测试 Deno 应用」课时是免费的吗?
是的 — 「测试 Deno 应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Edge Computing with Cloudflare Workers & Deno 课程的其余内容,请升级到 CoddyKit PRO。 Edge Computing with Cloudflare Workers & Deno 课程共包含 4 节课。
「测试 Deno 应用」这节课中我会学到什么?
使用内置测试运行器、断言和模拟功能,为 Deno 应用编写并运行测试,确保边缘代码可靠。 你通过在浏览器中直接运行的动手代码来练习 Edge Computing with Cloudflare Workers & Deno,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Edge Computing with Cloudflare Workers & Deno 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Edge Computing with Cloudflare Workers & Deno 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「测试 Deno 应用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Edge Computing with Cloudflare Workers & Deno 课中编写并运行代码吗?
能。每节 Edge Computing with Cloudflare Workers & Deno 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Deno 运行时基础
- 模块系统与权限
- 开发本地 Deno 应用
- 测试 Deno 应用