Skip to content

Instantly share code, notes, and snippets.

@mutewinter
Created October 4, 2016 21:28
Show Gist options
  • Save mutewinter/4024a569c70d7dda87a6b22a194d0d11 to your computer and use it in GitHub Desktop.
Save mutewinter/4024a569c70d7dda87a6b22a194d0d11 to your computer and use it in GitHub Desktop.
Use Jest with CoffeeScript
{
"jest": {
"moduleFileExtensions": [
"js",
"json",
"jsx",
"node",
"coffee"
],
"preprocessorIgnorePatterns": [ ],
"scriptPreprocessor": "<rootDir>/test/preprocessor.js"
}
}
const coffee = require('coffee-script');
const babelJest = require('babel-jest');
module.exports = {
process: (src, path) => {
// CoffeeScript files can be .coffee, .litcoffee, or .coffee.md
if (coffee.helpers.isCoffee(path)) {
return coffee.compile(src, { bare: true });
}
if (!/node_modules/.test(path)) {
return babelJest.process(src, path);
}
return src;
}
};
@MadcapJake
Copy link

MadcapJake commented Feb 1, 2018

This and only this:

// package.json
"jest": {
    "preset": "jest-preset-coffeescript"
}

Way easier than fiddling with a transform module

@surjikal
Copy link

surjikal commented Sep 12, 2018

@MadcapJake This doesn't seem to work too with packages in node_modules that are written in coffeescript.

@dima4p
Copy link

dima4p commented Jan 21, 2022

In order to have the line numbers to be correct use this code:

const coffee = require('coffeescript');
const babelJest = require('babel-jest');

module.exports = {
  process: (src, filename, options) => {
    if (coffee.helpers.isCoffee(filename)) {
      let js = coffee.compile(src, {
        bare: true,
        filename: filename,
        inlineMap: true,
      });
      return babelJest.default.process(js, filename, options);
    }
    return src;
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment