Jest 中的 describe 和 it 有什么区别? [英] What is the difference between describe and it in Jest?

查看:31
本文介绍了Jest 中的 describe 和 it 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用 JestJasmine 编写单元测试时,你什么时候使用 describe?

When writing a unit test in Jest or Jasmine when do you use describe?

你什么时候使用it?

我经常这样做

describe('my beverage', () => {
  test('is delicious', () => {
  });
});

什么时候需要一个新的describe 或一个新的it?

When is it time for a new describe or a new it?

推荐答案

describe 将您的测试套件分解为多个组件.根据您的测试策略,您可能会对类中的每个函数、插件的每个模块或每个面向用户的功能进行描述.

describe breaks your test suite into components. Depending on your test strategy, you might have a describe for each function in your class, each module of your plugin, or each user-facing piece of functionality.

您还可以嵌套描述以进一步细分套件.

You can also nest describes to further subdivide the suite.

it 是您执行单个测试的地方.您应该能够像一个小句子一样描述每个测试,例如它在设置半径时计算面积".您不应该能够进一步细分测试——如果您觉得需要,请改用 describe.

it is where you perform individual tests. You should be able to describe each test like a little sentence, such as "it calculates the area when the radius is set". You shouldn't be able to subdivide tests further-- if you feel like you need to, use describe instead.

describe('Circle class', function() {
  describe('area is calculated when', function() {
    it('sets the radius', function() { ... });
    it('sets the diameter', function() { ... });
    it('sets the circumference', function() { ... });
  });
});

这篇关于Jest 中的 describe 和 it 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆