1- const bcrypt = require ( "bcrypt" ) ;
1+ import { test , mock } from "node:test" ;
2+ import assert from "node:assert/strict" ;
3+
4+ import bcrypt from "bcrypt" ;
25
36const users = [
47 {
@@ -52,28 +55,34 @@ const mockRequest = (sessionData, body) => ({
5255
5356const mockResponse = ( ) => {
5457 const res = { } ;
55- res . status = jest . fn ( ) . mockReturnValue ( res ) ;
56- res . json = jest . fn ( ) . mockReturnValue ( res ) ;
58+ res . status = mock . fn ( ) ;
59+ res . status . mock . mockImplementation ( ( ) => res ) ;
60+ res . json = mock . fn ( ) ;
61+ res . status . mock . mockImplementation ( ( ) => res ) ;
5762 return res ;
5863} ;
5964
6065test ( "should 400 if username is missing from body" , async ( ) => {
6166 const req = mockRequest ( { } , { password : "boss" } ) ;
6267 const res = mockResponse ( ) ;
6368 await login ( req , res ) ;
64- expect ( res . status ) . toHaveBeenCalledWith ( 400 ) ;
65- expect ( res . json ) . toHaveBeenCalledWith ( {
66- message : "username and password are required" ,
67- } ) ;
69+ assert . deepEqual ( res . status . mock . calls [ 0 ] . arguments , [ 400 ] ) ;
70+ assert . deepEqual ( res . json . mock . calls [ 0 ] . arguments , [
71+ {
72+ message : "username and password are required" ,
73+ } ,
74+ ] ) ;
6875} ) ;
6976test ( "should 400 if password is missing from body" , async ( ) => {
7077 const req = mockRequest ( { } , { username : "hugo" } ) ;
7178 const res = mockResponse ( ) ;
7279 await login ( req , res ) ;
73- expect ( res . status ) . toHaveBeenCalledWith ( 400 ) ;
74- expect ( res . json ) . toHaveBeenCalledWith ( {
75- message : "username and password are required" ,
76- } ) ;
80+ assert . deepEqual ( res . status . mock . calls [ 0 ] . arguments , [ 400 ] ) ;
81+ assert . deepEqual ( res . json . mock . calls [ 0 ] . arguments , [
82+ {
83+ message : "username and password are required" ,
84+ } ,
85+ ] ) ;
7786} ) ;
7887test ( "should 401 with message if user with passed username does not exist" , async ( ) => {
7988 const req = mockRequest (
@@ -85,10 +94,12 @@ test("should 401 with message if user with passed username does not exist", asyn
8594 ) ;
8695 const res = mockResponse ( ) ;
8796 await login ( req , res ) ;
88- expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
89- expect ( res . json ) . toHaveBeenCalledWith ( {
90- message : "No user with matching username" ,
91- } ) ;
97+ assert . deepEqual ( res . status . mock . calls [ 0 ] . arguments , [ 401 ] ) ;
98+ assert . deepEqual ( res . json . mock . calls [ 0 ] . arguments , [
99+ {
100+ message : "No user with matching username" ,
101+ } ,
102+ ] ) ;
92103} ) ;
93104test ( "should 401 with message if passed password does not match stored password" , async ( ) => {
94105 const req = mockRequest (
@@ -100,10 +111,12 @@ test("should 401 with message if passed password does not match stored password"
100111 ) ;
101112 const res = mockResponse ( ) ;
102113 await login ( req , res ) ;
103- expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
104- expect ( res . json ) . toHaveBeenCalledWith ( {
105- message : "Wrong password" ,
106- } ) ;
114+ assert . deepEqual ( res . status . mock . calls [ 0 ] . arguments , [ 401 ] ) ;
115+ assert . deepEqual ( res . json . mock . calls [ 0 ] . arguments , [
116+ {
117+ message : "Wrong password" ,
118+ } ,
119+ ] ) ;
107120} ) ;
108121test ( "should 201 and set session.data with username if user exists and right password provided" , async ( ) => {
109122 const req = mockRequest (
@@ -115,9 +128,9 @@ test("should 201 and set session.data with username if user exists and right pas
115128 ) ;
116129 const res = mockResponse ( ) ;
117130 await login ( req , res ) ;
118- expect ( res . status ) . toHaveBeenCalledWith ( 201 ) ;
119- expect ( res . json ) . toHaveBeenCalled ( ) ;
120- expect ( req . session . data ) . toEqual ( {
131+ assert . deepEqual ( res . status . mock . calls [ 0 ] . arguments , [ 201 ] ) ;
132+ assert . deepEqual ( res . json . mock . calls [ 0 ] . arguments , [ ] ) ;
133+ assert . deepEqual ( req . session . data , {
121134 username : "guest" ,
122135 } ) ;
123136} ) ;
0 commit comments