vue-template-babel-compiler icon indicating copy to clipboard operation
vue-template-babel-compiler copied to clipboard

[Resolved Question] vue-jest usage

Open monkemedia opened this issue 4 years ago • 6 comments

Optional chaining works greats in my Nuxt application, however, when I run my Jest unit tests, I get the following error message:

Jest encountered an unexpected token

In my jest.config.js file I have the following in my transform property

 transform: {
    '^.+\\.js$': 'babel-jest',
    '.*\\.(vue)$': 'vue-jest'
  },

Can I assume I will need to update the transform property?

monkemedia avatar Sep 06 '21 14:09 monkemedia

You may need to add compiler: require('vue-template-babel-compiler') to your jest.config.js

According to vue-jest Doc, the step may be:

  1. Find or create your jest.config.js If you are using .json config, you need to replace .json config with jest.config.js, so that we can use programmatic configuration to require('vue-template-babel-compiler')

  2. Add require('vue-template-babel-compiler') as globals.['vue-jest'].templateCompiler.compiler value:

// jest.config.js
module.exports = {
  globals: {
    'vue-jest': {
      templateCompiler: {
         compiler: require('vue-template-babel-compiler')
      }
    }
  }
}
  1. Run your test

I haven't tested.

If you run into any problem, just let me know.

I would glad to help.

JuniorTour avatar Sep 07 '21 05:09 JuniorTour

Sadly this didn't work for me. Still getting the same error

monkemedia avatar Sep 07 '21 07:09 monkemedia

@monkemedia It will work for vue-jest >= 4.0.0 && jest <= 26.6.3

I just test by feat: add jest test example from vue-template-babel-compiler-nuxt-project

075183d94e8fe7227db5157eda7714b

Config

Nuxt.js && vue-jest DEMO project

1. Package Version:

  • Only work for vue-jest >= 4.0.0. (vue-jest<= 3.9.9 need upgrade, can't use customized compiler.)
// package.json
  "devDependencies": {
    "@vue/test-utils": "^1.2.2",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^26.6.3",
    "jest": "^26.6.3",
    "vue-jest": "4.0.1",
    "vue-template-babel-compiler": "^1.0.3"
  }

2. jest.config.js

module.exports = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
    '^~/(.*)$': '<rootDir>/$1',
    '^vue$': 'vue/dist/vue.common.js',
  },
  moduleFileExtensions: ['js', 'vue', 'json'],
  transform: {
    '^.+\\.js$': 'babel-jest',
    '.*\\.(vue)$': 'vue-jest',
  },
  globals: {
    'vue-jest': {
      templateCompiler: {
        compiler: require('vue-template-babel-compiler')
      }
    }
  }
}

3. test.spec.js

import { mount } from '@vue/test-utils'
import Tutorial from '@/components/Tutorial.vue'

describe('jest test with vue-template-babel-compiler', () => {
  test('should work for Optional Chaining', () => {
    const wrapper = mount(Tutorial)
    expect(wrapper.vm).toBeTruthy()
    expect(wrapper.vm.$el.innerHTML).toMatch('Optional Chaining enabled: true')
  })
})

Thank you for your feedback!

JuniorTour avatar Sep 07 '21 17:09 JuniorTour

You are a star. My vue-jest was below version 4. Thank you for you help :)

monkemedia avatar Sep 08 '21 07:09 monkemedia

I found that using functional components in jest will cause error as without the config transformAssetUrls: true, filename will not pass to compiler See here

Here is the full config

    'vue-jest': {
      templateCompiler: {
        compiler: require('vue-template-babel-compiler'),
        transformAssetUrls: true,
      },
    },

vip30 avatar Sep 14 '21 14:09 vip30

the above is not working for me on nuxt

ashley00101010 avatar Jun 29 '22 13:06 ashley00101010