Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected reserved word error using Jest

I'm trying to add Jest tests to my project (using React, Browserify and Babel), but I'm getting an error while doing the most basic thing:

With this structure:

|- /app
  |- /scripts    
    |- /models
      |- Vendor.js
      |- /__tests__
         |- Vendor-test.js

And this code:

Vendor.js:

class Vendor {
  constructor(json) {
   this.id = json.vendor_id;
  }
}

module.exports = Vendor;

Vendor-test:

jest.dontMock('../Vendor.js');

describe('Vendor', function() {
 it('Vendor creation', function() {
     var Vendor = require('../Vendor');
     var vendor = new Vendor({vendor_id:1});
     expect(vendor.id).toBe(1);
   });
 });

And this is the error I'm getting:

app/scripts/models/Vendor.js: Unexpected reserved word
        at Contextify.sandbox.run (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
        at JSDomEnvironment.runSourceText (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/JSDomEnvironment.js:108:22)
        at Object.runContentWithLocalBindings (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/lib/utils.js:345:23)
        at Loader._execModule (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:250:9)
        at Loader.requireModule (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:916:12)
        at Loader.requireModuleOrMock (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:937:17)
        at Spec.<anonymous> (/Users/jasalguero/work/projects/monoqi/b2b-frontend/app/scripts/models/__tests__/Vendor-test.js:5:18)
        at jasmine.Block.execute (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
        at jasmine.Queue.next_ (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
        at null._onTimeout (/Users/jasalguero/work/projects/monoqi/b2b-frontend/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

Actually the error happens as soon as I require a module. Any ideas?

like image 215
jasalguero Avatar asked Sep 14 '25 01:09

jasalguero


1 Answers

I kept getting this error even after installing babel-jest.

The solution was to create a .babelrc file in the root directory. Inside it you define the presets you need, for example like this:

{
    "presets": ["es2015", "react"]
}

Also, there is a node_modules/jest-cli/.haste-cache directory which might lead to too much caching. Consider disabling it with the preprocessCachingDisabled option.

like image 92
Matt Zeunert Avatar answered Sep 17 '25 19:09

Matt Zeunert