Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/henryckh/node-tdd-example
Nodejs with test driven development.
https://github.com/henryckh/node-tdd-example
chai mocha node-tdd
Last synced: about 2 months ago
JSON representation
Nodejs with test driven development.
- Host: GitHub
- URL: https://github.com/henryckh/node-tdd-example
- Owner: henryckh
- Created: 2018-05-30T02:17:57.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T00:56:14.000Z (almost 2 years ago)
- Last Synced: 2023-03-08T08:34:08.112Z (almost 2 years ago)
- Topics: chai, mocha, node-tdd
- Language: JavaScript
- Homepage:
- Size: 288 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-tdd-example
Project example using Mocha, chai to do test driven development for Node.js.## Prerequisites
Install Mocha globally by running:
```sh
$ npm install -g mocha
```Install mongodb and run at port 27017
During filling npm init, input mocha for "test command",
Your package.json file should contain the following field:
```js
"script": {
"test": "mocha"
}
````## Test http request
Use supertest to test api from express
```js
describe('Feed API', () => {
describe('Get Image list', () => {
it('should return images of link in array', (done) => {
api.get('/images')
.expect(200)
.end((err, res) => {
if (err) done(err);expect(res.body).to.have.property('images');
expect(res.body.images).to.be.an('array');
done();
});
})
});
});
```## Test function execution
Test logging with sandbox
```js
describe('Sanbox Winston Log', () => {
describe('Log an info level message in sandbox', () => {beforeEach(() => {
this.sandbox = sinon.createSandbox();
});
it('should logged "hello world"', () => {
sinon.stub(logger, 'info');
logger.info('hello world');
expect(logger.info).to.have.been.calledWith('hello world');
});
afterEach(() => {
this.sandbox.restore()
});
})
});
```## Test writing to Database
```js
describe('User Controller', () => {
describe('Create a new user', () => {before(() => {
db.connect(()=> {
});
});it('should return saved document with property "name"', () => {
let tom = new User({
name: "Tom"
});
return expect(userController.create(tom)).to.eventually.has.property('name');
});
})
});
```