Coder Social home page Coder Social logo

beeman / jest-preset-angular Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thymikee/jest-preset-angular

0.0 2.0 0.0 95 KB

A preset of Jest configuration for Angular projects.

License: BSD 3-Clause "New" or "Revised" License

TypeScript 69.03% JavaScript 27.30% HTML 2.98% CSS 0.70%

jest-preset-angular's Introduction

jest-preset-angular

CircleCI Build Status

A preset of Jest configuration for Angular projects.

This is a part of the article: Testing Angular faster with Jest

Exposed configuration

{
  "globals": {
    "__TS_CONFIG__": "src/tsconfig.json",
    "__TRANSFORM_HTML__": true
  },
  "transform": {
    "^.+\\.(ts|js|html)$": "<rootDir>/node_modules/jest-preset-angular/preprocessor.js"
  },
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|js)$",
  "moduleFileExtensions": [
    "ts",
    "js",
    "html"
  ],
  "moduleNameMapper": {
    "app/(.*)": "<rootDir>/src/app/$1",
    "environments/(.*)": "<rootDir>/src/environments/$1"
  },
  "transformIgnorePatterns": [
    "node_modules/(?!@ngrx)"
  ]
}

Brief explanation of config

  • <rootDir> is a special syntax for root of your project (here by default it's project's root /)
  • we're using some "globals" to pass information about where our tsconfig.json file is that we'd like to be able to transform HTML files through ts-jest
  • "transform" – run every TS, JS, or HTML file through so called preprocessor (we'll get there); this lets Jest understand non-JS syntax
  • "testRegex" – we want to run Jest on files that matches this regex
  • "moduleFileExtensions" – our modules are TypeScript and JavaScript files
  • "moduleNameMapper" – if you're using absolute imports here's how to tell Jest where to look for them; uses regex
  • "setupTestFrameworkScriptFile" – this is the heart of our config, in this file we'll setup and patch environment within tests are running
  • "transformIgnorePatterns" – unfortunately some modules (like @ngrx ) are released as TypeScript files, not pure JavaScript; in such cases we cannot ignore them (all node_modules are ignored by default), so they can be transformed through TS compiler like any other module in our project.

Preprocessor

Jest doesn't run in browser nor through dev server. It uses jsdom to abstract browser environment. So we have to cheat a little and inline our templates and get rid of styles (we're not testing CSS) because otherwise Angular will try to make XHR call for our templates and fail miserably.

I used a scrappy regex to accomplish this with minimum effort, but you can also write a babel plugin to make it bulletproof. And btw, don't bother about perf here – Jest heavily caches transforms. That's why you need to run Jest with --no-cache flag every time you change it.

Angular testing environment setup

If you look at your src/test.ts (or similar bootstrapping test file) file you'll see similarities to setupJest.js. What we're doing here is we're adding globals required by Angular. With jest-zone-patch we also make sure Jest test methods run in Zone context. Then we initialize the Angular testing environment like normal.

jest-preset-angular's People

Contributors

beeman avatar thymikee avatar

Watchers

 avatar  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.