Vuetify: Cannot import and use vuetify components for unit testing with jest

Created on 12 Aug 2019  路  8Comments  路  Source: vuetifyjs/vuetify

Environment

Vuetify Version: 1.5.16
Vue Version: 2.6.10
Browsers: Chrome 76.0.3809.100
OS: Mac OS 10.15.0

Steps to reproduce

yarn install
yarn test:unit

to serve

vue serve src/dev.vue

Expected Behavior

Working smoothly with single file component structure from vue

Actual Behavior

Syntax Error

Reproduction Link

https://github.com/sametsafak/sfc-jest

Other comments

I am trying to create a single file component and write unit tests with jest. Project works with vue's instant prototyping method.

Normally project and tests are working smoothly but not with vuetify.

When I try to import vuetify components, jest gives me SyntaxError: Unexpected identifier error on importing vuetify components line (src/components/FcTable.vue:41:1). I checked my environment to make sure if it's an es6 import issue but it's not. I can import and use another script files.

testing upstream

Most helpful comment

I have this in a project at work:

transform: { '^.*\\.js$': 'babel-jest' },
transformIgnorePatterns: ['node_modules/(?!vue-router|@babel|vuetify)']

May also need some babel config changes, idk.

All 8 comments

There is an ongoing issue with https://github.com/vuejs/vue-cli/issues/1584 . Not sure if something pops out at @KaelWD .

I have this in a project at work:

transform: { '^.*\\.js$': 'babel-jest' },
transformIgnorePatterns: ['node_modules/(?!vue-router|@babel|vuetify)']

May also need some babel config changes, idk.

Adding that transformIgnorePatterns property to my Jest config did the trick
Thank you, @KaelWD 馃檹

Update

馃毃 DISREGARD: THIS DOESN'T WORK LIKE I THOUGHT 馃毃

@KaelWD solution works for me. Here is another alternative that I found to be faster to run:

js// jest.config.js module.exports = { // ... other config ... moduleNameMapper: { // ... other mapped names ... 'vuetify/lib': '<rootDir>/node_modules/vuetify/es5', } }

The reason this works is because Vuetify ships with a pre-compiled ES5 version, so just mapping to that one prevents you from having to use Babel or any other sort of pre-processor.

@morphatic note that isn't 100% equivalent as vuetify/es5 doesn't export components, so anything other than the default import from vuetify/lib will fail.

Yeah, sorry. I've been banging my head against Jest and TS all day. I was sure I understood it at the time... 馃槗

It will work in most cases if you aren't manually importing anything else, just something to be aware of if you start getting problems. babel-jest should be just as fast after the first run as it caches the compilation result.

Adding that transformIgnorePatterns property to my Jest config did the trick

That worked for me too. Thank you @KaelWD ! 馃帀

My jest.config.js:

module.exports = {
  preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
  setupFilesAfterEnv: ['<rootDir>tests/setup.ts'],
  transformIgnorePatterns: ['node_modules/(?!vue-router|@babel|vuetify)']
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

paladin2005 picture paladin2005  路  3Comments

ricardovanlaarhoven picture ricardovanlaarhoven  路  3Comments

gluons picture gluons  路  3Comments

sebastianmacias picture sebastianmacias  路  3Comments

chriswa picture chriswa  路  3Comments