Testing ionic 2 apps

Thank you. I needed to update ‘rxjs’ and ‘zone.js’ too.

Thanks, @lathonez! I’ve based all my testing code on your clicker repo code and it all works wonderfully - you have probably saved me tons of hours in getting testing + typescript + angular2 + ionic2 + coverage + … integrated.

I recently ran into a problem as soon as we had to add the line ‘import es6-shim’ to app.ts, as was recommended in this forum post (and as was needed otherwise I got errors, after starting to use the new ionic2 build process).

This causes PhantomJS to break with a 404 error complaining it cannot find ‘/base/es6-shim.js’.

The following fix worked (a fix to the clicker testing code, which is what I use, almost verbatum and which is in this repository.

Just change the file karma.config.js in two places:

  1. Add this line to the files: section:


  2. Add this line to the proxies: section:

    ‘/base/es6-shim.js’: ‘/base/node_modules/es6-shim/es6-shim.js’

1 Like

Cool I’m really glad you found it helpful.

I’ve done my best to explain how to test external modules on the blog post, see ‘Testing External Modules’, there are also a few good worked examples in the FAQ.

FWIW I think having to wrestle with this karma config each time you add a new module is the worst part of the set up. Recently I’ve found a good alternative, which is bundling the spec (and all external dependencies) together, in the same way as Ionic does in app.bundle.js.

I’ve got a proof of concept for this on a branch and am looking to merge it back and update the blog post this week.

I would like to see a version without TypeScript. Is there something you are willing to provide? I can help you out with this one, because there is no good resource about this.
Thanks for sharing!

If you think a version without Typescript would be useful I’d encourage you to fork and document. I’ll be happy to provide advice if I am able but am not interested in contributing.

Personally I’m happy to stick with Typescript and would rather further it’s userbase.

The site https://www.joshmorony.com/how-to-unit-test-an-ionic-2-application/ was extremely helpful for me. It references Lathonez’s work. I remember spending weeks on this back in Ionic alpha days. Now it takes just minutes!

1 Like

Glad you found Josh’s post helpful, it’s definitely more accessible than what I’ve written!

Hi everyone. Maybe some one can help me with test service, that include third party library (pouchDB)?

I’m use lathonez clicker app as base.

Typings for pouchDB installed in node_modules. App works correct, but when I try to test service with pouchDB I receive an error:

TypeError: pouchdb_1.default is not a constructor

And console.log show in terminal, that pouchDB is undefined. I think that test missing typings for pouchDB, but can’t figure out how to include it correctly.

Service example:

'use strict';

import PouchDB from 'pouchdb';
import quickSearch from 'pouchdb-quick-search';
import PouchDbFind from 'pouchdb-find';


import { Injectable } from '@angular/core';
import { CONSTANTS } from '../../constants';

export class PouchDbService {
    private animalsDb: any = new PouchDB(CONSTANTS.dataBaseName); // this line throw error
    private searchFields: string[] = ['DE_Name', 'EN_Name'];

    public search(query: string): Promise<any> {
        return this.animalsDb.search({
            query: query,
            include_docs: true,
            mm: '50%',
            fields: this.searchFields
1 Like

Did you find a solution?

1 ) I switched on ionic2-boilerplate (Sorry, lathonez, your repo is great too, but I need a fast solution at that time).
2) Then I faced webpack issue with lie library (process is not defined)
3) I change script in package.json

"post-install": "gulp rm_broken_lie; webdriver-manager update"
  1. Add gulp and del to project with npm and gulp task to remove all broken lie library
var gulp = require('gulp'),
    del = require('del');

gulp.task('rm_broken_lie', done => {
        .then(() => del('node_modules/pouchdb-quick-search/node_modules/lie'))
        .then(() => del('node_modules/pouchdb-mapreduce-no-ddocs/node_modules/lie'))
        .then(() => del('node_modules/pouchdb-utils/node_modules/lie'))
        .then(() => del('node_modules/pouchdb-promise/node_modules/lie'))
        .then(() => del('node_modules/pouchdb-upsert/node_modules/lie'))
        .then(() => done());
  1. Fork lie, fix it and use in package.json
"lie": "https://github.com/EugeneSnihovsky/lie.git",
  1. In src/declarations.d.ts add
declare module 'pouchdb-quick-search' {
    const plugin: PouchDB.Plugin;
    export = plugin;
declare module 'pouchdb-find' {
    const plugin: PouchDB.Plugin;
    export = plugin;
  1. and in service
import * as PouchDB from 'pouchdb';
import * as quickSearch from 'pouchdb-quick-search';
import * as PouchDbFind from 'pouchdb-find';

  1. In test
import * as PouchDB from 'pouchdb';

// other logic and imports

beforeEach(() => {
        spyOn(PouchDB, 'plugin').and.callFake((a) => {});

Hey you should probably have a look at:

and read this really helpful blog post: http://roblouie.com/article/376/ionic-2-set-up-unit-testing-the-best-way/