Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest setupTestFrameworkScriptFile or setupFiles ES6 syntax

I am new to jest. I encounter the error on circle CI. My setup.js file has ES6 syntax. Since the file on setupTestFrameworkScriptFile will be executed each time every test is run, I thought I could use ES6 syntax but babel-jest won't transpile it. Regular tests such as unit tests for react components are transpiled. On local environment, the setup file is tranpiled.

 SyntaxError: Unexpected token {

      at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-cli/node_modules/jest-runtime/build/script_transformer.js:305:17)
      at Object.<anonymous> (test/setup.js:1:869)
      at next (native)

My jest config file looks like this.

{
  "verbose": true,
  "transform": {
    "^.+\\.(js|jsx)$": "babel-jest"
  },
  "transformIgnorePatterns": [
     "node_modules/(?!my-module)"
  ],
  "snapshotSerializers": [
    "jest-serializer-enzyme"
  ],
  "setupTestFrameworkScriptFile": "<rootDir>/test/setup.js",
  "globals": {
    "__PROD__": false,
    "__STG__": false
  },
  "moduleDirectories": [
    "node_modules"
  ],
  "testRegex": "(/__tests__/.*|__test__.js)$",
  "moduleFileExtensions": [
    "css",
    "jsx",
    "js",
    "scss",
    "svg"
  ],
  "moduleNameMapper": {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/test/fileMock.js",
    "^.+\\.(css|less|scss)$": "<rootDir>/test/styleMock.js"
  }
}

Do I miss any settings?

like image 408
undefined Avatar asked Oct 21 '17 00:10

undefined


1 Answers

Jest doesn't push the setup.js script through the transpilation step. Jest will transpile your test code, then run your setup script without transpilation, then run your test code. Just write it in ES5 syntax and it will work.

like image 117
Eye Avatar answered Nov 02 '22 06:11

Eye