项目作者: titogeorge

项目描述 :
Unit testing NodeJS with mochajs/chai.
高级语言: JavaScript
项目地址: git://github.com/titogeorge/mocha-promises-chai.git
创建时间: 2017-06-16T05:13:39Z
项目社区:https://github.com/titogeorge/mocha-promises-chai

开源协议:The Unlicense

下载


Unit testing NodeJS with mochajs/chai.

This code demonstrates the unit testing of JavaScript Functions using mochajs test framework.

Packages Used:

  • MochaJS - MochaJS is a feature-rich JavaScript test framework.
  • ChaiJS - Chai is a BDD / TDD assertion library for node
  • chai-as-promised - Chai as Promised extends Chai with a fluent language for asserting facts about promises.

Setup

  1. # Install dependencies
  2. $ npm install
  3. # Run Tests
  4. $ npm test
  5. # Run Tests with coverage
  6. $ npm run test_coverage

Mocha Guide to Testing

Objective is to explain describe(), it(), and before()/etc hooks

  • describe() is merely for grouping, which you can nest as deep
  • it() is a test case
  • before(), beforeEach(), after(), afterEach() are hooks to run before/after first/each it() or describe().
  1. var chai = require("chai");
  2. var chaiAsPromised = require("chai-as-promised"); //Required for unit testing functions that returns a Promise
  3. var usersApi = require("../src/users");
  4. chai.use(chaiAsPromised); //For Promise
  5. var expect = chai.expect;
  6. chai.should();
  7. // describe() are:
  8. // - commonly known as test suites, which contains test cases
  9. // - merely groups, and you can have groups within groups
  10. // **Three test cases, but nested 2-level deep**
  11. describe("Users API Test Suite", function () {
  12. describe("Get Users", function () {
  13. // **before() can be applied to describe() too**
  14. before(function(){
  15. console.log('see.. before function is run ONCE only')
  16. });
  17. after(function(){
  18. console.log("see.. after function is run ONCE only; After all the tests. ")
  19. });
  20. beforeEach(function(){
  21. console.log('see.. beforeEach function is run before every it')
  22. });
  23. // First Test Case
  24. // as long as no error within a it(), it() is considered PASSED
  25. it("Returns a List of Users, of Length 10.", function (done) {
  26. usersApi.getUsers(function (err, data) {
  27. expect(data.length).to.equal(10);
  28. done();
  29. });
  30. });
  31. /**
  32. * Demonstrates how to test a function that returns a Promise
  33. * See how clean it is.
  34. */
  35. // Second Test Case
  36. it("Promise Returns a List of Users, of Length 10.", function () {
  37. //This style will only work with chai-as-promised
  38. return usersApi.getUsersPromise().should.eventually.have.length(10);
  39. });
  40. // Third Test Case
  41. it("User 'Glenna Reichert' is in the list.", function (done) {
  42. usersApi.getUsers(function (err, data) {
  43. let found = false;
  44. for (let user of data){
  45. if (user.name === 'Glenna Reichert'){
  46. found = true;
  47. break;
  48. }
  49. }
  50. expect(found).to.equal(true);
  51. done();
  52. });
  53. });
  54. // Fourth Test Case - this will be shown as pending
  55. it('This test will show as pending in report');
  56. })
  57. })

If successful following output is produced.

  1. $ npm test
  2. > social@1.0.0 test H:\grgttg\work\raj\social
  3. > mocha --colors --reporter spec
  4. Comments API Test Suite
  5. Get Comments for Post with id 99
  6. Returns a List of Comments, of Length 5. (78ms)
  7. All posts returned has postId 5
  8. Posts API Test Suite
  9. Get Posts for User with id 10
  10. Returns a List of posts, of Length 10.
  11. All posts returned has userId 10
  12. Users API Test Suite
  13. Get Users
  14. see.. before function is run ONCE only
  15. see.. beforeEach function is run before every it
  16. Returns a List of Users, of Length 10.
  17. see.. beforeEach function is run before every it
  18. Promise Returns a List of Users, of Length 10.
  19. see.. beforeEach function is run before every it
  20. User 'Glenna Reichert' is in the list.
  21. - This test will show as pending in report
  22. see.. after function is run ONCE only; After all the tests.
  23. 7 passing (218ms)
  24. 1 pending

Good Reads Aka Acknowledgements