1
1
// set the app NODE_ENV environment variable to 'test' in case the app is set up to alter its behavior in such case
2
2
// in our case, the morgan logging module is turned off when this is set to 'test'
3
- process . env . NODE_ENV = "test"
3
+ process . env . NODE_ENV = "test" ;
4
4
5
5
// include the testing dependencies
6
- const chai = require ( "chai" )
7
- const chaiHttp = require ( "chai-http" )
8
- chai . use ( chaiHttp ) // use the chai-http middleware to simplify testing routes
9
- const expect = chai . expect // the assertion library in the style using the word 'expect'
10
- const should = chai . should ( ) // the same assertion library in the style using the word 'should'
6
+ const chai = require ( "chai" ) ;
7
+ const chaiHttp = require ( "chai-http" ) ;
8
+ chai . use ( chaiHttp ) ; // use the chai-http middleware to simplify testing routes
9
+ const expect = chai . expect ; // the assertion library in the style using the word 'expect'
10
+ const should = chai . should ( ) ; // the same assertion library in the style using the word 'should'
11
11
12
12
// import the server
13
- const server = require ( "../app" )
13
+ const server = require ( "../app" ) ;
14
14
15
15
// a group of tests related to the /protected route
16
16
describe ( "Protected" , ( ) => {
@@ -24,11 +24,11 @@ describe("Protected", () => {
24
24
. request ( server )
25
25
. get ( "/protected" )
26
26
. end ( ( err , res ) => {
27
- res . should . have . status ( 401 ) // use 'should' to make BDD-style assertions
28
- done ( ) // resolve the Promise that these tests create so mocha can move on
29
- } )
30
- } )
31
- } )
27
+ res . should . have . status ( 401 ) ; // use 'should' to make BDD-style assertions
28
+ done ( ) ; // resolve the Promise that these tests create so mocha can move on
29
+ } ) ;
30
+ } ) ;
31
+ } ) ;
32
32
33
33
/**
34
34
* test the GET /protected route when logged in
@@ -37,38 +37,38 @@ describe("Protected", () => {
37
37
// test a protected route when logged in... passport auth should allow it
38
38
39
39
// let's first create a valid JWT token to use in the requests where we want to be logged in
40
- const jwt = require ( "jsonwebtoken" )
41
- const User = require ( "../models/User" )
42
- const user = new User ( { username : "test" , password : "test" } )
43
- const token = user . generateJWT ( )
40
+ const jwt = require ( "jsonwebtoken" ) ;
41
+ const User = require ( "../models/User" ) ;
42
+ const user = new User ( { username : "test" , password : "test" } ) ;
43
+ const token = user . generateJWT ( ) ;
44
44
45
45
it ( "it should return a 200 HTTP response code" , done => {
46
46
chai
47
47
. request ( server )
48
48
. get ( "/protected" )
49
49
. set ( "Authorization" , `JWT ${ token } ` ) // set JWT authentication headers to simulate a logged-in user, using the token we created at top
50
50
. end ( ( err , res ) => {
51
- res . should . have . status ( 200 ) // use should to make BDD-style assertions
52
- done ( ) // resolve the Promise that these tests create so mocha can move on
53
- } )
54
- } )
51
+ res . should . have . status ( 200 ) ; // use should to make BDD-style assertions
52
+ done ( ) ; // resolve the Promise that these tests create so mocha can move on
53
+ } ) ;
54
+ } ) ;
55
55
56
56
it ( "it should return an object with specific properties" , done => {
57
57
chai
58
58
. request ( server )
59
59
. get ( "/protected" )
60
60
. set ( "Authorization" , `JWT ${ token } ` ) // set JWT authentication headers to simulate a logged-in user, using the token we created at top
61
61
. end ( ( err , res ) => {
62
- res . body . should . be . a ( "object" ) // our route sends back an object
62
+ res . body . should . be . a ( "object" ) ; // our route sends back an object
63
63
// res.body.should.have.property("success")
64
64
// res.body.should.have.property("user")
65
65
// res.body.should.have.property("message")
66
- res . body . should . have . keys ( "success" , "user" , "message" ) // a way to test the presence of an exact set of keys in the response object
67
- expect ( res . body ) . to . have . deep . property ( "user.id" , 1 ) // check for exact value of a nested value
68
- expect ( res . body ) . to . have . deep . property ( "user.username" ) // check for existence of a nested value
66
+ res . body . should . have . keys ( "success" , "user" , "message" ) ; // a way to test the presence of an exact set of keys in the response object
67
+ expect ( res . body ) . to . have . deep . property ( "user.id" , 1 ) ; // check for exact value of a nested value
68
+ expect ( res . body ) . to . have . deep . property ( "user.username" ) ; // check for existence of a nested value
69
69
70
- done ( ) // resolve the Promise that these tests create so mocha can move on
71
- } )
72
- } )
73
- } )
74
- } )
70
+ done ( ) ; // resolve the Promise that these tests create so mocha can move on
71
+ } ) ;
72
+ } ) ;
73
+ } ) ;
74
+ } ) ;
0 commit comments