vire / jest-vue-preprocessor

Preprocessor that allows importing of .vue files in jest tests
MIT License
130 stars 27 forks source link

Unexpected token import #47

Open emman27 opened 6 years ago

emman27 commented 6 years ago

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[x] support request

Current behavior

I've used jest-vue-preprocessor since the start of my project to work with files. Recently, I've added process.env (using Webpack's define plugin) to my project, and it gives me the following error.

 FAIL  test/unit/specs/components/onboarding/RecordForm.spec.js
  ● Test suite failed to run

    <path>/Form.vue:3
    import _defineProperty from 'babel-runtime/helpers/defineProperty';
    ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
      at Object.<anonymous> (test/unit/specs/components/Form.spec.js:6:19)
          at Generator.next (<anonymous>)

Expected behavior

Minimal reproduction of the problem with instructions Relevant sections of

// package.json

"jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "vue"
    ],
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1"
    },
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.(vue)$": "<rootDir>/node_modules/jest-vue-preprocessor"
    },
    "mapCoverage": true,
    "snapshotSerializers": [
      "<rootDir>/node_modules/jest-serializer-vue"
    ],
    "testRegex": "test/unit/.+\\.spec.js$",
    "collectCoverageFrom": [
      "<rootDir>/src/**/*"
    ]
  }
// .babelrc

{
  "presets": [
    [
      "env",
      {
        "modules": false,
        "targets": {
          "browsers": [
            "> 1%",
            "last 2 versions",
            "not ie <= 8"
          ]
        }
      }
    ],
    "stage-2"
  ],
  "plugins": [
    "transform-runtime"
  ],
  "env": {
    "test": {
      "plugins": [
        "istanbul",
        "transform-es2015-modules-commonjs"
      ]
    }
  }
}

What is the motivation / use case for changing the behavior? Er I just want it to work

Please tell us about your environment: Ubuntu 16.04, bash.

As a side note, I've tried replacing jest-vue-preprocessor with vue-jest, and the transpilation process goes fine, which is why I'm assuming the error lies here and not in something else. However vue-jest has problems with test coverage reporting which I can't deal with either.

signal-intrusion commented 6 years ago

@emman27 were you ever able to get this to work. I'm completely blocked.

emman27 commented 6 years ago

@signal-intrusion Unfortunately not. I've moved to vue-jest. If you're on a relatively new project. I'd suggest setting up with vue-cli also - they've recently added Jest as one of their default configs.

vire commented 6 years ago

Guy unfortunately I don't have now much capacity to investigate this bug, I'd assume it's related to .babelrc config

try to use the default .babelrc config in this repo

Fallenstedt commented 6 years ago

@vire Your suggestion worked perfectly for me. Thank you. I have a nuxt project and I am configuring jest to work with my Nuxt components.