Import Unexpected identifier + SyntaxError: Unexpected string

Jal*_*lil 10 javascript vue.js jestjs babeljs

I'm having a problem when running jest test. I'm getting an import unexpected identifier

I have already tried by cleaning npm cache and installing npm babel jest, polyfill, preset-es2015. I've also read this trying some different configs here and there.

This is my babel.config.js

module.exports = {
  presets: [
    '@vue/app'
  ],
  env: {
    test: {
      plugins: [
        "transform-strict-mode",
        "transform-es2015-modules-commonjs",
        "transform-es2015-spread",
        "transform-es2015-destructuring",
        "transform-es2015-parameters"
      ]
    }
  }
}

Run Code Online (Sandbox Code Playgroud)

and my jest config in package.json

"jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "vue"
    ],
    "transform": {
      "^.+\\.js$": "babel-jest",
      ".*\\.(vue)$": "vue-jest"
    },
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1"
    }
  }
Run Code Online (Sandbox Code Playgroud)

Here is the error.

Test suite failed to run

    PCROUTE\Test.test.js:3
    import _interopRequireDefault from "PCROUTE\\node_modules\\@babel\\runtime-corejs2/helpers/esm/interopRequireDefault";
           ^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)

I'm expecting to pass the test but I'm getting that error message. I think the problem comes with the env part in the babel.config.js

Edit: I'm also using windows 7, and I this can be causing those back and forward mixed slashes.

Edit2: I just tested it in Linux and it doesn't work. I have all in forward slashes

 PCROUTE/src/tests/Test.test.js:3
    import _interopRequireDefault from "PCROUTE/node_modules/@babel/runtime-corejs2/helpers/esm/interopRequireDefault";
           ^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript 
Run Code Online (Sandbox Code Playgroud)

Edit 3: I saw a lot of people fixed it with [these lines](https://github.com/vuejs/vue-cli/issues/1879#issuecomment-409872897).

transformIgnorePatterns: [
    "node_modules/(?!(babel-jest|jest-vue-preprocessor)/)"
  ]
Run Code Online (Sandbox Code Playgroud)

in the jest config file. I added them and ran this ./node_modules/jest/bin/jest.js --clearCache but it doesn't work.

Edit 4: I just added a couple of things to my jest file. And deleted a couple in my babel file, and now I'm getting. Unexpected String

NEW JEST FILE

module.exports = {
    moduleFileExtensions: [
      'js',
      'jsx',
      'json',
      'vue'
    ],
    transform: {
      '^.+\\.vue$': 'vue-jest',
      '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
      '^.+\\.jsx?$': 'babel-jest'
    },
    transformIgnorePatterns: [
      '/node_modules/'
    ],
    moduleNameMapper: {
      '^@/(.*)$': '<rootDir>/src/$1'
    },
    snapshotSerializers: [
      'jest-serializer-vue'
    ],
    testMatch: [
      '**/tests/**/*.test.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
    ],
    testURL: 'http://localhost/',
    watchPlugins: [
      'jest-watch-typeahead/filename',
      'jest-watch-typeahead/testname'
    ]
  }
Run Code Online (Sandbox Code Playgroud)

And I deleted everything from my babel file except the presets: @vue/app part.

Here is the new error I'm getting.

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.find";
                                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected string
Run Code Online (Sandbox Code Playgroud)

Jal*_*lil 3

经过几天的努力。我最终回答了我的问题。在这里我留下了所有的配置文件。

笑话配置.js

process.env.VUE_CLI_BABEL_TARGET_NODE = true;
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true;

module.exports = {
    moduleFileExtensions: [
      'js',
      'jsx',
      'json',
      'vue'
    ],
    transform: {
      '^.+\\.vue$': '<rootDir>/node_modules/vue-jest',
      '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
      '^.+\\.jsx?$': 'babel-jest'
    },
    transformIgnorePatterns: [
      '/node_modules/'
    ],
    moduleNameMapper: {
        '^@/(.*)$': '<rootDir>/src/$1'
    },
    snapshotSerializers: [
      'jest-serializer-vue'
    ],
    testMatch: [
      '**/tests/**/*.test.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
    ],
    testURL: 'http://localhost/',
    watchPlugins: [
      'jest-watch-typeahead/filename',
      'jest-watch-typeahead/testname'
    ]
  }
Run Code Online (Sandbox Code Playgroud)

babel.config.js

module.exports = {
  presets: [
    '@vue/app'
  ]
}

Run Code Online (Sandbox Code Playgroud)

笔记

  • 我需要添加babel core 桥才能将 @babel/core 与 vue-jest 一起使用
  • 建议更改文件后清除 jest 的缓存