Invalid or unexpected token, testing bare react native app

Viewed 1036

I am practicing test driven development in bare react native and using package jest and enzyme. I installed NativeBase for UI components. and I am getting error when I am using icons from NativeBase

FAIL  src/screens/Welcome/Welcome.test.js
ā— Test suite failed to run

/Users/../node_modules/@expo/vector-icons/build/vendor/react-native-vector-icons/Fonts/AntDesign.ttf:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){
                                                                                         

SyntaxError: Invalid or unexpected token

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1258:14)
  at Object.<anonymous> (node_modules/@expo/vector-icons/src/AntDesign.ts:2:1)

I have following setting in my package.json file:

"jest": {
  "preset": "react-native",
  "testEnvironment": "jsdom",
  "setupFiles": [
    "./setup.js",
    "./node_modules/react-native-gesture-handler/jestSetup.js"
  ],
  "transformIgnorePatterns": [
    "/!node_modules\\/@expo"
  ]
},
1 Answers

I had the same error and fixed it by adding this line to my jest config.

"transform": {
  "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js"
}

And creating the file fileTransformer.js at the root of my project.

// fileTransformer.js
const path = require('path');

module.exports = {
  process(src, filename, config, options) {
    return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
  },
};

As described in the Jest documentation https://jestjs.io/docs/en/webpack#mocking-css-modules

Related