Open yaofly2012 opened 4 years ago
路线:
test suite
测试套件test suite
;test suite
可以视为一个无形的Root Describe
。test suite
没有test suite
时执行jest
会提示:
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 0 matches
testPathIgnorePatterns: \\node_modules\\ - 3 matches
testRegex: - 0 matches
node_modules
目录。默认的匹配规则:[ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ]
__tests__
目录下的.js
, .jsx
, .ts
, .tsx
文件;test
或则spec
后端的.js
, .jsx
, .ts
, .tsx
文件。describe
describe
可以对测试用例(test
)进行分组,也定义了hooks
作用域。describe
可以嵌套,即describe
里除了可以包含tests
, hooks
之外还可以包含子describe
。Hooks
& 作用域describe
或者test
的执行前后都有Hook函数,通过Hook函数可以执行一些公共逻辑,比如:初始化,资源释放等。
beforeAll
, beforeEach
afterAll
, afterEach
这里的All
和Each
指的是test
。
Hooks
是声明在Describe
里的(Test suite
视为Root Describe
),Hooks
的作用域就是其Describe
块。test
, it
test
和it
用来声明具体的测试用例。it
只是test
的别名;it's about readability not about functionality
区别在于表达的方式:
// test if xxxx
describe('yourModule', () => {
test('if it does this thing', () => {});
test('if it does the other thing', () => {});
});
// it should xxxx
describe('yourModule', () => {
it('should do this thing', () => {});
it('should do the other thing', () => {});
});
Matchers
:匹配器真实值
;test
里可以有多个匹配器,只有当所有匹配器通过才算是test
通过;expect
JS单元测试(Unit Test, UT)
一、理论(TDD,BDD)
前端单元测验 TDD Vs BDD
二、JS测试框架Jest/Mocha/Ava的简单比较
三、如何写UT?