组件库质量保证 | 青训营笔记

130 阅读4分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 13 天

为保证组件库的质量,需要我们借助外部的工具进行开发质量检测,包括代码提交、代码测试等。

安装 Husky

npm install husky@4.2.5 --save-dev

Husky 是一个 Git 钩子工具,对 git 执行的一些命令,通过对应的 hooks 钩子触发,执行自定义的脚本程序。它用于规范代码提交,保证代码质量。

在 package.json 中添加如下配置,实现在 git commit 时,自动执行 npm run lint 命令。

"husky": {
  "hooks": {
    "pre-commit": "npm run lint"
  }
}

git commit 规范

核心语法如下:

type(scope?): subject  #scope is optional; multiple scopes are supported (current delimiter options: "/", "\" and ",")
typecommit 的类型
feat新功能、新特性
fix修改 bug
perf更改代码,以提高性能
refactor代码重构(重构,在不影响代码内部行为、功能下的代码修改)
docs文档修改
style代码格式修改, 注意不是 css 修改(例如分号修改)
test测试用例新增、修改
build影响项目构建或依赖项修改
revert恢复上一次提交
ci持续集成相关文件修改
chore其他修改(不在上述类型中的修改)
release发布新版本
workflow工作流相关文件修改

示例:

commit message描述
chore: init初始化项目
chore: update deps更新依赖
chore: wording调整文字(措词)
chore: fix typos修复拼写错误
chore: release v1.0.0发布 1.0.0 版本
fix: icon size修复图标大小
fix: value.length -> values.lengthvalue 变量调整为 values
feat(blog): add comment sectionblog 新增评论部分
feat: support typescript新增 typescript 支持
feat: improve xxx types改善 xxx 类型
style(component): code调整 component 代码样式
refactor: xxx重构 xxx
perf(utils): random function优化 utils 的 random 函数
docs: xxx.md添加 xxx.md 文章

CSS

CSS 变量的位置在 src/_variables.scss 文件中。存储了基础设计系统。

normalize.css

normalize.css 是一个 CSS reset 文件,用于重置浏览器的默认样式。在 src/_normalize.scss 文件中。它会保护你的网站在不同浏览器中的一致性。并为大部分浏览器提供了一些有用的默认样式。修复了一些浏览器的 bug,保证了一些浏览器的一致性。

normalize.css。本项目中,该文件是 src/_reboot.scss。做了一些变量替换,来适应本项目的设计系统。

组件测试

测试的意义

  • 高质量的代码
  • 保证代码的可靠性,更早发现问题
  • 保证代码的可维护性,让重构更加轻松

React 为什么适合单元测试

  • Component 组件
  • Function 函数
  • 单向数据流

Jest

Jest 是一个 JavaScript 测试框架,由 Facebook 开发。它可以让你的测试用例更加简单、快速、可靠。地址:Jest

断言即测试一个值是否是预期的值。Jest 提供了一些断言方法。

test('two plus two is four', () => {
  expect(2 + 2).toBe(4);  // 等于
  expect(2 + 2).not.toBe(5);  // 不等于
});

期望 2 + 2 等于 4

运行:

npx jest jest.test.js

可以使用 watch 参数,实现自动化测试。

npx jest jest.test.js --watch

布尔类型的断言方法:

test('object assignment', () => {
  expect(1).toBeTruthy();  // 真
  expect(0).toBeFalsy();  // 假
});

数字类型的断言方法:

test('two plus two', () => {
  const value = 2 + 2;
  expect(value).toBeGreaterThan(3);  // 大于
  expect(value).toBeGreaterThanOrEqual(3.5);  // 大于等于
  expect(value).toBeLessThan(5);  // 小于
  expect(value).toBeLessThanOrEqual(4.5);  // 小于等于
});

对象类型的断言方法:

test('there is no I in team', () => {
  expect({name: 'test'}).not.toBe({name: 'test1'});  // 不相等
  expect({name: 'test'}).toEqual({name: 'test'});  // 相等,值相等
  expect({name: 'test'}).toBe({name: 'test'});  // 相等,完全相等
});

React-Testing-Library

React-Testing-Library 是一个 React 测试工具,它可以让你的测试用例更加简单、快速、可靠。地址:React-Testing-Library

在 create-react-app 中,已经集成了 React-Testing-Library。

来测试按钮组件:

test('our first react test case', () => {
  const wrapper = render(<Button>Nice</Button>) // render the component
  const element = wrapper.queryByText('Nice') // find the element
  expect(element).toBeTruthy() // check if the element exists
  expect(element).toBeInTheDocument() // check if the element is in the document
})

上面的测试用例先渲染了 Button 组件,然后找到了文本为 Nice 的元素,最后断言这个元素存在。

运行:

npx jest button.test.js

如果发生了错误,会显示错误信息。

test('our first react test case', () => {
  const wrapper = render(<Button>Nice</Button>) // render the component
  const element = wrapper.queryByText('Nice1') // find the element
  expect(element).toBeTruthy() // check if the element exists
  expect(element).toBeInTheDocument() // check if the element is in the document
})

例如,上面的测试用例中,找到的元素文本为 Nice1,而不是 Nice,所以会报错。

× our first react test case (29 ms)

  ● our first react test case

    expect(received).toBeTruthy()

    Received: null

       6 |   const wrapper = render(<Button>Nice</Button>) // render the component
       7 |   const element = wrapper.queryByText('Nice1') // find the element
    >  8 |   expect(element).toBeTruthy() // check if the element exists
         |                   ^
       9 |   expect(element).toBeInTheDocument() // check if the element is in the document
      10 | })

jest-dom

jest-dom 是一个扩展的断言方法,它可以让你的测试用例更加简单、快速、可靠。地址:jest-dom

它为我们添加了针对 dom 类型的断言方法。例如:

  • toBeDisabled
  • toBeEmpty
  • toBeEnabled
  • toBeEmptyDOMElement
  • toBeInTheDocument
  • toBeInvalid
  • toBeRequired
  • toBeValid
  • toBeVisible
  • toContainElement

新版的 Create-React-App 已经集成了 jest-dom,所以我们可以直接使用。