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

[May be a bug.] I want to test with Jest in Vue.js. #2473

Closed
sasasa opened this issue Nov 5, 2019 · 2 comments
Closed

[May be a bug.] I want to test with Jest in Vue.js. #2473

sasasa opened this issue Nov 5, 2019 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@sasasa
Copy link

sasasa commented Nov 5, 2019

  import { shallowMount, createLocalVue } from '@vue/test-utils'
  import { FormTodo } from '@/components'
  import { ValidationProvider, ValidationObserver, localize, extend } from 'vee-validate';
  import { required, min } from 'vee-validate/dist/rules'
  import ja from 'vee-validate/dist/locale/ja.json'

  extend('required', {
    ...required,
  })
  extend('min', {
    ...min,
  })
  localize('ja', ja)

  const localVue = createLocalVue()
  localVue.component('ValidationProvider', ValidationProvider)
  localVue.component('ValidationObserver', ValidationObserver)

  describe('Todo', () => {
    describe('Initialize', () => {
      let props
      beforeEach(() => {
        props = {
          submitWord: 'test submit',
        }
      })

      it('mount', () => {
        const wrapper = shallowMount(FormTodo, {
          propsData: props,
          localVue,
        })
        expect(wrapper.isVueInstance()).toBe(true)
      })
    })
  })

log

● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

/app/todolist/node_modules/vee-validate/dist/rules.js:690
export { alpha$1 as alpha, alpha_dash, alpha_num, alpha_spaces, between, confirmed, digits, dimensions, email, excluded, ext, image, integer, is, is_not, length, max, max_value, mimes, min, min_value, numeric, oneOf, regex, required, required_if, size };
^^^^^^

SyntaxError: Unexpected token export

  2 | import { FormTodo } from '@/components'
  3 | import { ValidationProvider, ValidationObserver, localize, extend } from 'vee-validate';
> 4 | import { required, min } from 'vee-validate/dist/rules'
    | ^
  5 | import ja from 'vee-validate/dist/locale/ja.json'
  6 |
  7 | extend('required', {

  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
  at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
  at Object.<anonymous> (tests/unit/components/FormTodo.spec.js:4:1)
@logaretm
Copy link
Owner

logaretm commented Nov 5, 2019

This is a duplicate of #2310

The reason is that Jest excludes transpilation for node_modules, the rules use ES6 exports to achieve tree-shaking. You can find a solution here:

#2310 (comment)

@logaretm logaretm closed this as completed Nov 5, 2019
@logaretm logaretm added the duplicate This issue or pull request already exists label Nov 5, 2019
@sasasa
Copy link
Author

sasasa commented Nov 6, 2019

thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants