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
Boxiang Sun
gitlab-ce
Commits
6ca5a989
Commit
6ca5a989
authored
May 18, 2017
by
Clement Ho
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'winh-testing-promises' into 'master'
Document Promise testing best practice See merge request !11284
parents
06b614f9
4950dd97
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 @
6ca5a989
...
@@ -68,6 +68,68 @@ describe('.methodName', () => {
...
@@ -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
#### 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