Skip to content

Instantly share code, notes, and snippets.

@hillal20
Forked from luishrd/.gitignore
Created May 24, 2018 19:11
Show Gist options
  • Save hillal20/df4ce1ec0fcb1a609d0a0e7e09e7bf74 to your computer and use it in GitHub Desktop.
Save hillal20/df4ce1ec0fcb1a609d0a0e7e09e7bf74 to your computer and use it in GitHub Desktop.
Server Testing
node_modules
.DS_Store
.vscode

User Story

As a: sales exec I want: to login to the system So that: i can see my sales for the day

Scenarios

  • given: a username and password
  • when: the username is valid
    • and: the password is incorrect
  • then: the system should not allow login
    • and: should return an error with 401 status code
    • and: should show the following message to the user: "you shall not pass"
    • and: should redirect the user to the login page

created new empty directory git init

Users.

Jest uses jsdom as the default environment. Change it to node in jest key inside package.json;

{
"name": "testingserver",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "cross-env NODE_ENV=test jest test --watch --verbose"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cross-env": "^5.1.6",
"jest": "^22.4.4",
"supertest": "^3.1.0"
},
"jest": {
"testEnvironment": "node"
},
"dependencies": {
"bcrypt": "^2.0.1",
"express": "^4.16.3",
"mongoose": "^5.1.2"
}
}
const express = require('express');
const server = express();
server.get('/', (req, res) => {
res.status(200).json({ api: 'running!' });
});
if (process.env.NODE_ENV !== 'test') {
server.listen(9000);
}
module.exports = server;
const request = require('supertest');
const server = require('./server');
describe('server', () => {
it('should return Ok and a json object from the index route', async () => {
const expectedBody = { api: 'running!' };
const response = await request(server).get('/');
expect(response.status).toEqual(200);
expect(response.type).toEqual('application/json');
expect(response.body).toEqual(expectedBody);
});
});
// /users/User.js
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const userSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
},
password: String,
});
userSchema.pre('save', function(next) {
bcrypt
.hash(this.password, 10)
.then(hash => {
this.password = hash;
next();
})
.catch(err => {
next(err);
});
});
module.exports = mongoose.model('User', userSchema);
// /users/User.spec.js
const mongoose = require('mongoose');
const bcrypt = require('bcrypt'); // yarn added this
const User = require('./User');
describe('User model', () => {
beforeAll(() => {
return mongoose
.connect('mongodb://localhost/testingdb')
.then(console.log('connected to test db'));
});
beforeEach(() => {
// return User.remove();
});
afterEach(() => {
return User.remove();
});
afterAll(() => {
return mongoose.disconnect();
});
it('should hash the password before saving the user', async () => {
const user = { username: 'frodo', password: 'irrelevant' };
const savedUser = await User.create(user); // new + save
expect(savedUser.password).not.toEqual(user.password);
expect(savedUser.password).toHaveLength(60);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment