gpt4 book ai didi

async-await - Jest 测试是否等待先前的异步测试解决?

转载 作者:行者123 更新时间:2023-12-01 00:14:15 28 4
gpt4 key购买 nike

我读过同一个测试文件中的 Jest 测试是按顺序执行的。我也读过在编写涉及回调的测试时 done应该使用参数。

但是,当使用我在下面的代码中使用的 async/await 语法使用 promise 时,我可以指望测试但按顺序运行和解析吗?

import Client from '../Client';
import { Project } from '../Client/types/client-response';

let client: Client;

beforeAll(async () => {
jest.setTimeout(10000);
client = new Client({ host: 'ws://127.0.0.1', port: 8080 , logger: () => {}});
await client.connect();
})


describe('Create, save and open project', () => {
let project: Project;
let filename: string;

beforeAll(async () => {
// Close project
let project = await client.getActiveProject();
if (project) {
let success = await client.projectClose(project.id, true);
expect(success).toBe(true);
}
})

test('createProject', async () => {
project = await client.createProject();
expect(project.id).toBeTruthy();
});

test('projectSave', async () => {
filename = await client.projectSave(project.id, 'jesttest.otii', true);
expect(filename.endsWith('jesttest.otii')).toBe(true);
});

test('projectClose', async () => {
let success = await client.projectClose(project.id);
expect(success).toBe(true);
});

test('projectOpen', async () => {
project = await client.openProject(filename);
expect(filename.endsWith('jesttest.otii')).toBe(true);
});
})

afterAll(async () => {
await client.disconnect();
})

最佳答案

当然,这取决于配置的测试运行器。说 Jasmine2 it seems impossible同时运行测试:

Because of the single-threadedness of javascript, it isn't really possible to run your tests in parallel in a single browser window



但调查 docs' config section :

--maxConcurrency= Prevents Jest from executing more than the specified amount of tests at the same time. Only affects tests that use test.concurrent.

--maxWorkers=| Alias: -w. Specifies the maximum number of workers the worker-pool will spawn for running tests. This defaults to the number of the cores available on your machine. It may be useful to adjust this in resource limited environments like CIs but the default should be adequate for most use-cases.

For environments with variable CPUs available, you can use percentage based configuration: --maxWorkers=50%



同时查看 jest-runner-concurrent 的描述:

Jest's default runner uses a new child_process (also known as a worker) for each test file. Although the max number of workers is configurable, running a lot of them is slow and consumes tons of memory and CPU.



所以看起来您可以配置并行运行的测试文件数量( maxWorkers )以及单个工作人员范围内的并发测试用例( maxConcurrency )。如果您使用 jest作为测试运行器。这仅影响 test.concurrent() 测试。

出于某种原因,我在 test.concurrent() 上找不到任何内容。在他们的主要文档站点。

无论如何,您可以自己检查您的环境:
describe('checking concurrent execution', () => {
let a = 5;

it('deferred change', (done) => {
setTimeout(() => {
a = 11;
expect(a).toEqual(11);
done();
}, 1000);
});

it('fails if running in concurrency', () => {
expect(a).toEqual(11);
});
})

当然,上面我使用了 Jasmine 的语法( describe , it )所以你可能需要用其他调用来替换它。

关于async-await - Jest 测试是否等待先前的异步测试解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54552074/

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