在jest设置中Momentjs导入错误

时间:2018-06-15 14:34:18

标签: reactjs testing momentjs jest

我正在导入momentjs 导入时刻从"时刻/ src /时刻"在我的申请中。

我想为react应用程序设置我的jest配置。 这是我的.babelrc.js文件

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)/,
}

以下是我的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
}

当我运行npm t时,终端抛出以下错误

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)

非常感谢任何帮助

0 个答案:

没有答案