Sunday, 16 April 2017

node.js Mocha : how to reuse a variable defined in a local setup before() hook?

In my ('resource' test , I create a new User

/**
 * root level hooks
 */
before((done) => {
  const newUser = new User({
    username: 'johndoe',
    password: 'jdpwd',
    email: 'johndoe@example.com',
    mobileNumber: 123456789
  });
  newUser.save((err) => {
    // console.log('error: ', err);
    done();
  });
});

I would like to reuse the user id in my test :

describe('## Resource APIs', () => {
  let resource = {
    name: 'RS123',
    description: 'RS123Description',
    owner: newUser.id,   <= should be the id of the newUser
    private: true
  };
  describe('# POST /api/v1/resources', () => {
    it('should create a new resource', (done) => {
      request(app)
        .post('/api/v1/resources')
        .send(resource)
        .expect(httpStatus.OK)
        .then((res) => {
          expect(res.body.name).to.equal(resource.name);
          expect(res.body.description).to.equal(resource.description);
          resource = res.body;
          done();
        })
        .catch(done);
    });
  });
   ...
});

newUser is undefined.. how can I change it ? thanks for feedback



via erwin

No comments:

Post a Comment