Manu
Manu

Reputation: 10944

How to use Karma and Jest together in unit testing React application?

I am testing a React application already developed. The application has some unit test cases written in Karma but I am going to use Jest to unit test the application as it is the new framework, developed by facebook for testing React. The problem I am facing is because of file .babelrc. For Jest to run fine the content of .babelrc file should be as follows:

// .babelrc
{
  "presets": ["es2015", "react"]
}

But my existing application uses Karma and has the babelrc file content as below:

{
  "presets": [
   "es2015",
    "react",
    "stage-0"
  ]
, "plugins": [
    "transform-object-rest-spread"
  , "transform-decorators-legacy"
  , "transform-es2015-modules-amd"
  ]
}

The current babelrc file is not allowing Jest to run properly and causes error. But the thing is I cant modify its content as required by Jest since it will hamper my Karma test cases to stop running. Is there a way I can use the both at same time until i rewrite existing test cases in Jest?

Upvotes: 1

Views: 1587

Answers (1)

Manu
Manu

Reputation: 10944

I was able to run both Karma and Jest together so thought of sharing. I configured the babelrc file as follows:

{
  "env": {
    "test": {
      "presets": [
        "es2015",
        "react"
      ]
    },
    "development": {
      "presets": [
        "es2015",
        "react"
      ],
      "plugins": [
        ............
      ]
    },
    "production": {
      "presets": [
        "es2015",
        "react"
      ],
      "plugins": [
        ...............
      ]
    }
  }
}

Upvotes: 1

Related Questions