0

I am writing a Jest integration test to test my server. Here is my test:

import { app, port } from '../server' // the other lines are not so important
...

When running jest this raises an error:

__tests__/graphql.test.js
  ● Test suite failed to run

    <project folder>/node_modules/papercut/lib/papercut.coffee:3
    {FileStore, S3Store, TestStore } = require('./store')
                                     ^
    SyntaxError: Unexpected token =

I am requiring server.js in my Jest test, this file requires upload.js and this file requires node module called papercut. The problem is, it's written in CoffeeScript, not pure JS.

At the beginning I had this error: Jest: cannot find module required inside module to be tested (relative path). I added coffee to my package.json's jest config like that:

"jest": {
  "moduleFileExtensions": ["js", "json", "jsx", "node", "coffee"]
},

but now I have the error I've described above.

How can I handle this?

Community
  • 1
  • 1
serge1peshcoff
  • 4,342
  • 11
  • 45
  • 76
  • Maybe take a look at the "Second attempt" part of this [question](http://stackoverflow.com/questions/27123072/jest-with-coffeescript-jsx) as it shows how to compile the coffee on import – caffeinated.tech Sep 22 '16 at 15:49

2 Answers2

1

You will need to setup a preprocessor for Jest as it can't nativity handle coffee script.

This has already been answered before so I won't go into the detail. The reason es6 works "out of the box" is because jest-babel has been provided as es6 is de facto in most React code bases.

Community
  • 1
  • 1
Tim Reynolds
  • 421
  • 2
  • 14
  • I tried to implement my own preprocessor, now I am getting this error: https://gist.github.com/serge1peshcoff/6823cba9f464ea7e3f920af2d895bb69. Seems like this package uses some ES6 syntax inside CoffeeScript or something like that. – serge1peshcoff Sep 23 '16 at 07:54
  • And also it's strange how it is not compiled by itself, the papercut's `index.js` is requiring `coffee-script` by itself, like that: `require('coffee-script'); module.exports = require('./lib/papercut');` – serge1peshcoff Sep 23 '16 at 08:33
0

I eventually fixed it by doing 2 things:

  • adding a preprocessor for coffee files (like in other question)
  • adding "preprocessorIgnorePatterns": [] to my Jest config. Jest ignores .coffee files inside node_modules/ if you won't tell it explicitly that you want to compile these files as well.
serge1peshcoff
  • 4,342
  • 11
  • 45
  • 76