Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
4950dd97
Commit
4950dd97
authored
May 11, 2017
by
winh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document Promise testing best practice (!11284)
parent
fb31c6ce
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
doc/development/fe_guide/testing.md
doc/development/fe_guide/testing.md
+62
-0
No files found.
doc/development/fe_guide/testing.md
View file @
4950dd97
...
...
@@ -68,6 +68,68 @@ describe('.methodName', () => {
});
});
```
#### Testing Promises
When testing Promises you should always make sure that the test is asynchronous and rejections are handled.
Your Promise chain should therefore end with a call of the
`done`
callback and
`done.fail`
in case an error occurred.
```
javascript
/// Good
it
(
'
tests a promise
'
,
(
done
)
=>
{
promise
.
then
((
data
)
=>
{
expect
(
data
).
toBe
(
asExpected
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
/// Good
it
(
'
tests a promise rejection
'
,
(
done
)
=>
{
promise
.
catch
((
error
)
=>
{
expect
(
error
).
toBe
(
expectedError
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
/// Bad (missing done callback)
it
(
'
tests a promise
'
,
()
=>
{
promise
.
then
((
data
)
=>
{
expect
(
data
).
toBe
(
asExpected
);
})
});
/// Bad (missing catch)
it
(
'
tests a promise
'
,
(
done
)
=>
{
promise
.
then
((
data
)
=>
{
expect
(
data
).
toBe
(
asExpected
);
})
.
then
(
done
)
});
/// Bad (use done.fail in asynchronous tests)
it
(
'
tests a promise
'
,
(
done
)
=>
{
promise
.
then
((
data
)
=>
{
expect
(
data
).
toBe
(
asExpected
);
})
.
then
(
done
)
.
catch
(
fail
)
});
/// Bad (missing catch)
it
(
'
tests a promise rejection
'
,
(
done
)
=>
{
promise
.
catch
((
error
)
=>
{
expect
(
error
).
toBe
(
expectedError
);
})
.
then
(
done
)
});
```
#### Stubbing
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment