diff options
author | Clement Ho <clemmakesapps@gmail.com> | 2017-05-18 16:49:33 +0000 |
---|---|---|
committer | Clement Ho <clemmakesapps@gmail.com> | 2017-05-18 16:49:33 +0000 |
commit | 6ca5a9890c2a6761c932a6da4e82505db4b756f5 (patch) | |
tree | 75b12bc7d4317034becdff9893c8358be77a8fa5 /doc/development | |
parent | 06b614f9fd7a311b9cbc6ad90eecb4cfc5281c78 (diff) | |
parent | 4950dd9792c5d1da401a846ba408bf275f58e332 (diff) | |
download | gitlab-ce-6ca5a9890c2a6761c932a6da4e82505db4b756f5.tar.gz |
Merge branch 'winh-testing-promises' into 'master'
Document Promise testing best practice
See merge request !11284
Diffstat (limited to 'doc/development')
-rw-r--r-- | doc/development/fe_guide/testing.md | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/doc/development/fe_guide/testing.md b/doc/development/fe_guide/testing.md index 5852cac2aa5..5c6bdc97ef6 100644 --- a/doc/development/fe_guide/testing.md +++ b/doc/development/fe_guide/testing.md @@ -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 |