MOBILE-3566 tests: Prepare unit tests with jest

main
Noel De Martin 2020-10-06 09:44:27 +02:00 committed by Dani Palou
parent 81ce1a7d01
commit a4126dd67b
13 changed files with 3718 additions and 1623 deletions

5
.travis.yml 100644
View File

@ -0,0 +1,5 @@
language: node_js
node_js: 12
cache: npm
script:
- npm run test:ci

View File

@ -94,35 +94,6 @@
"browserTarget": "app:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"styles": [],
"scripts": [],
"assets": [
{
"glob": "favicon.ico",
"input": "src/",
"output": "/"
},
{
"glob": "**/*",
"input": "src/assets",
"output": "/assets"
}
]
},
"configurations": {
"ci": {
"progress": false,
"watch": false
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
@ -134,21 +105,6 @@
"exclude": ["**/node_modules/**"]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "app:serve"
},
"configurations": {
"production": {
"devServerTarget": "app:serve:production"
},
"ci": {
"devServerTarget": "app:serve:ci"
}
}
},
"ionic-cordova-build": {
"builder": "@ionic/angular-toolkit:cordova-build",
"options": {

22
jest.config.js 100644
View File

@ -0,0 +1,22 @@
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/src/tests/setup.ts'],
testMatch: ['**/?(*.)test.ts'],
collectCoverageFrom: [
'src/**/*.{ts,html}',
'!src/tests/**/*',
],
transform: {
'^.+\\.(ts|html)$': 'ts-jest',
},
transformIgnorePatterns: ['node_modules/(?!@ionic-native|@ionic)'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' }),
globals: {
'ts-jest': {
tsConfig: './tsconfig.test.json',
},
},
};

5103
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -21,9 +21,11 @@
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
"test": "jest --verbose",
"test:ci": "jest -ci --runInBand --verbose",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"lint": "ng lint"
},
"dependencies": {
"@angular/common": "~10.0.0",
@ -89,18 +91,11 @@
"@angular/compiler-cli": "~10.0.0",
"@angular/language-service": "~10.0.0",
"@ionic/angular-toolkit": "^2.3.0",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~3.3.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"jest": "^26.5.0",
"jest-preset-angular": "^8.3.1",
"ts-jest": "^26.4.1",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~3.9.5"

View File

@ -0,0 +1,30 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { createComponent, prepareComponentTest } from '@/tests/utils';
import { AppComponent } from '@app/app.component';
describe('App component', () => {
beforeEach(() => prepareComponentTest(AppComponent));
it('should render', () => {
const fixture = createComponent(AppComponent);
expect(fixture.debugElement.componentInstance).toBeTruthy();
expect(fixture.nativeElement.querySelector('ion-router-outlet')).toBeTruthy();
});
});

View File

@ -0,0 +1,30 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { createComponent, prepareComponentTest } from '@/tests/utils';
import { CoreLoginInitPage } from '@core/login/pages/init/init.page';
describe('CoreLogin Init Page', () => {
beforeEach(() => prepareComponentTest(CoreLoginInitPage));
it('should render', () => {
const fixture = createComponent(CoreLoginInitPage);
expect(fixture.debugElement.componentInstance).toBeTruthy();
expect(fixture.nativeElement.querySelector('ion-spinner')).toBeTruthy();
});
});

View File

@ -0,0 +1 @@
import 'jest-preset-angular';

29
src/tests/utils.ts 100644
View File

@ -0,0 +1,29 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { CUSTOM_ELEMENTS_SCHEMA, Type } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
export async function prepareComponentTest(component: any): Promise<void> {
TestBed.configureTestingModule({
declarations: [component],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
});
await TestBed.compileComponents();
}
export function createComponent<T>(component: Type<T>): ComponentFixture<T> {
return TestBed.createComponent(component);
}

View File

@ -25,6 +25,6 @@
"src/**/*.d.ts"
],
"exclude": [
"src/**/*.spec.ts"
"src/**/*.test.ts"
]
}

View File

@ -25,7 +25,8 @@
"@directives/*": ["app/directives/*"],
"@pipes/*": ["app/pipes/*"],
"@services/*": ["app/services/*"],
"@singletons/*": ["app/singletons/*"]
"@singletons/*": ["app/singletons/*"],
"@tests/*": ["tests/*"]
}
},
"angularCompilerOptions": {

View File

@ -1,18 +0,0 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"src/test.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}

33
tsconfig.test.json 100644
View File

@ -0,0 +1,33 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"esModuleInterop": true,
"outDir": "./out-tsc/tests",
"types": [
"jest",
"node"
],
"paths": {
"@/*": ["*"],
"@addon/*": ["app/addon/*"],
"@app/*": ["app/*"],
"@classes/*": ["app/classes/*"],
"@components/*": ["app/components/*"],
"@core/*": ["app/core/*"],
"@directives/*": ["app/directives/*"],
"@pipes/*": ["app/pipes/*"],
"@services/*": ["app/services/*"],
"@singletons/*": ["app/singletons/*"],
"@tests/*": ["tests/*"]
}
},
"files": [
"src/tests/setup.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.test.ts",
"src/**/*.d.ts",
"src/tests/**/*.ts"
]
}