mercredi 4 septembre 2019

How do I fix Jest test throwing 'return' outside of function?

Being relatively new to Jest - and having just used it to good success in a pristine environment/project with no problems, yesterday I decided to give it a shot in an older node.js environment (10.15.x).

Initially worked fine. Then I started pulling in older requires dependencies and it immediately started failing:

 FAIL  modules/reviews/review.jest.js
  ● Test suite failed to run

    SyntaxError: /Users/darrin/src/tot/commons/index.js: 'return' outside of function (2:0)

      1 | 'use strict';
    > 2 | return (module.exports = {
        | ^
      3 |       accessControl: require('./modules/accessControl'),
      4 |       about: require('./modules/about'),
      5 |       api: require('./modules/api'),

      at Parser.raise (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:6325:17)
      at Parser.parseReturnStatement (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:10190:12)
      at Parser.parseStatementContent (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:9877:21)
      at Parser.parseStatement (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:9829:17)
      at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:10405:25)
      at Parser.parseBlockBody (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:10392:10)
      at Parser.parseTopLevel (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:9758:10)
      at Parser.parse (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:11270:17)
      at parse (node_modules/@babel/core/node_modules/@babel/parser/lib/index.js:11306:38)
      at parser (node_modules/@babel/core/lib/transformation/normalize-file.js:170:34)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.111s

It was news to me that returns at global level (in these dependencies) aren't strictly okay in Javascript but it makes sense. Still... I cannot change these libraries so how do I get around this error so I can use Jest?

Aucun commentaire:

Enregistrer un commentaire