// auth.test.jsdescribe('POST /api/user/signup',()=>{test('It should return protected page if token is correct',asyncdone=>{// Store our cookiesletCookies;// Create a new userawaitagent.post('/api/user/signup').send({email:'hello@world.com',password:'123456'}).expect(201).then(res=>{expect(res.body.user).toBeTruthy();// Save the cookie to use it later to retrieve the sessionCookies=res.headers['set-cookie'].pop().split(';')[0];});constreq=agent.get('/');req.cookies=Cookies;req.end(function(err,res){if(err){returndone(err);}expect(res.text).toBe('Protected page');expect(res.status).toBe(200);done();});});});
You can create a user and generate a token in each test or just populate it through beforeAll:
Hi, thank you :)
Its a good question! Please, check my updated Github repo where I added a simple authentication using JWT and some tests.
My solution is based on this issue
You can create a user and generate a token in each test or just populate it through
beforeAll
: