lundi 4 avril 2016

Undefined Es6 class in unit test with gulp mocha

I'm having some troubles with the following Reference Error while running my unit tests where i can not figure what i did wrong.

First, i've defined the following class :

/**
 * Invitation class
 */
export default class Invitation {

  /**
   * Create a Invitation
   * @param {string} user The user id
   * @param {string} creationDate The creation date of the invitation
   * @param {string} validationDate The validation date of the invitation
   * @param {string} denyDate The deny date of the invitation
   * @param {string} project The project id
   * @param {string} userTargetEmail The email address where the invitation was send
   * @param {string} _id The invitation _id
   */
  constructor(user, project, userTargetEmail, creationDate, validationDate, denyDate, _id) {
    /** @type {string} */
    this.user = user;
    /** @type {string} */
    this.creationDate = creationDate;
    /** @type {string} */
    this.validationDate = validationDate;
    /** @type {string} */
    this.denyDate = denyDate;
    /** @type {string} */
    this._id = _id;
    /** @type {string} */
    this.project = project;
    /** @type {string} */
    this.userTargetEmail = userTargetEmail;
  }
}

and then the following unit test file (problem occurs in the before each hook) :

   import Mongoose from 'mongoose';

    import InvitationController from '../../../../../app/business/invitations/controller/InvitationController';
    import InvitationService from '../../../../../app/business/invitations/service/InvitationService';
    import ErrorList from '../../../../../app/helper/ErrorList';
    import NotificationService from '../../../../../app/helper/service/NotificationService';
    import MailService from '../../../../../app/helper/service/mail/op/MailService';
    import RegexService from '../../../../../app/helper/service/RegexService';
    import UserService from '../../../../../app/business/user/service/UserService';
    import User from '../../../../../app/business/user/model/User';
    import ProjectService from '../../../../../app/business/projects/service/ProjectService';
    import Project from '../../../../../app/business/projects/model/Project';
    import UtilService from '../../../../../app/helper/service/UtilService';
    import Invitation from '../../../../../app/business/invitations/model/Invitation';

    let chai = require('chai');
    let sinon = require('sinon');
    let expect = chai.expect;

    /** @test {InvitationController} */
    describe('InvitationController ', () => {
      let controller;
      let mailService;
      let notificationService;
      let invitationService;
      let regexService;
      let userService;
      let projectService;
      let utilService;

      let mockMailService;
      let mockNotifService;
      let mockInvService;
      let mockProjectService;
      let mockUserService;

      let project;
      let userId;
      let projectId;
      let invitationId;
      let user;

      beforeEach(() => {
        invitationId = Mongoose.Types.ObjectId('4edd40c86762e0fb12000007');
        projectId = Mongoose.Types.ObjectId('4edd40c86762e0fb12000005');
        userId = Mongoose.Types.ObjectId('4edd40c86762e0fb12000003');

        mailService = new MailService();
        notificationService = new NotificationService();
        invitationService = new InvitationService();
        regexService = new RegexService();
        userService = new UserService();
        projectService = new ProjectService();
        utilService = new UtilService();

        mockMailService = sinon.mock(mailService);
        mockNotifService = sinon.mock(notificationService);
        mockInvService = sinon.mock(invitationService);
        mockProjectService = sinon.mock(projectService);
        mockUserService = sinon.mock(userService);

        console.log('Loggin Invitation ', Invitation);

        let invitation = new Invitation(userId, projectId, 'mail@mail.fr', 'creationdate', null, null, invitationId);

        console.log('logging invit ', invitation);
        project = new Project('name', 'creationDate', 'lastModificationDate', 'user', projectId);
        user = new User('mail', 'password', 'firstname', 'lastname', '1');
        controller = new InvitationController(invitationService, projectService, regexService, mailService, notificationService, userService, utilService);
      });
/// all tests

And there things goes bad, when i'm runing my tests, i get :

  InvitationController
    addInvitationAction
Loggin Invitation function Invitation(user,project,userTargetEmail,creationDate,validationDate,denyDate,_id){__cov_Thbl4xwtVLP9GaZCsEH5nQ.f['2']++;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['6']++;_classCallCheck(this,Invitation);__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['7']++;this.user=user;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['8']++;this.creationDate=creationDate;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['9']++;this.validationDate=validationDate;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['10']++;this.denyDate=denyDate;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['11']++;this._id=_id;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['12']++;this.project=project;__cov_Thbl4xwtVLP9GaZCsEH5nQ.s['13']++;this.userTargetEmail=userTargetEmail;}
      1) "before each" hook for "Expect to return an error when projectService.getProjectFromId fail"
...
/// ALL OTHER TESTS LOGS
...
     1) InvitationController  "before each" hook for "Expect to return an error when projectService.getProjectFromId fail":
     ReferenceError: Invitation is not defined
      at new Invitation (C:\Users\gayard\Documents\Dev\MYPROJECT\src\app\business\invitations\model\Invitation.js:9:684)
      at Context.<anonymous> (C:/Users/gayard/Documents/Dev/MYPROJECT/src/test/unit/business/invitations/controller/TestInvitationController-spec.js:64:22)
      at callFn (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runnable.js:315:21)
      at Hook.Runnable.run (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runnable.js:308:7)
      at next (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runner.js:298:10)
      at Immediate._onImmediate (C:\Users\gayard\Documents\Dev\MYPROJECT\node_modules\gulp-mocha\node_modules\mocha\lib\runner.js:320:5)

The import paths are all valid (i've double checked them, and used an atom plugin to make sure they were correct).

At first i suspected a wrong import in the controller, but it wasn't, in fact, it doesn't even reach there, since it doesn't even log the 'logging invit' + invitation.

I have multiple other class imports in the app that i did the exact same way without any troubles, and i'm not able to figure what's happening. I've even tried to rename/rewrite it, but no chances.

Any idea on why Invitation goes undefined even when i'm able to log it before calling for a new Invitation() ? Thanks in advance !

Aucun commentaire:

Enregistrer un commentaire