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

    [Solved] Configuring Jest with Quasar

    Help
    5
    7
    3627
    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.
    • Jaxon
      Jaxon last edited by Jaxon

      Hey, vue-test-utils got released and since then I’ve decided to integrate it into my project. I chose the path of Testing SFCs with Jest and followed the guide to integrate it.

      Testing SFC’s now works! So long as the component does not import anything from 'quasar'.

      Initially, I ran into the issue of:
      Cannot find module 'quasar' from 'Hello-Two.vue'

      As per research and asking around, was advised to add an alias to my Jest configuration, which looks like this:

      "jest": {
          "moduleFileExtensions": [
            "js",
            "json",
            "vue"
          ],
          "moduleNameMapper": {
            "^@/(.*)$": "<rootDir>/src/$1",
            "^quasar$": "<rootDir>/node_modules/quasar-framework"
          },
          "transform": {
            ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest",
            "^.+\\.js$": "<rootDir>/node_modules/babel-jest"
          },
          "mapCoverage": true,
          "snapshotSerializers": [
            "<rootDir>/node_modules/jest-serializer-vue"
          ]
        }
      

      Unfortunately, the issue has now mutated to:
      SyntaxError: Unexpected token export in the quasar.esm.js file
      0_1509989058523_Screen Shot 2017-11-06 at 17.24.04.png

      This has been reported multiple times by other users here, here and some mention of it; in the Quasar Gitter chat but unfortunately, there is no definitive answer on how to resolve it and hoping to compile it all here.
      Although this post potentially hints at a fix but am unclear how to proceed. As importing from quasar-framework leads to the same outcome.

      I’ve created a git repo to recreate the problem using the Quasar PWA template.
      Clone it if you wish and run the quasar test command to begin the testing process.

      • Hello.vue -> Hello.spec.js passes as it do not import any quasar components.

      • Hello-Two.vue -> Hello-Two.spec.js fails, due to SyntaxError: Unexpected token export as mentioned above. This component also import’s a quasar component.

      Researching this issue in general, it seems to lay with the way babel transpiles such that Jest can understand Quasar. As a result, it is now out of my depth and was hoping someone could provide some insight into how to resolve this?

      Thanks!

      1 Reply Last reply Reply Quote 2
      • Jaxon
        Jaxon last edited by Jaxon

        Thank you @leon who provided the answer. The following configuration allows the integration of Jest with Quasar.
        Checkout the original answer in this post!

        "jest": {
            "moduleFileExtensions": [
              "js",
              "vue"
            ],
            "collectCoverageFrom": [
              "**/*.{vue}"
            ],
            "transformIgnorePatterns": [
              "node_modules/core-js",
              "node_modules/babel-runtime",
              "node_modules/lodash",
              "node_modules/vue"
            ],
            "moduleNameMapper": {
              "quasar": "quasar-framework/dist/quasar.esm.js",
              "^vue$": "vue/dist/vue.common.js"
            },
            "coverageDirectory": "<rootDir>/src/components/coverage",
            "transform": {
              "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
              ".*\\.(vue)$": "<rootDir>/node_modules/jest-vue"
            },
            "mapCoverage": true
          }
        
        1 Reply Last reply Reply Quote 0
        • T
          trajano last edited by

          @Jaxon do you have your .babelrc handy?

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

            Instead of the moduleNameMapper in package.json I added this to .babelrc (as recommended here in the comments by the man behind vue-test-utils):

            "env": {
                "test": {
                  "presets": [
                    "env",
                    "stage-2"
                  ],
                  "plugins": [
                    [
                      "module-resolver",
                      {
                        "root": [
                          "./src"
                        ],
                        "alias": {
                          "^@/(.*)$": "<rootDir>/src/components/$1",
                          "quasar": "quasar-framework/dist/quasar.esm.js",
                          "^vue$": "vue/dist/vue.common.js",
                          "src": "<rootDir>/src",
                          "assets": "<rootDir>/src/assets",
                          "components": "<rootDir>/src/components"
                        }
                      }
                    ]
                  ]
                }
              }
            

            You do need to install babel-plugin-module-resolver  for this.

            1 Reply Last reply Reply Quote 0
            • P
              paul last edited by

              Any suggestions for V17 when using vue-cli with quasar as plugin?

              1 Reply Last reply Reply Quote 0
              • s.molinari
                s.molinari last edited by

                @paul - have a look at this: https://github.com/nothingismagick/quasar-starter-ssr-pwa-jest-cypress

                Scott

                1 Reply Last reply Reply Quote 2
                • P
                  paul last edited by

                  @s-molinari thanks a lot Scott. Very much appreciated.

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