Mocha termasuk pustaka pengetesan yang bisa dipakai secara BDD ataupun TDD. Pustaka ini secara default memakai style BDD. Metode umum BDD adalah seperti describe, it, beforeEach, beforeAfter, before, after. (http://idjs.github.io/belajar-nodejs/testing/mocha.html)
Chai library yang digunakan untuk memudahkan Mocha dalam melakukan assertion pada setiap API yang dipanggil. (https://medium.com/@albertkurnia/api-testing-dengan-mocha-chai-6543ff03010b)
SuperTest yaitu modul npm yang khusus untuk mengetest server HTTP. (https://idjs.github.io/belajar-nodejs/testing/rest_testing.html)
- Install Node.js and npm, we will use npm to install Mocha & Chai and SuperTest
- MacOS Catalina version 10.15.2
- node version
^v12.14.1
- npm version
^6.14.4
$ cd mochaiwithsupertest
$ npm install
$ cp .env.sample .env
.
├── api
├── data
| ├── data_test.json
| └── schema
├── helpers
| ├── grafana.js
| ├── random_string.js
| ├── message.json
| └── response.json
├── reports
| ├── report.json
| └── report.html
├── tests
├── .env
└── package.json
Directory | Description |
---|---|
api | 1 endpoint, many HTTP method in one file .js |
data | For test data, file extention should be .json |
helpers | Common code, for general needed such as response_code, token, etc |
tests | 1 endpoint, 1 HTTP method, many test cases. or e2e |
- Filename using
snake_case
- Variable name using
camelCase
- The file (.js and .json) should be located in a path that matches the endpoint path
- Env Variable using
UPPER_CASE
GET {{url}}/authors/books
--page: /api/authors/books.js
--test: /test/authors/get_books_test.js
--data: /data/authors/book_data.json
You can specify the command that you want to run from package.json file.
$ npm run test-api | to run all tests
$ npm run test-api -- --grep @tag | to run test with specific tag
$ npm run test-api -- --grep @skip --invert | to exclude @skip tag
$ npm run open-reports | to open the mochawesome report