UNPKG

karma-typescript

Version:

Simplifying running unit tests with coverage for Typescript projects.

1 lines 7.94 kB
{"version":3,"file":"reporter.js","sourceRoot":"","sources":["../../src/karma/reporter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wDAA0D;AAC1D,oDAAsD;AACtD,6DAA+D;AAC/D,kDAAoD;AAEpD,+BAAiC;AACjC,2BAA6B;AAW7B,IAAM,YAAY,GAAG,kBAAkB,CAAC;AAExC;IAOI,kBAAY,MAAqB,EAAE,SAAoB;QAEnD,4DAA4D;QAC5D,IAAM,IAAI,GAAG,IAAI,CAAC;QAElB,gDAAgD;QAChD,IAAI,CAAC,MAAM,GAAG,UAAS,qBAA0B,EAAE,MAAW;YAAhD,iBAoEb;YAlEG,qBAAqB,CAAC,IAAI,CAAC,CAAC;YAE5B,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,mBAAY,YAAY,CAAE,CAAC,CAAC;YAErD,IAAI,CAAC,UAAU,GAAG;gBACd,IAAI,CAAC,WAAW,GAAG,IAAI,OAAO,EAAY,CAAC;YAC/C,CAAC,CAAC;YAEF,IAAI,CAAC,cAAc,GAAG,cAAa,CAAC,CAAC;YACrC,IAAI,CAAC,WAAW,GAAG,cAAa,CAAC,CAAC;YAElC,IAAI,CAAC,iBAAiB,GAAG,UAAC,OAAY,EAAE,MAAW;gBAC/C,IAAI,MAAM,IAAI,MAAM,CAAC,QAAQ,EAAE;oBAC3B,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;iBAClD;YACL,CAAC,CAAC;YAEF,IAAI,CAAC,aAAa,GAAG,UAAO,UAA6B,EAAE,OAAY;;;;;;4BAC7D,QAAQ,GAAU,EAAE,CAAC;4BAC3B,UAAU,CAAC,OAAO,CAAC,UAAC,OAAO,IAAK,OAAA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAtB,CAAsB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C;IAChB,CAAC;IACL,eAAC;AAAD,CAAC,AAvGD,IAuGC;AAvGY,4BAAQ","sourcesContent":["import * as istanbulCoverage from \"istanbul-lib-coverage\";\nimport * as istanbulReport from \"istanbul-lib-report\";\nimport * as istanbulSourceMaps from \"istanbul-lib-source-maps\";\nimport * as istanbulReports from \"istanbul-reports\";\n\nimport * as lodash from \"lodash\";\nimport * as path from \"path\";\n\nimport { Logger } from \"log4js\";\n\nimport { Threshold } from \"../istanbul/threshold\";\nimport { Configuration } from \"../shared/configuration\";\n\ntype BrowserCollection = {\n forEach: any[]['forEach'];\n}\n\nconst reporterName = \"karma-typescript\";\n\nexport class Reporter {\n\n public create: (baseReporterDecorator: any, logger: any) => void;\n\n private log: Logger;\n private coverageMap: WeakMap<any, any>;\n\n constructor(config: Configuration, threshold: Threshold) {\n\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const that = this;\n\n // tslint:disable-next-line:only-arrow-functions\n this.create = function(baseReporterDecorator: any, logger: any) {\n\n baseReporterDecorator(this);\n\n that.log = logger.create(`reporter.${reporterName}`);\n\n this.onRunStart = () => {\n that.coverageMap = new WeakMap<any, any>();\n };\n\n this.onBrowserStart = () => { /**/ };\n this.specFailure = () => { /**/ };\n\n this.onBrowserComplete = (browser: any, result: any) => {\n if (result && result.coverage) {\n that.coverageMap.set(browser, result.coverage);\n }\n };\n\n this.onRunComplete = async (collection: BrowserCollection, results: any) => {\n const browsers: any[] = [];\n collection.forEach((browser) => browsers.push(browser));\n\n const coverageChecks = browsers.map(async (browser) => {\n const coverage = that.coverageMap.get(browser);\n const coverageMap = istanbulCoverage.createCoverageMap();\n coverageMap.merge(coverage);\n\n const sourceMapStore = istanbulSourceMaps.createSourceMapStore();\n const remappedCoverageMap = await sourceMapStore.transformCoverage(coverageMap);\n\n Object.keys(config.reports).forEach((reportType: any) => {\n\n const reportConfig = config.reports[reportType] as any;\n const directory = that.getReportDestination(browser, reportConfig, reportType);\n\n if (directory) {\n that.log.debug(\"Writing coverage to %s\", directory);\n }\n\n const istanbulReportOptions = {\n coverageMap: remappedCoverageMap,\n dir: directory,\n sourceFinder: sourceMapStore.sourceFinder,\n watermarks: config.coverageOptions.watermarks\n };\n const context = istanbulReport.createContext(istanbulReportOptions);\n\n istanbulReports\n .create(reportType, { file: reportConfig ? reportConfig.filename : undefined })\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n .execute(context);\n\n });\n\n return results\n && config.hasCoverageThreshold\n && !threshold.check(browser, remappedCoverageMap);\n });\n\n const isCoverageCheckFailed = (await Promise.all(coverageChecks))\n .some(isCheckFailed => isCheckFailed);\n\n if (isCoverageCheckFailed && config.karma.singleRun) {\n process.exit(1);\n }\n };\n };\n\n Object.assign(this.create, { $inject: [\"baseReporterDecorator\", \"logger\", \"config\"] });\n }\n\n private getReportDestination(browser: any, reportConfig: any, reportType: any) {\n\n if (lodash.isPlainObject(reportConfig)) {\n let subdirectory = reportConfig.subdirectory ?? browser.name;\n if (typeof subdirectory === \"function\") {\n subdirectory = subdirectory(browser);\n }\n\n return path.join(reportConfig.directory ?? \"coverage\", subdirectory);\n }\n\n if (lodash.isString(reportConfig) && reportConfig.length > 0) {\n return path.join(reportConfig, browser.name, reportType);\n }\n\n return null;\n }\n}\n"]}