Skip to content

Commit 58150a0

Browse files
Add StackBlitz example for test timeouts
1 parent 9a106dd commit 58150a0

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

docs/07-test-timeouts.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/07-test-timeouts.md)
44

5+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/timeouts?file=test.js&terminal=test&view=editor)
6+
57
Timeouts in AVA behave differently than in other test frameworks. AVA resets a timer after each test, forcing tests to quit if no new test results were received within the specified timeout. This can be used to handle stalled tests.
68

79
The default timeout is 10 seconds.

examples/timeouts/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const delay = ms => {
4+
return new Promise(resolve => {
5+
setTimeout(resolve, ms);
6+
});
7+
};
8+
9+
exports.fetchUsers = async () => {
10+
await delay(50);
11+
12+
return [
13+
{
14+
id: 1,
15+
firstName: 'Ava',
16+
name: 'Rocks',
17+
18+
}
19+
];
20+
};
21+
22+
exports.fetchPosts = async userId => {
23+
await delay(200);
24+
25+
return [
26+
{
27+
id: 1,
28+
userId,
29+
message: 'AVA Rocks 🚀'
30+
}
31+
];
32+
};
33+
34+
exports.createPost = async message => {
35+
await delay(3000);
36+
37+
return {
38+
id: 2,
39+
userId: 1,
40+
message
41+
};
42+
};

examples/timeouts/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "ava-timeouts",
3+
"description": "Example for test timeouts",
4+
"scripts": {
5+
"test": "ava --timeout=2s --verbose"
6+
},
7+
"devDependencies": {
8+
"ava": "^3.15.0"
9+
}
10+
}

examples/timeouts/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Test timeouts
2+
3+
> Example for [test timeouts](https://github.com/avajs/ava/blob/main/docs/07-test-timeouts.md)
4+
5+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/timeouts?file=test.js&terminal=test&view=editor)

examples/timeouts/test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
const test = require('ava');
3+
4+
const {fetchUsers, fetchPosts, createPost} = require('.');
5+
6+
test('retrieve users', async t => {
7+
t.timeout(100);
8+
9+
const users = await fetchUsers();
10+
11+
t.deepEqual(users, [
12+
{
13+
id: 1,
14+
firstName: 'Ava',
15+
name: 'Rocks',
16+
17+
}
18+
]);
19+
});
20+
21+
test('retrieve posts', async t => {
22+
t.timeout(100, 'retrieving posts is too slow');
23+
24+
const posts = await fetchPosts(1);
25+
26+
t.deepEqual(posts, [
27+
{
28+
id: 1,
29+
userId: 1,
30+
message: 'AVA Rocks 🚀'
31+
}
32+
]);
33+
});
34+
35+
test('create post', async t => {
36+
const post = await createPost('I love 🦄 and 🌈');
37+
38+
t.deepEqual(post, {
39+
id: 2,
40+
userId: 1,
41+
message: 'I love 🦄 and 🌈'
42+
});
43+
});

0 commit comments

Comments
 (0)