Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
质量保证
|
41
app.get('/', handlers.home)
app.get('/about', handlers.about)
// 定制404页
app.use(handlers.notFound)
// 定制500页
app.use(handlers.serverError)
现在要测试这些处理函数就更容易了,毕竟它们只是接收
request
和
response
对象的函数。
我们需要验证是否在正确使用这些对象。
5.7.3
 写第一个测试
有多种方式可以识别出
Jest
的测试文件,最常见的有两种,一种是把测试文件放到
__tests__
(
test
前后各有两条下划线)子目录下面
,另一种是使用
.test.js
作为测试文件的
扩展名。我个人喜欢组合使用这两种方式,因为二者各有千秋。把测试放到
__tests__
目录
下面可以避免将源代码目录弄乱(否则,在源代码目录下,什么东西看起来都好像有两
个……每个
foo.js
文件都有一个
foo.test.js
)
。使用
.test.js
作为扩展名则意味着,如果向编
辑器中的一连串标签页看过去,就可以一眼看出哪个是测试文件,哪个是源代码文件。
那么,创建一个名为
lib/__tests__/handlers.test.js
的文件
(版本库的
ch05/lib/__tests__/handlers.
test.js
):
const handlers = require('../handlers')
test('home page renders', () => ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099