Created
December 11, 2018 08:10
-
-
Save whitehorse0/94d5671128ae24574bc60e4f69058e29 to your computer and use it in GitHub Desktop.
Here is an example of using Mocha programmatically for the integration test/ API test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// out scenario test write here | |
const chalk = require('chalk') | |
const expect = require('chai').expect | |
const request = require('./../request') | |
describe(chalk.cyanBright.bold('#GET: request to /user'), function () { | |
let auth = null | |
let user = [] | |
before(async function () { | |
this.timeout(10000) // 10 second | |
auth = await request.login({username: 'user', password: 'password'}) | |
// create user before runing scenario test | |
user = await request.postUser(auth, {name: 'John Doe'}) | |
}) | |
after(function (done) { | |
this.timeout(10000) // 10 second | |
request.deleteUser(auth, user.id) // delete user after runing scenario test | |
done() | |
}) | |
describe('valid request', function () { | |
describe('should return users resource with status code 200', function () { | |
let response = null | |
it('status code is 200', async function () { | |
response = await request.getUser(auth, '?status=active') | |
expect(response).to.have.property('status', 200) | |
}) | |
}) | |
// describe() other positive test | |
}) | |
describe('invalid request', function () { | |
describe('should return property message with status code 400', function () { | |
let response = null | |
it('status code is 400', async function () { | |
response = await request.getUser(null, '?status=null') | |
expect(response).to.have.property('status', 400) | |
}) | |
}) | |
// describe() other negative test | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"mocha-node-test": "node ./tests/api/index.js", | |
"test": "npm run mocha-node-test" | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"chai": "^3.5.0", | |
"chalk": "^2.4.1", | |
"mocha": "^5.2.0", | |
"supertest": "^3.1.0" | |
}, | |
"standard": { | |
"env": [ | |
"mocha", | |
"chai" | |
] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Our event request api testing | |
const request = require('supertest') | |
const headers = { | |
'Content-type': 'application/json', | |
'x-api-version': '0.1', | |
'token': '' | |
} | |
const baseurl = 'http://localhost:3000/api' | |
module.exports = { | |
/** | |
* Request post for user login | |
* @return user object with token | |
*/ | |
login: async function (query) { | |
let response = await request(baseurl).post('/user/login/').send(query); | |
return response; | |
}, | |
/** | |
* Request get for retrive users | |
* @return user list | |
*/ | |
getUser: async function (auth = null, query) { | |
headers.token = auth ? auth.body.token : ''; | |
let response = await request(baseurl).get('/user' + query).set(headers); | |
return response; | |
}, | |
/** | |
* Request post for create user | |
* @return user object | |
*/ | |
postUser: async function (auth = null, query) { | |
headers.token = auth ? auth.body.token : ''; | |
let response = await request(baseurl).post('/user').send(query).set(headers); | |
return response; | |
}, | |
/** | |
* Request delete for remove user | |
* @return message object | |
*/ | |
deleteUser: async function (auth = null, id) { | |
headers.token = auth ? auth.body.token : ''; | |
let response = await request(baseurl).delete('/user/' + id).set(headers); | |
return response; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// registry file Node.js API test | |
const Mocha = require('mocha') | |
const mocha = new Mocha() // Instantiate a Mocha instance. | |
var testsDir = [ | |
'./tests/api/users/' | |
] | |
for (var i = 0; i < testsDir.length; i++) { | |
mocha.addFile(testsDir[i]) | |
} | |
// Run the tests. | |
mocha.run(function (failures) { | |
// exit with non-zero status if there were failures | |
process.exitCode = failures ? -1 : 0 | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment