gpt4 book ai didi

javascript - Jasmine 中的全局 `beforeEach`?

转载 作者:IT王子 更新时间:2023-10-29 02:59:42 30 4
gpt4 key购买 nike

我正在使用 Jasmine 编写测试。

我有几个测试文件,每个文件都有一个beforeEach,但它们完全一样。

我如何为他们提供一个global beforeEach

最佳答案

x1a4 的回答让我很困惑。这可能更清楚:

当您在所有 describe block 的外部声明一个 beforeEach 函数时,它将在每次测试之前触发(因此在每个 it)。在 describe block 之前或之后声明 beforeEach 并不重要。

您可以将其包含在测试运行中包含的任何规范文件中——包括在一个单独的文件中,因此规范助手文件的概念可能只包含您的全局 beforeEach 声明。

It's not mentioned in the documentation.

// Example: 

beforeEach(function() {
localStorage.clear();
});

describe('My tests', function() {
describe('Test localstorage', function() {

it('Adds an item to localStorage', function() {
localStorage.setItem('foo', 'bar');
expect(localStorage.getItem('foo')).toBe('bar');
});

it('Is now empty because our beforeEach cleared localStorage', function() {
expect(localStorage.getItem('foo')).toBe(null);
});

});
});

关于javascript - Jasmine 中的全局 `beforeEach`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560716/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com