当前位置: 首页 > news >正文

上海专业建网站公司营销比较成功的品牌

上海专业建网站公司,营销比较成功的品牌,wordpress主动推送到Google,网络干什么赚钱软件测试 本节介绍如何在 express.js 使用 Jest 进行单元测试 准备工作 准备一个基础的 express 项目,本文基于 evp-express-cli安装 Jest npm install jest --save-dev生成 Jest 配置 npx jest --init编写测试 创建测试文件,以 .test.js 后缀命名…

软件测试

本节介绍如何在 express.js 使用 Jest 进行单元测试

准备工作

  1. 准备一个基础的 express 项目,本文基于 evp-express-cli
  2. 安装 Jest
npm install jest --save-dev
  1. 生成 Jest 配置
npx jest --init

编写测试

创建测试文件,以 .test.js 后缀命名,Jest 在运行期间会自动查找并执行符合 *.test.js 命名的文件,为规范起见,新建一个 test 目录,存放所有的测试文件。

编写第一个测试

  1. 先准备一个要被测试的函数 sum(a, b),写在 src/uitls/index.js 中:
module.exports = {/*** Return the result of the sum of a and b.* @function* @param {number} a * @param {number} b * @returns {number}*/sum: (a, b) => {return a + b;}
}
  1. test 目录下创建 sum.test.js,引入 sum 并编写测试代码:
const { sum } = require('../src/utils');describe('test demo', () => {test('sum test', () => {expect(sum(1, 2)).toBe(3);})
})

toBe() 可以简单得判断相等,类似于 assertEqual,Jest 的具体语法本文不做介绍,自行查阅,其它测试框架如 mocha.js 的语法也是类似的。
3. 执行测试

npx jest

得到结果:

 PASS  test/sum.test.jstest demo√ sum test (2 ms)----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 | index.js |     100 |      100 |     100 |     100 | 
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.58 s, estimated 1 s

对路由进行测试

现在我们尝试对路由进行测试,先安装 supertest

npm install supertest --save-dev

test 目录创建 router.test.js,引入 express app 和 supertest:

const request = require('supertest');
const app = require('../src/app');

接着争对 src/router/index.js 中定义的 / 路由进行测试:

describe('router test', () => {it('GET /', () => {request(app).get('/').expect('Content-Type', /json/).expect(200).then(res => {expect(res.body).toStrictEqual({ok: true, msg: 'Hello World!', data: null, symbol: 1, type: 'Ok' });}).catch(err => console.error(err));})
})

toStrictEqual 用于判断对象的严格相等;对路由测试需要用到 supertest 传入 app,然后对其 / 路由发起 GET 请求,因为请求过程是异步的,所以对响应结果的测试写在 then 回调中,当然你也可以把测试函数写成 async-await 语法糖;这个路由不需要传递任何数据,如果你需要携带数据,可以使用 send (x-www-form-urlencoded), set (请求头), field (multipart-form)等,具体用法请自行查阅 supertest 文档。

接下来执行测试:

npx jest

得到结果:

 PASS  test/sum.test.js   PASS  test/router.test.js
---------------|---------|----------|---------|---------|------------------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|------------------------------
All files      |   62.93 |       60 |      30 |   62.93 |                              src           |   71.76 |    66.66 |     100 |   71.76 |                              app.js       |     100 |      100 |     100 |     100 |                              config.js    |   66.66 |    66.66 |     100 |   66.66 | 36-59                        src/midwares  |   30.18 |      100 |       0 |   30.18 | exhandler.js |   30.18 |      100 |       0 |   30.18 | 7-12,16-22,26-43,47-52      src/model     |      50 |        0 |       0 |      50 | resp.js      |      50 |        0 |       0 |      50 | 4-10,19-25,29-31,35-37,41-43src/router    |   83.33 |      100 |     100 |   83.33 | index.js     |   83.33 |      100 |     100 |   83.33 | 8-9src/utils     |     100 |      100 |     100 |     100 | index.js     |     100 |      100 |     100 |     100 | logger.js    |     100 |      100 |     100 |     100 | 
---------------|---------|----------|---------|---------|------------------------------Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        2.054 s
Ran all test suites.
[2023-08-06 18:43:20] INFO Hello World!

如果你只想执行 router.test.js,则在后边指定要执行的测试文件即可:

npx jest test/router.test.js

下一节-软件构建

http://www.hkea.cn/news/420186/

相关文章:

  • 夸克看网站要钱吗电商网站seo优化
  • 自己做网站排版138ip查询网域名解析
  • 东莞做网站 南城石佳2023网站推广入口
  • 广东省省建设厅网站郴州网站建设网络推广平台
  • 校园网站推广方案怎么做应用商店优化
  • 巩义网站建设网络营销公司是做什么的
  • 做网站基本教程一站式营销平台
  • 杭州模板网站建设电脑培训网上培训班
  • 大连做网站不错的公司怎样把广告放到百度
  • 网站上面带官网字样怎么做的网站设计的流程
  • 有个网站是做视频相册的网球排名即时最新排名
  • 论坛网站备案流程图优化大师怎么提交作业
  • 织梦政府网站模板百度在线入口
  • 专业做婚纱摄影网站会员制营销
  • 网站内容丰富互动营销平台
  • 阿里巴巴logo高清图谷歌seo网站推广
  • 网站如何做内链seo高手是怎样炼成的
  • 设计师个人网站建设怎样注册一个自己的平台
  • 徐州营销网站建设产品线上推广渠道
  • 绍兴市网站建设公司企业官网搭建
  • 关于网页设计的网站免费发布信息网站大全
  • 郑州新闻头条seo基础教程
  • 做网站比较大的公司朔州seo
  • 如何制作私人网站福州专业的seo软件
  • 做网站主流技术南宁在哪里推广网站
  • 老板让我做网站负责人微博营销软件
  • 教我做网站百度打开
  • 网站开发时如何兼容电商运营是做什么的
  • 河北建设银行石家庄分行招聘网站怎么申请自己的网络平台
  • vs2008 做网站搜索引擎的工作原理是什么