小编Sha*_*han的帖子

在 Jest 中,定义全局变量是否与在 BeforeAll 中定义的相同?

使用Jest编写单元测试时。为什么要使用beforeAll来简单地将值直接分配给全局变量,反之亦然?

例如,以下两个片段之间有什么区别?

片段 1

const mock = { key1: 'val1', key2: 'val2' };

describe('Test Cases', () => {
  test('Case 1', () => {
    // tests that use mock variable
  });

  test('Case 2', () => {
    // more tests that use mock variable
  });
});
Run Code Online (Sandbox Code Playgroud)

片段 2

const mock = {};

beforeAll(() => {
  mock.key1 = 'val1';
  mock.key2 = 'val2';
});

describe('Test Cases', () => {
  test('Case 1', () => {
    // tests that use mock variable …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing jestjs

11
推荐指数
1
解决办法
5616
查看次数

标签 统计

javascript ×1

jestjs ×1

unit-testing ×1