Giter Club home page Giter Club logo

jest-vue's Introduction

vue-jest

Jest transformer for Vue Single File Components.

Installation

Since we need to support a variety of Vue and Jest versions, vue-jest doesn't follow semantic versioning.

Vue version Jest Version Package
Vue 2 Jest 26 and below vue-jest@4
Vue 3 Jest 26 and below vue-jest@5
Vue 2 Jest 27 @vue/vue2-jest@27
Vue 3 Jest 27 @vue/vue3-jest@27
npm install --save-dev @vue/vue2-jest@27 # (use the appropriate version)
yarn add @vue/vue2-jest@27 --dev

Setup

To use vue-jest as a transformer for your .vue files, map them to the appropriate vue-jest module:

{
  "jest": {
    "transform": {
      "^.+\\.vue$": "@vue/vue2-jest" // Update to match your installed version
    }
  }
}

A full config will look like this.

{
  "jest": {
    "moduleFileExtensions": ["js", "json", "vue"],
    "transform": {
      "^.+\\.js$": "babel-jest",
      "^.+\\.vue$": "@vue/vue2-jest"
    }
  }
}

Usage with Babel 7

If you use jest > 24.0.0 and babel-jest make sure to install babel-core@bridge

npm install --save-dev babel-core@bridge
yarn add babel-core@bridge --dev

Supported languages for SFC sections

vue-jest compiles <script />, <template />, and <style /> blocks with supported lang attributes into JavaScript that Jest can run.

Supported script languages

  • typescript (lang="ts", lang="typescript")
  • coffeescript (lang="coffee", lang="coffeescript")

Global Jest options

You can change the behavior of vue-jest by using jest.globals.

Supporting custom blocks

A great feature of the Vue SFC compiler is that it can support custom blocks. You might want to use those blocks in your tests. To render out custom blocks for testing purposes, you'll need to write a transformer. Once you have your transformer, you'll add an entry to vue-jest's transform map. This is how vue-i18n's <i18n> custom blocks are supported in unit tests.

A package.json Example

{
  "jest": {
    "moduleFileExtensions": ["js", "json", "vue"],
    "transform": {
      "^.+\\.js$": "babel-jest",
      "^.+\\.vue$": "@vue/vue2-jest"
    },
    "globals": {
      "@vue/vue2-jest": {
        "transform": {
          "your-custom-block": "./custom-block-processor.js"
        }
      }
    }
  }
}

Tip: Need programmatic configuration? Use the --config option in Jest CLI, and export a .js file

A jest.config.js Example - If you're using a dedicated configuration file like you can reference and require your processor in the config file instead of using a file reference.

module.exports = {
  globals: {
    '@vue/vue2-jest': {
      transform: {
        'your-custom-block': require('./custom-block-processor')
      }
    }
  }
}

Writing a processor

Processors must return an object with a "process" method, like so...

module.exports = {
  /**
   * Process the content inside of a custom block and prepare it for execution in a testing environment
   * @param {SFCCustomBlock[]} blocks All of the blocks matching your type, returned from `@vue/component-compiler-utils`
   * @param {string} vueOptionsNamespace The internal namespace for a component's Vue Options in vue-jest
   * @param {string} filename The SFC file being processed
   * @param {Object} config The full Jest config
   * @returns {string} The code to be output after processing all of the blocks matched by this type
   */
  process({ blocks, vueOptionsNamespace, filename, config }) {}
}

templateCompiler

You can provide TemplateCompileOptions in templateCompiler section like this:

{
  "jest": {
    "globals": {
      "@vue/vue2-jest": {
        "templateCompiler": {
          "transpileOptions": {
            "transforms": {
              "dangerousTaggedTemplateString": true
            }
          }
        }
      }
    }
  }
}

Supported template languages

  • pug (lang="pug")

    • To give options for the Pug compiler, enter them into the Jest configuration. The options will be passed to pug.compile().
    {
      "jest": {
        "globals": {
          "@vue/vue2-jest": {
            "pug": {
              "basedir": "mybasedir"
            }
          }
        }
      }
    }
  • jade (lang="jade")

  • haml (lang="haml")

Supported style languages

  • stylus (lang="stylus", lang="styl")

  • sass (lang="sass"), and

  • scss (lang="scss")

    • The Sass compiler supports Jest's moduleNameMapper which is the suggested way of dealing with Webpack aliases. Webpack's sass-loader uses a special syntax for indicating non-relative imports, so you'll likely need to copy this syntax into your moduleNameMapper entries if you make use of it. For aliases of bare imports (imports that require node module resolution), the aliased value must also be prepended with this ~ or vue-jest's custom resolver won't recognize it.

      {
        "jest": {
          "moduleNameMapper": {
            "^~foo/(.*)": "<rootDir>/foo/$1",
            // @import '~foo'; -> @import 'path/to/project/foo';
            "^~bar/(.*)": "~baz/lib/$1"
            // @import '~bar/qux'; -> @import 'path/to/project/node_modules/baz/lib/qux';
            // Notice how the tilde (~) was needed on the bare import to baz.
          }
        }
      }
    • To import globally included files (ie. variables, mixins, etc.), include them in the Jest configuration at jest.globals['vue-jest'].resources.scss:

      {
        "jest": {
          "globals": {
            "vue-jest": {
              "resources": {
                "scss": [
                  "./node_modules/package/_mixins.scss",
                  "./src/assets/css/globals.scss"
                ]
              }
            }
          }
        }
      }

CSS options

experimentalCSSCompile: Boolean Default true. Turn off CSS compilation

hideStyleWarn: Boolean Default false. Hide warnings about CSS compilation

resources:

{
  "jest": {
    "globals": {
      "vue-jest": {
        "hideStyleWarn": true,
        "experimentalCSSCompile": true
      }
    }
  }
}

jest-vue's People

Contributors

agualis avatar catrope avatar cexbrayat avatar danielbastos11 avatar dcherman avatar dependabot[bot] avatar eddyerburgh avatar jeremyzahner avatar jessicasachs avatar jesusgn90 avatar jolting avatar justinhelmer avatar lmiller1990 avatar nogic1008 avatar rchaser53 avatar sandst1 avatar sasanfarrokh avatar seriouslag avatar sibiraj-s avatar sodatea avatar steplov avatar stijn98s avatar thecycoone avatar toilal avatar tran2 avatar vvanpo avatar wataruoguchi avatar yohodopo avatar yutamakotaro avatar yyx990803 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.