No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    [0.15.1] Components registering within Jest tests

    Help
    10
    23
    7451
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • O
      odyssianHound last edited by

      @Akaryatrh did you ever find a solution to this? I am running into the same issue with quasar 15.3. I’ve tried importing the quasar components like in previous versions, but my build is having issues with the quasar moduleNameMapper you’ve listed.

      1 Reply Last reply Reply Quote 0
      • O
        odyssianHound last edited by

        Anyone find a solution to this yet? I’ve tried importing the quasar components manually, but no luck

        1 Reply Last reply Reply Quote 0
        • V
          vinstah last edited by vinstah

          I’ve finally cracked it after learning from different configurations in testing 0.15.x with jest

          npm install babel-jest jest-vue vue-template-compiler -D

          /src/tests/example.test.js

          import Quasar, * as All from 'quasar'
          import { shallow, createLocalVue } from '@vue/test-utils'
          import { IsEmpty } from 'ipayroll-js/src/utils'
          import Component from '@/pages/login.vue'
          
          const localVue = createLocalVue()
          localVue.use(Quasar, {components: All, directives: All, plugins: All})
          
          const wrapper = shallow(Component, {
            localVue,
            mocks: {
              $isEmpty: IsEmpty
            }
          })
          
          it('Component Page test:', () => {
            expect(wrapper.isVueInstance()).toBe(true)
          })
          

          package.json

          ...
          "jest": {
              "testMatch": [
                "<rootDir>/src/**/?(*.)(spec).js?(x)"
              ],
              "testPathIgnorePatterns": [
                "<rootDir>/src/e2e/"
              ],
              "moduleNameMapper": {
                "^@/(.*)$": "<rootDir>/src/$1"
              },
              "moduleFileExtensions": [
                "js",
                "vue"
              ],
              "collectCoverageFrom": [
                "**/*.{vue}"
              ],
              "transformIgnorePatterns": [
                "node_modules/core-js",
                "node_modules/babel-runtime",
                "node_modules/lodash",
                "node_modules/vue"
              ],
              "coverageDirectory": "<rootDir>/src/components/coverage",
              "transform": {
                "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
                ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue"
              },
              "mapCoverage": true
            }
          ...
          

          .babelrc

          {
            "presets": [
              [ "env", {"modules": false} ],
              "stage-2"
            ],
            "env": {
              "test": {
                "presets": [
                  ["env", { "targets": { "node": "current" }}]
                ],
                "plugins": [
                  [
                    "module-resolver",
                    {
                      "root": [
                        "./src"
                      ],
                      "alias": {
                        "quasar": "quasar-framework/dist/quasar.mat.esm.js",
                        "^vue$": "vue/dist/vue.common.js"
                      }
                    }
                  ]
                ]
              }
            },
            "plugins": ["transform-runtime"],
            "comments": false
          }
          
          
          1 Reply Last reply Reply Quote 0
          • Akaryatrh
            Akaryatrh last edited by

            You rock @vinstah I’ll have a deeper look on what you did 🙂

            1 Reply Last reply Reply Quote 0
            • marek.kaczkowski
              marek.kaczkowski last edited by

              @vinstah thank you! You saved my life 😉

              Is there a guide how to inject layout when my template root element is a q-page?
              At the moment test yelds QPage needs to be child of QPageContainer, when I remove root q-page (so q-table is root template element) test works.

              V 1 Reply Last reply Reply Quote 0
              • V
                vinstah @marek.kaczkowski last edited by

                @onrel I did have the same issue but when using shallow() from @vue/test-utils, I stopped getting that error as it wasn’t actually rendering any child components.

                I am testing that my methods work with Jest and use TestCafe for UI functional testing

                V 1 Reply Last reply Reply Quote 1
                • M
                  MusicForMellons last edited by

                  @vinstah Do jest snapshots work with your settings? I think vue-server-renderer needs to be installed for instance?

                  1 Reply Last reply Reply Quote 0
                  • V
                    vinstah last edited by

                    Hi @MusicForMellons,
                    You are correct vue-server-renderer is required for snapshots, it is also needed if you use render/renderToString from @vue/server-tests-utils

                    1 Reply Last reply Reply Quote 0
                    • V
                      vinstah @vinstah last edited by vinstah

                      on a side note I can turn off those QPage needs to be child of QPageContainer messages by using jest --silent

                      1 Reply Last reply Reply Quote 0
                      • M
                        MusicForMellons last edited by

                        @vinstah Ah, thanks. I get these error messages (and components are ‘registered’ I think…):
                        console.error node_modules/vue/dist/vue.common.js:593
                        [Vue warn]: Unknown custom element: <q-page-sticky> - did you register the component correctly? For recursive components, make sure to provide the “name” option.

                          (found in <Root>)
                        console.error node_modules/vue/dist/vue.common.js:593
                          [Vue warn]: Unknown custom element: <q-btn> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
                          
                          (found in <Root>)
                        console.error node_modules/vue/dist/vue.common.js:593
                          [Vue warn]: Unknown custom element: <q-icon> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
                        Jaxon 1 Reply Last reply Reply Quote 0
                        • Jaxon
                          Jaxon @MusicForMellons last edited by

                          @musicformellons do you happen to have a solution for this?

                          1 Reply Last reply Reply Quote 0
                          • M
                            MusicForMellons last edited by

                            @Jaxon No, sorry. I got this answer recently, but it does not answer your issue: https://stackoverflow.com/questions/49616642/jest-unit-testing-config-with-quasar-framework-0-15

                            I gave up on Jest for now and work happily with cypress (use it for e2e, and some unit-testing when necessary…).

                            1 Reply Last reply Reply Quote 0
                            • Akaryatrh
                              Akaryatrh last edited by Akaryatrh

                              @MusicForMellons @Jaxon Do you guys import your components the way @vinstah does? On my side It’s working properly:

                              import Quasar, * as All from 'quasar'
                              import { shallow, createLocalVue } from '@vue/test-utils'
                              const localVue = createLocalVue()
                              
                              localVue.use(Quasar, {components: All, directives: All, plugins: All})
                              
                              Jaxon 1 Reply Last reply Reply Quote 0
                              • Jaxon
                                Jaxon @Akaryatrh last edited by

                                @akaryatrh Yeah, I switched it over using @vinstah’s method and it works!

                                Akaryatrh 1 Reply Last reply Reply Quote 1
                                • Akaryatrh
                                  Akaryatrh @Jaxon last edited by

                                  @akaryatrh Yeah, I switched it over using @vinstah’s method and it works!

                                  👍

                                  1 Reply Last reply Reply Quote 0
                                  • A
                                    avorias5 last edited by

                                    Using the same settings as @Akaryatrh

                                    import Quasar, * as All from 'quasar'
                                    import { shallow, createLocalVue } from '@vue/test-utils'
                                    import Component from '/my/Component/
                                    
                                    const localVue = createLocalVue()
                                    localVue.use(Quasar, {components: All, directives: All, plugins: All})
                                    
                                    const wrapper = shallow(Component, {
                                      localVue
                                    })
                                    

                                    The problem is that the wrapper.html() now returns undefined. So wrapper.find() and wrapper.findAll() always fail.

                                    I’ve play around with the settings and found that if I use

                                    localVue.use(Quasar, { directives: All, plugins: All})
                                    

                                    Without using components, the wrapper.html() would act normally.
                                    If use components: All or components: ["A", "B" .. ], the wrapper.html() will become undefined.
                                    However, we have to use Quasar components, otherwise Jest will throw the “component not registered correctly” error.

                                    Does anyone facing the same problem? Can you guys successfully use wrapper.html(), wrapper.find() and wrapper.findAll() with this settings?
                                    @vinstah @Jaxon @Akaryatrh

                                    1 Reply Last reply Reply Quote 0
                                    • nothingismagick
                                      nothingismagick last edited by

                                      anybody having problems resolving quasar in jest, use this:

                                          "moduleNameMapper": {
                                            "quasar": "quasar-framework/dist/umd/quasar.mat.umd.min.js"
                                            ...
                                      
                                      haigha-earwicket 1 Reply Last reply Reply Quote 1
                                      • nothingismagick
                                        nothingismagick last edited by

                                        This conversation is no longer applicable to testing in 1.0.

                                        If you are reading this, please use $ quasar ext add @quasar/testing and install the prerigged jest harness that way.

                                        1 Reply Last reply Reply Quote -1
                                        • haigha-earwicket
                                          haigha-earwicket @nothingismagick last edited by

                                          For anyone else coming across this in a 1.0 world, the mapping has changed to (made it work for me, anyway):

                                          "moduleNameMapper": {
                                            "quasar": "quasar/dist/quasar.umd.min.js"
                                            ...
                                          1 Reply Last reply Reply Quote 0
                                          • M
                                            manubi last edited by

                                            I am still getting the same error. Installing quasar ext add @quasar/testing throws an error “ext unknown”

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post