jestjs/jest

`basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")`

nolde opened this issue ยท 22 comments

nolde commented

When importing vue-test-utils, I get a weird error that doesn't seem to be related to anything else other than the component itself. I am running the test with jest.

Test code:

import { shallow, createLocalVue } from 'vue-test-utils'

describe('App', () => {
  it('should work', () => {
    expect(true).toBe(true)
  })
})

If the import line is commented out, the test runs fine and passes; with the import in, I get the following error:

 FAIL  src\core\App.test.js
  โ— Test suite failed to run

    C:\Program Files\nodejs\vue:2
    basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
              ^^^^^^^
    SyntaxError: missing ) after argument list

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
      at Object.<anonymous> (node_modules/vue-test-utils/dist/vue-test-utils.js:5:27)
      at Object.<anonymous> (src/core/App.test.js:2:21)

That done, convinced that the problem was with vue-test-utils, I tried to create tests without using the component:

import Vue from 'vue'
import App from './App.vue'

describe('App', () => {
  it('renders default layout if none is defined in', () => {
    const Ctor = Vue.extend(App)
    const instance = new Ctor().$mount()
    expect(instance.layout).toBe('default')
  })
})

... aaaannnd I get the same error again. Therefore, the issue seems to be somewhere within the jest ecosystem.

I have removed the node_modules folder and reinstalled it all using npm install to make sure it was not a dependency issue with each test.

Jest config:

// package.json
  "jest": {
    "moduleFileExtensions": [
      "js",
      "vue"
    ],
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1"
    },
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
      ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue"
    },
    "mapCoverage": true,
    "verbose": true
  }

Versions:

  • Windows 10
  • npm v3.10.10
  • node v6.10.3
  • vue v2.5.2
  • babel-jest v21.2.0
  • jest v21.2.1
  • jest-vue v0.8.1
  • vue-test-utils with v1.0.0.beta.3, v1.0.0.beta.2, v1.0.0.beta.1

This seems like a problem with vue-test-utils, not a Jest issue.

nolde commented

It is not, as I mention on the original request.

Even without any reference to vue-test-utils, I get the exact same error.

It could be related to jest-vue, but the fact that just requesting vue-test-utils on the first set of tests triggered the error, I suppose it is not there.

require('vue') is pulling in a shell script not JavaScript. Does this happen in the node repl? If not, I'm inclined to calling this a jest bug

nolde commented

Just had a friend set up similar code in OSX; it works on his machine, fails in my Windows device.
Therefore, it is Windows environment related.

Can you delete C:\Program Files\nodejs\vue? And install vue locally in your project instead (and maybe do npm install --global vue)

nolde commented

It is installed locally, and there is no vue installed globally, only local to the project.

The vue that is being picked up seems to be the entry point for vue-cli command vue, which is pretty weird.

EDIT:
I have uninstalled vue-cli from global scope, and then the test passes. For some reason, the jest runtime is picking up that bash file instead of the actual vue component.

Thanks for the tip, @SimenB , I didn't realise it was picking the bash file up.

If you install the cli globally, what does node -p "require.resolve('vue')" return? If that command returns something else than C:\Program Files\nodejs\vue, you found a bug in Jest

nolde commented

Running it inside a project that has vue installed, I get C:\git\<project-folder>\node_modules\vue\dist\vue.runtime.common.js

If I run it outside of a project folder, I get the vue-cli bash file, same one being required by jest.

Yeah, that's a bug in the module resolution of Jest, then.

That said, I can't reproduce on mac. node -p "require.resolve('vue')" outside of a project throws Error: Cannot find module 'vue', even if vue-cli is installed globally.

nolde commented

Yeah, it seems to be related to Windows. My co-worker using mac installed vue-cli as well and nothing came up.

@Nulimitz not related to jest at all

Just putting it out there that we need help from the community with this one.

A fix should probably go in here: https://github.com/facebook/jest/blob/5fc33197b9711d1ca86bcdf41f5cb98073dab484/packages/jest-resolve/src/default_resolver.js

FYI,

I faced this same problem when trying to debug jest inferno tests using windows machine.

I found this npm script causes error:
node --inspect-brk ./node_modules/.bin/jest --runInBand --no-cache --no-watchman
but this works as expected:
node --inspect-brk ./node_modules/jest/bin/jest --runInBand --no-cache --no-watchman

@Havunen I had this issue with Nuxt, and your comment was exactly what I needed. It's a shame I only found it after spending the better part of two days on this.

Is there a ./node_modules/.bin/jest.cmd? SHould be for windows

  1. Checkout https://github.com/vuejs/vue-test-utils-jest-example
  2. yarn
  3. yarn run test
  4. multiple basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") errors
  • @SimenB: There is jest.cmd in ./node_modules/.bin
  • vue-cli installed globally

For whomever comes after me, my issue was this was in trying to run this command on a windows machine:
node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage,

However, I could run it by running this command as suggested above
node --expose-gc ./node_modules/jest/bin/jest --runInBand --logHeapUsage

I was using below configuration in package.json file

"scripts": {
 "test": "node --inspect node_modules/.bin/cross-env API_HOST=url ember serve",
}

The above config work for the Linux machine. But I have a windows machine and during npm run test I got this error.

In my case when I removed node --inspect than its work for me.

 "test": "node_modules/.bin/cross-env API_HOST=url ember serve",

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.