Skip to content
Permalink
Browse files
updated lab sheet
  • Loading branch information
aa7401 committed Jan 24, 2019
1 parent 73e2b1a commit 0321ed29925cc0c334eeb3ded19f3343addff56e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
@@ -139,12 +139,12 @@ Once the web server is up and running you access the page by pointing the browse

As we develop more complex web apps it becomes more and more difficult to fully test our app every time we change the code. By not testing everything at regular intervals there is an increasing chance that our new code could break the existing code in our system. Since regular testing is a chore, programmers have developed ways to automate this process.

In this section you will be given a sneak preview of how an automated test suite works. We will be using a testing framework developed by Facebook, called [Jest](https://jestjs.io) and will be using a second tool called [Supertest](https://github.com/visionmedia/supertest#readme) which allows us to interact with our code using http.
In this section you will be given a sneak preview of how an automated test suite works. We will be using a testing framework developed by Facebook, called [Jest](https://jestjs.io) and will be using a second tool called [Supertest](https://github.com/visionmedia/supertest#readme) which allows us to interact with our code using http. Rather than using the status code numbers in our tests we will use a module called [http-status-codes](https://www.npmjs.com/package/http-status-codes) to display these as human-readable strings.

The process requires you to install both these packages and then run the `jest` command:

```shell
$ npm install jest supertest
$ npm install jest supertest http-status-codes
$ ./node_modules/.bin/jest
PASS .test/index.test.js
GET /
@@ -0,0 +1,36 @@

'use strict'

const Router = require('koa-router')
const auth = require('koa-simple-auth')
const koaBody = require('koa-body')()
const router = module.exports = new Router()

const catch_api_error = async ctx => {
try{
yield next
} catch(err){
this.body = JSON.stringify({ 'error': err.message })
}
}

router.post('/login',
catch_api_error,
koaBody,
auth.login,
function *() {
this.body = JSON.stringify({ authenticated: true })
}
)

router.post('/register', catch_api_error, koaBody, auth.register, function *() {
this.body = JSON.stringify({ authenticated: true })
})

router.get('/unregister', catch_api_error, koaBody, auth.unregister, function *() {
this.body = JSON.stringify({ authenticated: false })
})

router.get('/logout', auth.logout, function *() {
this.body = JSON.stringify({ authenticated: false })
})

0 comments on commit 0321ed2

Please sign in to comment.