serge1peshcoff serge1peshcoff - 2 months ago 35
Javascript Question

SyntaxError when running a Jest test containing import of a CoffeeScript file

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?

Answer

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.
Comments