vendredi 15 juin 2018

Momentjs import error in jest setup

I am importing momentjs as import moment from "moment/src/moment" in my application.

I want to setup my jest configuration for the react app. Here is my .babelrc.js file

const isTest = String(process.env.NODE_ENV) === "test"
console.log("Test environment", isTest)
module.exports = {
    presets: [["env", {modules: isTest ? "commonjs" : false}],"react", "es2015", "es2016"],
    plugins: [
        "syntax-dynamic-import",
        "transform-class-properties",
        "transform-object-rest-spread",
        "transform-object-assign",
        "transform-class-properties",
        isTest ? "dynamic-import-node": null
    ].filter(Boolean),
    ignore: /(node_modules)/,
}

And following is my jest.config.js

module.exports = {
  testEnvironment: "jsdom",
  verbose: true,
//   moduleNameMapper: {
//     "\\.css$": require.resolve("./test/style-mock"),
//   },
  // roots: ["./src"],
  moduleFileExtensions: ["js", "jsx"],
  moduleDirectories: ["./node_modules", "./src"],
  moduleNameMapper: {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
      require.resolve("./__mocks__/fileMock.js"),
    "\\.(css|less)$": require.resolve("./__mocks__/styleMock.js"),
  },
  transform: {
    "^.+\\.js?$": "babel-jest",
    // "node_modules/moment/src/moment.js": "",
  },
  transformIgnorePatterns: [
    "node_modules/(?!(react-native|redux-persist)/)",
  ],
  // transformIgnorePatterns: ["./node_modules/moment"],
  setupFiles: ["./testSetup.js"],
  //collectCoverageFrom
  //coverageThreshold
}

when I run npm t, Terminal throws following error

Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://facebook.github.io/jest/docs/en/configuration.html

    Details:

    /home/gmadmin/Practice/tryWebClientApp/web-client-app/node_modules/moment/src/moment.js:7
    import { hooks as moment, setHookCallback } from './lib/utils/hooks';
    ^^^^^^

    SyntaxError: Unexpected token import



      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:402:17)

Any help is really appreciated

Aucun commentaire:

Enregistrer un commentaire