rjz / supertest-session

Persistent sessions for supertest
Other
88 stars 21 forks source link

Test stops after when use authenticatedSession #33

Open kazkondo opened 6 years ago

kazkondo commented 6 years ago
const session = require('supertest-session');
const myApp = require('../../app');

describe('Test sample', () => {

  let testSession;
  let authenticatedSession;

  beforeEach( () => {
    testSession = session(myApp);
  });

  it('/member/login', (done) => {
    testSession.post('/member/login')
      .send({ userid: 'test@example.com', password: 'test' })
      .expect(302)
      .end(function (err) {
        if (err) { return done(err); }
        authenticatedSession = testSession;
        return done();
      });
  });

  // if there is this test, test freezes after all test suite finished
  it('/member/profile', (done) => {
    authenticatedSession.get('/member/profile')
      .expect(200)
      .end(done); 
  });
});

Written by Typescript, but executed as javascript with jest

rjz commented 6 years ago

Hey @kazkondo, and thanks for reporting this!

It sounds like we may have another async problem, ala https://github.com/rjz/supertest-session/issues/28. Before we cook up a test case, could you confirm:

  1. your installed version of supertest and supertest-session, and also that
  2. the continuation in the last spec is being called as expected?
it('/member/profile', (done) => {
  authenticatedSession.get('/member/profile')
    .expect(200)
    .end((err) => {
      console.log('Yep, definitely broken.');
      done(err);
    }); 
});
nickfreemandesign commented 6 years ago

@rjz I have a similar condition. to answer your clarification, there is no Yep, definitely broken. logged, but the error thrown is TypeError: Cannot read property 'expect' of undefined.

authenticatedSession logs as the testSession as you would expect, in the beforeEach hook.

UDPATE: my variable testSession was moved to the global scope next to const myApp = require('/myAppPath') as let testSession = session(myApp) and now its 👌