All Projects → datencia → ionic2-jest-example

datencia / ionic2-jest-example

Licence: MIT license
Example of how to test an Ionic2 app with Jest

Programming Languages

typescript
32286 projects
HTML
75241 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ionic2-jest-example

React Test Demo
React test demo with Jest and Enzyme
Stars: ✭ 134 (+211.63%)
Mutual labels:  jest, unit-test
Angular Builders
Angular build facade extensions (Jest and custom webpack configuration)
Stars: ✭ 843 (+1860.47%)
Mutual labels:  jest, unit-test
automock
A library for testing classes with auto mocking capabilities using jest-mock-extended
Stars: ✭ 26 (-39.53%)
Mutual labels:  jest, unit-test
Generator Jhipster Ionic
Ionic for JHipster 💥
Stars: ✭ 147 (+241.86%)
Mutual labels:  ionic, jest
ng-nest-cnode
Angular 10 Front-End and Nestjs 7 framework Back-End build Fullstack CNode
Stars: ✭ 17 (-60.47%)
Mutual labels:  jest
patent-free-react-ecosystem-migration-plan
Patent Free React Ecosystem Migration Plan
Stars: ✭ 15 (-65.12%)
Mutual labels:  jest
travis-ci-ionic-yml
An example configuration of Ionic/cordova + NPM + AngularJS + Android continuous integration setup on Travis CI
Stars: ✭ 21 (-51.16%)
Mutual labels:  ionic
zero
📦 A zero config scripts library
Stars: ✭ 17 (-60.47%)
Mutual labels:  jest
ionic-uuchat
基于ionic3,angular4的实时聊天app,兼容web端。该项目只是前端部分,所有数据需要请求后端服务器,需要配套express-uuchat-api使用。
Stars: ✭ 14 (-67.44%)
Mutual labels:  ionic
aws-nestjs-starter
Serverless, AWS, NestJS, GraphQL and DynamoDB starter
Stars: ✭ 200 (+365.12%)
Mutual labels:  jest
typescript-nuxtjs-boilerplate
🍱 Nuxt.js with TypeScript and Run with docker and docker-compose 🐶🦄🔥 visit: https://typescript-nuxtjs-boilerplate.netlify.com/example
Stars: ✭ 51 (+18.6%)
Mutual labels:  jest
pytest-spark
pytest plugin to run the tests with support of pyspark
Stars: ✭ 65 (+51.16%)
Mutual labels:  unit-test
nodejs-vuejs-mysql-boilerplate
Node.js (REST API) + Vue.js/Nuxt.js (Frontend/Backend) + MySQL Boilerplate
Stars: ✭ 134 (+211.63%)
Mutual labels:  jest
react-jest-enzyme-example
A minimal configuration for testing React components with Jest and Enzyme.
Stars: ✭ 23 (-46.51%)
Mutual labels:  jest
ts-detox-example
Example TypeScript + React-Native + Jest project that integrates Detox for writing end-to-end tests
Stars: ✭ 54 (+25.58%)
Mutual labels:  jest
graphql-workshop
🕸️ GraphQL Node.js Workshop: Create an API Gateway
Stars: ✭ 20 (-53.49%)
Mutual labels:  jest
Coursera-Clone
Coursera clone
Stars: ✭ 48 (+11.63%)
Mutual labels:  jest
ionic-3-video-calling-using-webrtc
This is demo code of how to implement video calling in ionic 3 using webrtc
Stars: ✭ 58 (+34.88%)
Mutual labels:  ionic
ionic-workflow-guide
Create a full and powerful worflow with Ionic (Unit Testing, Environment variables, Automatic documentation, Production App Server, Automatic deployment)
Stars: ✭ 46 (+6.98%)
Mutual labels:  unit-test
React-Redux-Enterprise
A React-Redux boilerplate for enterprise/large scaled web applications
Stars: ✭ 77 (+79.07%)
Mutual labels:  jest

Testing Ionic2 with Jest

tested with jest Build Status codecov

ionic love test

This is an example of how to test an Ionic 2 app using Jest.

yarn test

Ionic 2 is a great framework for building amazing mobile apps with Angular, but as you may know, it comes without support for unit tests. Yes, that's even if it seems incredible in the 21st century :-(

Of course, there are different approaches to have unit tests working with an Ionic 2 Project, but most of then require too much configuration and knowledge about the related tooling (jasmine, karma, ...).

Jest is a complete and easy to set-up JavaScript testing solution created by Facebook. Some of its benefits are:

  • Fast and sandboxed
  • Built-in code coverage reports
  • Zero configuration

This project tries to illustrate how to add support for unit tests to an Ionic 2 project with a minimal configuration. It's based on the awesome article Testing Angular faster with Jest by Michal Pierzchala.

Summary

  • Prerequisites
  • Steps to run the example
  • Steps to add Jest to your own Ionic 2 project

Prerequisites

You’ll need to install the latest version of the Ionic CLI and Cordova. Before you do that, you’ll need a recent version of Node.js. Download the installer for Node.js 6 or greater and then proceed to install the Ionic CLI and Cordova for native app development:

Verify that you are running at least node v6.x.x and npm 3.x.x by running node -v and npm -v in a terminal / console window. Older versions may produce errors.

Steps to run then example

  • Install the latest version of the Ionic CLI and Cordova.
$ npm install -g cordova ionic

You may need to add “sudo” in front of these commands to install the utilities globally

If you run ionic -v it should return 3.0.0 (or greater)

  • Clone this repo into a new project folder.
$ git clone https://github.com/datencia/ionic2-jest-example.git
$ cd ionic2-jest-example
  • Run npm test (or yarn test if you have yarn installed) to run the tests.

  • You can also run the command ionic serve to get a quick preview of the app in the browser.

Steps to add Jest to your own Ionic 2 project

  • Install Jest dependencies:
$ npm install jest jest-preset-angular @types/jest --save-dev
  • Add this to your npm scripts:
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "jest --runInBand",

Learn more about Jest CLI Options

  • Include this in your package.json:
{
  "jest": {
    "preset": "jest-preset-angular",
    "roots": [
      "src"
    ],
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts",
    "transformIgnorePatterns": [
      "node_modules/(?!@ngrx|@ionic-native|@ionic)"
    ]
  }
}
  • In the src folder create a setupJest.ts file with following contents:
import 'jest-preset-angular';
import './jestGlobalMocks'; // browser mocks globally available for every test
  • Then create the jestGlobalMocks.ts file with following contents
const mock = () => {
  let storage = {};
  return {
    getItem: key => key in storage ? storage[key] : null,
    setItem: (key, value) => storage[key] = value || '',
    removeItem: key => delete storage[key],
    clear: () => storage = {},
  };
};

Object.defineProperty(window, 'localStorage', {value: mock()});
Object.defineProperty(window, 'sessionStorage', {value: mock()});
Object.defineProperty(window, 'getComputedStyle', {
  value: () => ['-webkit-appearance']
});
  • In the src folder create a tsconfig.spec.json file with following contents:
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es5",
    "allowJs": true
  },
  "include": [
    "**/*.spec.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].