Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest Unit Test SyntaxError: Unexpected token import #5395

Closed
JeremyWalters opened this issue Oct 23, 2018 · 9 comments
Closed

Jest Unit Test SyntaxError: Unexpected token import #5395

JeremyWalters opened this issue Oct 23, 2018 · 9 comments
Labels
invalid The issue is missing information or is not a valid bug/feature request

Comments

@JeremyWalters
Copy link

Versions and Environment

Vuetify: 1.3.0
Vue: 2.5.17
OS: Windows 10

Previously worked in:

Vuetify: 1.2.10
Vue: 2.5.17

Steps to reproduce

Vue Create (typescript/Babel/Jest)
Vue Add vuetify (default)
Create jest test that involves any vuetify component

Expected Behavior

All my jest test should run

Actual Behavior

Get an error before test start to run:

location_to_project\node_modules\vuetify\lib\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Vuetify from './components/Vuetify';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

      1 | import Vue from 'vue'
      2 | // @ts-ignore
    > 3 | import * as Vuetify from 'vuetify/lib'
        | ^
      4 | import 'vuetify/src/stylus/app.styl'
      5 |
      6 | Vue.use(Vuetify, {

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:4    7)
03:17)
      at Object.<anonymous> (src/plugins/vuetify.ts:3:1)

Additional Comments:

It is probable a babel transform issue with jest.

@KaelWD
Copy link
Member

KaelWD commented Oct 23, 2018

This is a problem with your test setup, it has nothing to do with vuetify.

@KaelWD KaelWD closed this as completed Oct 23, 2018
@KaelWD KaelWD added the invalid The issue is missing information or is not a valid bug/feature request label Oct 23, 2018
@JeremyWalters
Copy link
Author

JeremyWalters commented Oct 23, 2018

@KaelWD Well it worked before 1.3.0

@johnleider
Copy link
Member

I'm not sure how it could have worked, vuetify/lib wasn't a thing until v1.3

@JeremyWalters
Copy link
Author

@johnleider I know. Before that I used

import {
	Vuetify,
	VApp,
	VAlert,
	VAutocomplete,
	VBtnToggle,
	VCheckbox,
        ....
} from "vuetify";

Which in any case it still works fine like that. It just doesn't work with
import Vuetify from 'vuetify/lib'

@KaelWD
Copy link
Member

KaelWD commented Oct 24, 2018

You need to provide a reproduction environment then, because it looks to me like babel isn't configured correctly.

@JeremyWalters
Copy link
Author

@KaelWD Thanks I will do that

@JeremyWalters
Copy link
Author

JeremyWalters commented Oct 24, 2018

@KaelWD

Vue CLI v3.0.5
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, TS, CSS Pre-processors, Linter, Unit
? Use class-style component syntax? Yes
? Use Babel alongside TypeScript for auto-detected polyfills? Yes
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Stylus
? Pick a linter / formatter config: Prettier
? Pick additional lint features: Lint on save
? Pick a unit testing solution: Jest
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In dedicated config files

vue add vuetify(Default Preset)

https://github.com/JeremyWalters/vue-jest-test-not-working Just run npm run test:unit

There is another trivial issue on this branch https://github.com/JeremyWalters/vue-jest-test-not-working/tree/types-error

Thank you in making time for this, I really appreciate all vuetifies work .

@KaelWD
Copy link
Member

KaelWD commented Oct 26, 2018

You have several problems in that repo:

  • You should not be importing the vuetify plugin in a component, it is already done in main.ts
  • Vuetify distributes types, add it it the types array in tsconfig instead of using // @ts-ignore
  • vue-cli's default jest config ignores js if you've selected typescript, change the regex to [jt]sx and enable allowJs in tsconfig
  • vuetify/lib doesn't contain any components, and vuetify-loader only works with webpack. Either import 'vuetify' in your test setup, or import plugins/vuetify and use moduleNameMapper and transformIgnorePatterns so the styles are ignored properly

See vuetifyjs/vue-cli-plugins#9 and vuetifyjs/vue-cli-plugins#43, we don't yet modify anything for unit tests or typescript.

JeremyWalters/vue-jest-test-not-working#1

@JeremyWalters
Copy link
Author

@KaelWD Thank you so much for all your effort, I really appreciate it a lot. Thanks so much for the changes you made.

Thanks for everything you do for the community.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid The issue is missing information or is not a valid bug/feature request
Projects
None yet
Development

No branches or pull requests

3 participants