Karma test refactor to support test in portal and coverage report

Signed-off-by: Meina Zhou <meinaz@vmware.com>
This commit is contained in:
Meina Zhou 2019-09-23 13:52:07 +08:00
parent 63373c8ef9
commit 898442370a
8 changed files with 1238 additions and 377 deletions

View File

@ -171,7 +171,29 @@
"main": "lib/src/test.ts",
"tsConfig": "lib/tsconfig.lib.json",
"karmaConfig": "lib/karma.conf.js"
}
},
"scripts": [
"node_modules/core-js/client/shim.min.js",
"node_modules/mutationobserver-shim/dist/mutationobserver.min.js",
"node_modules/@webcomponents/custom-elements/custom-elements.min.js",
"node_modules/@clr/icons/clr-icons.min.js",
"node_modules/web-animations-js/web-animations.min.js",
"node_modules/marked/lib/marked.js",
"node_modules/prismjs/prism.js",
"node_modules/prismjs/components/prism-yaml.min.js"
],
"styles": [
"node_modules/@clr/icons/clr-icons.min.css",
"node_modules/@clr/ui/clr-ui.min.css",
"node_modules/prismjs/themes/prism-solarizedlight.css",
"src/styles.css"
],
"assets": [
"src/images",
"src/favicon.ico",
"src/setting.json",
"src/i18n"
]
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",

View File

@ -1,47 +1,87 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html
// https://karma-runner.github.io/1.0/config/configuration-file.html
const path = require('path');
module.exports = function (config) {
config.set({
basePath: '/',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-mocha-reporter'),
require('karma-remap-istanbul'),
require('@angular-devkit/build-angular/plugins/karma')
],
files: [
{pattern: './src/test.ts', watched: false}
],
preprocessors: {
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-mocha-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
// reports can be any that are listed here: https://github.com/istanbuljs/istanbuljs/tree/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib
reports: ['html', 'lcovonly', 'text-summary'],
// base output directory. If you include %browser% in the path it will be replaced with the karma browser name
dir: path.join(__dirname, 'coverage'),
// Combines coverage information from multiple browsers into one report rather than outputting a report
// for each browser.
combineBrowserReports: true,
// if using webpack and pre-loaders, work around webpack breaking the source path
fixWebpackSourcePaths: true,
// Omit files with no statements, no functions and no branches from the report
skipFilesWithNoCoverage: true,
// Most reporters accept additional config options. You can pass these through the `report-config` option
'report-config': {
// all options available at: https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/html/index.js#L135-L137
html: {
// outputs the report in ./coverage/html
subdir: 'html'
}
},
mime: {
'text/x-typescript': ['ts', 'tsx']
},
remapIstanbulReporter: {
dir: require('path').join(__dirname, 'coverage'), reports: {
html: 'coverage',
lcovonly: './coverage/coverage.lcov'
}
},
reporters: config.angularCli && config.angularCli.codeCoverage
? ['mocha', 'karma-remap-istanbul']
: ['mocha'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
// enforce percentage thresholds
// anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode
thresholds: {
emitWarning: false, // set to `true` to not fail the test command when thresholds are not met
// thresholds for all files
global: {
statements: 10,
lines: 10,
branches: 0,
functions: 10
},
singleRun: true
// thresholds per file
each: {
statements: 0,
lines: 0,
branches: 0,
functions: 0
}
}
},
reporters: ['progress', 'mocha','coverage-istanbul'],
mochaReporter: {
output: 'minimal'
},
reportSlowerThan: 100,
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
singleRun: true,
browsers: ['ChromeHeadlessNoSandbox'],
browserDisconnectTolerance: 2,
browserNoActivityTimeout: 50000,
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
restartOnFileChange: true
});
};
};

View File

@ -0,0 +1,87 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
const path = require('path');
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-mocha-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
// reports can be any that are listed here: https://github.com/istanbuljs/istanbuljs/tree/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib
reports: ['html', 'lcovonly', 'text-summary'],
// base output directory. If you include %browser% in the path it will be replaced with the karma browser name
dir: path.join(__dirname, 'coverage'),
// Combines coverage information from multiple browsers into one report rather than outputting a report
// for each browser.
combineBrowserReports: true,
// if using webpack and pre-loaders, work around webpack breaking the source path
fixWebpackSourcePaths: true,
// Omit files with no statements, no functions and no branches from the report
skipFilesWithNoCoverage: true,
// Most reporters accept additional config options. You can pass these through the `report-config` option
'report-config': {
// all options available at: https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/html/index.js#L135-L137
html: {
// outputs the report in ./coverage/html
subdir: 'html'
}
},
// enforce percentage thresholds
// anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode
thresholds: {
emitWarning: false, // set to `true` to not fail the test command when thresholds are not met
// thresholds for all files
global: {
statements: 10,
lines: 10,
branches: 0,
functions: 10
},
// thresholds per file
each: {
statements: 0,
lines: 0,
branches: 0,
functions: 0
}
}
},
reporters: ['progress', 'mocha','coverage-istanbul'],
mochaReporter: {
output: 'minimal'
},
reportSlowerThan: 100,
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
singleRun: true,
browsers: ['ChromeHeadlessNoSandbox'],
browserDisconnectTolerance: 2,
browserNoActivityTimeout: 50000,
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
restartOnFileChange: true
});
};

View File

@ -1,36 +1,42 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/lib",
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"typeRoots": ["node_modules/@types"],
"compilerOptions": {
"outDir": "../out-tsc/lib",
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"baseUrl": "./",
"typeRoots": [
"../node_modules/@types"
],
"lib": [
"dom",
"es2015"
],
"paths": {
"lib": [
"dom",
"es2015"
"dist/lib"
],
"paths": {
"@angular/*": [ "../node_modules/@angular/*"]
"lib/*": [
"dist/lib/*"
],
"@angular/*": [ "./node_modules/@angular/*"]
}
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"flatModuleId": "AUTOGENERATED",
"flatModuleOutFile": "AUTOGENERATED"
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"flatModuleId": "AUTOGENERATED",
"flatModuleOutFile": "AUTOGENERATED"
},
"exclude": [
]
}

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,12 @@
"start": "ng serve --ssl true --ssl-key ssl/server.key --ssl-cert ssl/server.crt --host 0.0.0.0 --proxy-config proxy.config.json",
"lint": "tslint \"src/**/*.ts\"",
"lint:lib": "tslint \"lib/**/*.ts\" -e \"lib/dist/**/*\" ",
"test": "ng test harbor-portal",
"test": "ng test --code-coverage",
"test:watch": "ng test --code-coverage --watch",
"test:debug": "ng test --code-coverage --source-map false",
"test:chrome": "ng test --code-coverage --browsers Chrome",
"test:headless": "ng test --watch=false --no-progress --code-coverage --browsers=ChromeNoSandboxHeadless",
"test:chrome-debug": "ng test --code-coverage --browsers Chrome --watch",
"pree2e": "webdriver-manager update",
"e2e": "protractor",
"build": "ng build --aot",
@ -71,9 +76,10 @@
"codelyzer": "~5.0.0",
"enhanced-resolve": "^3.0.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma": "^1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-cli": "^1.0.1",
"karma-coverage": "^2.0.1",
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "^2.0.0",
"karma-jasmine-html-reporter": "^0.2.2",

View File

@ -1,16 +1,19 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { LicenseComponent } from './license.component';
describe('LicenseComponent', () => {
fdescribe('LicenseComponent', () => {
let component: LicenseComponent;
let fixture: ComponentFixture<LicenseComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LicenseComponent ]
declarations: [LicenseComponent],
imports: [
HttpClientTestingModule
]
})
.compileComponents();
.compileComponents();
}));
beforeEach(() => {

View File

@ -38,7 +38,7 @@ getTestBed().initTestEnvironment(
platformBrowserDynamicTesting()
);
// Then we find all the tests.
let context = require.context('../lib', true, /\.spec\.ts/);
let context = require.context('./', true, /\.spec\.ts/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.