dejakob
dejakob

Reputation: 2092

Jest + ES2015 import

I am making an exercise with ES2015, Jest, React and I get this error:

TypeError: Property description must be an object: undefined
at defineProperties (native)
at Object.eval (<PROJECT>/node_modules/event-emitter/index.js:127:8)

After digging into it, I think it is related to the import of the nodeModule EventEmitter or by extending the class by it.

This is the code of the script file:

import EventEmitter from 'event-emitter';
import AppDispatcher from '../dispatcher/app-dispatcher';

import {
    ACTION_CURSOR_POSITION_CHANGED,
    ACTION_IS_DRAGGING_CHANGED
} from '../constants/actions';

let _draggingStoreInstance = null;

/**
 * DraggingStore class
 */
export default class DraggingStore extends EventEmitter
{
    /**
     * Constructor
     */
    constructor () {
    // ...

The source code of the test file looks like this:

import '../unmock/dragging-store.unmock.js';
import DraggingStore from '../../src/stores/dragging-store';

describe('Dragging Store', () => {
   let draggingStoreInstance = null;

   beforeEach(() => {
       draggingStoreInstance = DraggingStore.getInstance();
   });

   it('should be defined', () => {
       expect(DraggingStore).toBeDefined();
       expect(draggingStoreInstance).toBeDefined();
   });
});

I made an extra file for excluding mocks:

jest.dontMock('../../src/stores/dragging-store.js');
jest.dontMock('../../src/dispatcher/app-dispatcher.js');
jest.dontMock('../../src/constants/actions.js');

The code itself runs smoothly in the browser after compiling, but the test engine gives the error.

I added this in my package.json:

"scripts": {
    "test": "jest"
  },
 "jest": {
    "scriptPreprocessor": "./node_modules/babel-jest",
    "unmockedModulePathPatterns": [
      "./node_modules/react"
    ],
    "collectCoverage": true,
    "testDirectoryName": "spec",
    "moduleFileExtensions": [
      "js"
    ],
    "collectCoverageOnlyFrom": {
        // All files to test
    }
  }

Does anyone have a clue how to get around the problem? Thanks in advance...

Update: full source code can be found here: https://github.com/dejakob/unlease-chess

Upvotes: 4

Views: 1527

Answers (1)

JaKXz
JaKXz

Reputation: 1651

I realize this is very late, but, a lot has changed in the time you have posted this question. With Jest v19+, and assuming you are using the latest version of Babel as well, you can follow the instructions here:

http://facebook.github.io/jest/docs/webpack.html#using-with-webpack-2

since you are using modules, you will need to tell Babel to transpile them to commonjs requires so that they can be run in the node environment, which is how Jest works.

Upvotes: 2

Related Questions