@@ -1054,18 +1054,17 @@ Our test coverage is currently lacking. Some requests like <i>GET /api/notes/:id
1054
1054
Below is an example of the test file after making some minor improvements:
1055
1055
1056
1056
` ` ` js
1057
- const { test , after , beforeEach , describe } = require (' node:test' )
1058
1057
const assert = require (' node:assert' )
1058
+ const { test , after , beforeEach , describe } = require (' node:test' )
1059
1059
const mongoose = require (' mongoose' )
1060
1060
const supertest = require (' supertest' )
1061
1061
const app = require (' ../app' )
1062
- const api = supertest (app)
1063
-
1064
1062
const helper = require (' ./test_helper' )
1065
-
1066
1063
const Note = require (' ../models/note' )
1067
1064
1068
- describe (' when there are some notes saved initially' , () => {
1065
+ const api = supertest (app)
1066
+
1067
+ describe (' when there is initially some notes saved' , () => {
1069
1068
beforeEach (async () => {
1070
1069
await Note .deleteMany ({})
1071
1070
await Note .insertMany (helper .initialNotes )
@@ -1079,23 +1078,21 @@ describe('when there are some notes saved initially', () => {
1079
1078
})
1080
1079
1081
1080
test (' all notes are returned' , async () => {
1082
- const response = await api . get ( ' /api/notes ' )
1081
+ const notes = await helper . notesInDb ( )
1083
1082
1084
- assert .strictEqual (response . body .length , helper .initialNotes .length )
1083
+ assert .strictEqual (notes .length , helper .initialNotes .length )
1085
1084
})
1086
1085
1087
1086
test (' a specific note is within the returned notes' , async () => {
1088
- const response = await api . get ( ' /api/notes ' )
1087
+ const notes = await helper . notesInDb ( )
1089
1088
1090
- const contents = response . body . map (r => r .content )
1089
+ const contents = notes . map (n => n .content )
1091
1090
assert (contents .includes (' HTML is easy' ))
1092
1091
})
1093
1092
1094
1093
describe (' viewing a specific note' , () => {
1095
-
1096
1094
test (' succeeds with a valid id' , async () => {
1097
1095
const notesAtStart = await helper .notesInDb ()
1098
-
1099
1096
const noteToView = notesAtStart[0 ]
1100
1097
1101
1098
const resultNote = await api
@@ -1109,17 +1106,13 @@ describe('when there are some notes saved initially', () => {
1109
1106
test (' fails with statuscode 404 if note does not exist' , async () => {
1110
1107
const validNonexistingId = await helper .nonExistingId ()
1111
1108
1112
- await api
1113
- .get (` /api/notes/${ validNonexistingId} ` )
1114
- .expect (404 )
1109
+ await api .get (` /api/notes/${ validNonexistingId} ` ).expect (404 )
1115
1110
})
1116
1111
1117
1112
test (' fails with statuscode 400 id is invalid' , async () => {
1118
1113
const invalidId = ' 5a3d5da59070081a82a3445'
1119
1114
1120
- await api
1121
- .get (` /api/notes/${ invalidId} ` )
1122
- .expect (400 )
1115
+ await api .get (` /api/notes/${ invalidId} ` ).expect (400 )
1123
1116
})
1124
1117
})
1125
1118
@@ -1144,14 +1137,9 @@ describe('when there are some notes saved initially', () => {
1144
1137
})
1145
1138
1146
1139
test (' fails with status code 400 if data invalid' , async () => {
1147
- const newNote = {
1148
- important: true
1149
- }
1140
+ const newNote = { important: true }
1150
1141
1151
- await api
1152
- .post (' /api/notes' )
1153
- .send (newNote)
1154
- .expect (400 )
1142
+ await api .post (' /api/notes' ).send (newNote).expect (400 )
1155
1143
1156
1144
const notesAtEnd = await helper .notesInDb ()
1157
1145
@@ -1164,16 +1152,14 @@ describe('when there are some notes saved initially', () => {
1164
1152
const notesAtStart = await helper .notesInDb ()
1165
1153
const noteToDelete = notesAtStart[0 ]
1166
1154
1167
- await api
1168
- .delete (` /api/notes/${ noteToDelete .id } ` )
1169
- .expect (204 )
1155
+ await api .delete (` /api/notes/${ noteToDelete .id } ` ).expect (204 )
1170
1156
1171
1157
const notesAtEnd = await helper .notesInDb ()
1172
1158
1173
- assert .strictEqual (notesAtEnd .length , helper .initialNotes .length - 1 )
1174
-
1175
- const contents = notesAtEnd .map (r => r .content )
1159
+ const contents = notesAtEnd .map (n => n .content )
1176
1160
assert (! contents .includes (noteToDelete .content ))
1161
+
1162
+ assert .strictEqual (notesAtEnd .length , helper .initialNotes .length - 1 )
1177
1163
})
1178
1164
})
1179
1165
})
0 commit comments