Last active
March 31, 2018 22:42
-
-
Save leifwells/61763562e0e4d093d97b1fe585f91e67 to your computer and use it in GitHub Desktop.
Setting Up Testing in Ionic 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "./node_modules/@angular/cli/lib/config/schema.json", | |
"project": { | |
"name": "ng-test-project" | |
}, | |
"apps": [ | |
{ | |
"root": "src", | |
"outDir": "dist", | |
"assets": [ | |
"assets", | |
"favicon.ico" | |
], | |
"index": "index.html", | |
"main": "main.ts", | |
"polyfills": "polyfills.ts", | |
"test": "test.ts", | |
"tsconfig": "tsconfig.app.json", | |
"testTsconfig": "tsconfig.spec.json", | |
"prefix": "app", | |
"styles": [ | |
"styles.css" | |
], | |
"scripts": [], | |
"environmentSource": "environments/environment.ts", | |
"environments": { | |
"dev": "environments/environment.ts", | |
"prod": "environments/environment.prod.ts" | |
} | |
} | |
], | |
"e2e": { | |
"protractor": { | |
"config": "./protractor.conf.js" | |
} | |
}, | |
"lint": [ | |
{ | |
"project": "src/tsconfig.app.json", | |
"exclude": "**/node_modules/**" | |
}, | |
{ | |
"project": "src/tsconfig.spec.json", | |
"exclude": "**/node_modules/**" | |
}, | |
{ | |
"project": "e2e/tsconfig.e2e.json", | |
"exclude": "**/node_modules/**" | |
} | |
], | |
"test": { | |
"karma": { | |
"config": "./karma.conf.js" | |
} | |
}, | |
"defaults": { | |
"styleExt": "css", | |
"component": {} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Karma configuration file, see link for more information | |
// https://karma-runner.github.io/1.0/config/configuration-file.html | |
module.exports = function (config) { | |
config.set({ | |
basePath: '', | |
frameworks: ['jasmine', '@angular/cli'], | |
plugins: [ | |
require('karma-jasmine'), | |
require('karma-chrome-launcher'), | |
require('karma-jasmine-html-reporter'), | |
require('karma-coverage-istanbul-reporter'), | |
require('@angular/cli/plugins/karma') | |
], | |
client:{ | |
clearContext: false // leave Jasmine Spec Runner output visible in browser | |
}, | |
files: [ | |
{ pattern: './src/test.ts', watched: false } | |
], | |
preprocessors: { | |
'./src/test.ts': ['@angular/cli'] | |
}, | |
coverageIstanbulReporter: { | |
reports: [ 'html', 'lcovonly' ], | |
fixWebpackSourcePaths: true | |
}, | |
angularCli: { | |
environment: 'dev' | |
}, | |
reporters: ['progress', 'kjhtml'], | |
port: 9876, | |
colors: true, | |
logLevel: config.LOG_INFO, | |
autoWatch: true, | |
browsers: ['Chrome'], | |
singleRun: false | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file is required by karma.conf.js and loads recursively all the .spec and framework files | |
import 'zone.js/dist/zone-testing'; | |
import { getTestBed, TestBed } from '@angular/core/testing'; | |
import { | |
BrowserDynamicTestingModule, | |
platformBrowserDynamicTesting | |
} from '@angular/platform-browser-dynamic/testing'; | |
// IONIC IMPORTS | |
import { | |
App, | |
Config, | |
DeepLinker, | |
Form, | |
IonicModule, | |
Keyboard, | |
DomController, | |
MenuController, | |
NavController, | |
Platform, | |
} from 'ionic-angular'; | |
import { ConfigMock, PlatformMock } from 'ionic-mocks'; | |
declare const require: any; | |
// First, initialize the Angular testing environment. | |
getTestBed().initTestEnvironment( | |
BrowserDynamicTestingModule, | |
platformBrowserDynamicTesting() | |
); | |
// Then we find all the tests. | |
const context = require.context('./', true, /\.spec\.ts$/); | |
// And load the modules. | |
context.keys().map(context); | |
export class TestUtils { | |
public static beforeEachCompiler(components: Array<any>): Promise<{fixture: any, instance: any}> { | |
return TestUtils.configureIonicTestingModule(components) | |
.compileComponents().then(() => { | |
let fixture: any = TestBed.createComponent(components[0]); | |
return { | |
fixture: fixture, | |
instance: fixture.debugElement.componentInstance, | |
}; | |
}); | |
} | |
public static configureIonicTestingModule(components: Array<any>): typeof TestBed { | |
return TestBed.configureTestingModule({ | |
declarations: [ | |
...components | |
], | |
providers: [ | |
App, Form, Keyboard, DomController, MenuController, NavController, | |
{provide: Platform, useFactory: () => PlatformMock.instance()}, | |
{provide: Config, useFactory: () => ConfigMock.instance()}, | |
{provide: DeepLinker, useFactory: () => ConfigMock.instance()} | |
], | |
imports: [ | |
IonicModule | |
], | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment