相关疑难解决方法(0)

如何将变量从beforeEach挂钩传递到测试中?

beforeEach(async () => {
  const sandbox = sinon.sandbox.create()
  ...
})

test('/add', () => {
  // how can I use sandbox here?
})
Run Code Online (Sandbox Code Playgroud)

我需要的是类似t.contextava

jestjs

16
推荐指数
1
解决办法
4267
查看次数

如何在beforeAll/beforeEach和Jest中的测试之间共享数据?

我们使用jest来测试我们的API并且具有非常复杂的场景.我们使用这些beforeAll函数为每个测试设置一般辅助变量,有时设置租户分离,在其他情况下,我们使用beforeEach函数为测试设置租户分离,为测试租户设置一些默认配置,...

例如,测试可能喜欢这样的东西(正如你所看到的,我们使用TypeScript来编写测试,以防万一):

let apiClient: ApiClient;
let tenantId: string;

beforeAll(async () => {
    apiClient = await getClientWithCredentials();
});

beforeEach(async () => {
    tenantId = await createNewTestTenant();
});

describe('describing complex test scenario', () => {
    it('should have some initial state', async () => {
        await checkState(tenantId);
    });

    it('should have some state after performing op1', async () =>{
        await op1(tenantId);
        await checkStateAfterOp1(tenantId);
    });

    it('should have some state after performing op2', async () =>{
        await op2(tenantId);
        await checkStateAfterOp2(tenantId);
    });

    it('should …
Run Code Online (Sandbox Code Playgroud)

async-await typescript jest web-api-testing

8
推荐指数
1
解决办法
1592
查看次数

标签 统计

async-await ×1

jest ×1

jestjs ×1

typescript ×1

web-api-testing ×1