From 95103ed4093a9afe5a034865788af835b144b98d Mon Sep 17 00:00:00 2001 From: gossi Date: Wed, 14 Aug 2024 16:06:29 +0200 Subject: [PATCH 1/3] Add config per reporter --- .../ember-cli-code-coverage/lib/attach-middleware.js | 5 +++-- .../ember-cli-code-coverage/lib/coverage-merge.js | 4 ++-- packages/ember-cli-code-coverage/lib/reports.js | 11 +++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 packages/ember-cli-code-coverage/lib/reports.js diff --git a/packages/ember-cli-code-coverage/lib/attach-middleware.js b/packages/ember-cli-code-coverage/lib/attach-middleware.js index 42154145..1cc7741c 100644 --- a/packages/ember-cli-code-coverage/lib/attach-middleware.js +++ b/packages/ember-cli-code-coverage/lib/attach-middleware.js @@ -3,12 +3,13 @@ const bodyParser = require('body-parser').json({ limit: '500mb' }); const libCoverage = require('istanbul-lib-coverage'); const libReport = require('istanbul-lib-report'); -const reports = require('istanbul-reports'); + const getConfig = require('./config'); const path = require('path'); const crypto = require('crypto'); const fs = require('fs-extra'); const libSourceMaps = require('istanbul-lib-source-maps'); +const { createReport } = require('./reports'); const sourceMapStore = libSourceMaps.createSourceMapStore(); @@ -219,7 +220,7 @@ function reportCoverage(map, root, configPath) { }); reporters.forEach((reporter) => { - let report = reports.create(reporter, {}); + let report = createReport(reporter); // call execute to synchronously create and write the report to disk report.execute(context); diff --git a/packages/ember-cli-code-coverage/lib/coverage-merge.js b/packages/ember-cli-code-coverage/lib/coverage-merge.js index f9328adc..93bf9dbb 100644 --- a/packages/ember-cli-code-coverage/lib/coverage-merge.js +++ b/packages/ember-cli-code-coverage/lib/coverage-merge.js @@ -3,6 +3,7 @@ const path = require('path'); const getConfig = require('./config'); const dir = require('node-dir'); +const { createReport } = require('./reports'); /** * Merge together coverage files created when running in multiple threads, @@ -15,7 +16,6 @@ module.exports = { run() { const libCoverage = require('istanbul-lib-coverage'); const libReport = require('istanbul-lib-report'); - const reports = require('istanbul-reports'); let config = this._getConfig(); @@ -56,7 +56,7 @@ module.exports = { }); reporters.forEach((reporter) => { - let report = reports.create(reporter, {}); + let report = createReport(reporter); // call execute to synchronously create and write the report to disk report.execute(context); diff --git a/packages/ember-cli-code-coverage/lib/reports.js b/packages/ember-cli-code-coverage/lib/reports.js new file mode 100644 index 00000000..aebf78a5 --- /dev/null +++ b/packages/ember-cli-code-coverage/lib/reports.js @@ -0,0 +1,11 @@ +const reports = require('istanbul-reports'); + +function createReport(reporter) { + return Array.isArray(reporter) + ? reports.create(reporter[0], reporter[1]) + : reports.create(reporter, {}); +} + +module.exports = { + createReport, +}; From 7950511bddaabf8d9026e47e2076049d8dc44cfe Mon Sep 17 00:00:00 2001 From: gossi Date: Wed, 14 Aug 2024 16:27:06 +0200 Subject: [PATCH 2/3] add tests --- test-packages/reports-test.mjs | 24 ++++++++++++++++++++++++ test-packages/utils.mjs | 9 ++++----- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 test-packages/reports-test.mjs diff --git a/test-packages/reports-test.mjs b/test-packages/reports-test.mjs new file mode 100644 index 00000000..3e8648d0 --- /dev/null +++ b/test-packages/reports-test.mjs @@ -0,0 +1,24 @@ +'use strict'; + +import { createReport } from '../packages/ember-cli-code-coverage/lib/reports'; +import { expect, describe, it } from 'vitest'; + +describe('reports', () => { + it('createReport with simple reporter', async () => { + const report = createReport('lcov'); + + expect(Object.keys(report)).toContain('lcov'); + }); + + it('createReport with options', async () => { + const report = createReport([ + 'lcov', + { + projectRoot: 'some/where/else', + }, + ]); + + expect(Object.keys(report)).toContain('lcov'); + expect(report.lcov.projectRoot).toBe('some/where/else'); + }); +}); diff --git a/test-packages/utils.mjs b/test-packages/utils.mjs index a255212c..2fc67151 100644 --- a/test-packages/utils.mjs +++ b/test-packages/utils.mjs @@ -8,7 +8,7 @@ import { execa } from 'execa'; export default async function setupTestDir(APP_DIR, env, deps) { const project = Project.fromDir(`test-packages/${APP_DIR}`, { linkDevDeps: true }); - for (const [key,value] of Object.entries(deps)) { + for (const [key, value] of Object.entries(deps)) { project.addDevDependency(key, value); } @@ -68,9 +68,8 @@ export async function assertCoverageExists(buildPath) { expect(summary).toMatchSnapshot(); } - export async function assertFileExists(path) { - if (!await exists(path)) { + if (!(await exists(path))) { throw new Error(`File ${path} does not exist`); } } @@ -82,5 +81,5 @@ export async function assertDirDoesNotExists(dir) { } export async function assertFileIsNotEmpty(path) { - return (await readFile(path, 'utf8').length) > 0; -} \ No newline at end of file + return (await readFile(path, 'utf8').length) > 0; +} From a9423499af7b700831e26d2292409875943be3b1 Mon Sep 17 00:00:00 2001 From: gossi Date: Wed, 14 Aug 2024 16:30:36 +0200 Subject: [PATCH 3/3] Add docs --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b23ee501..3e61c2bf 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,13 @@ Configuration is optional. It should be put in a file at `config/coverage.js` (` - `coverageEnvVar`: Defaults to `COVERAGE`. This is the environment variable that when set will cause coverage metrics to be generated. -- `reporters`: Defaults to `['lcov', 'html']`. The `json-summary` reporter will be added to anything set here, it is required. This can be any [reporters supported by Istanbul](https://github.com/gotwarlost/istanbul/tree/master/lib/report). +- `reporters`: Defaults to `['lcov', 'html']`. The `json-summary` reporter will + be added to anything set here, it is required. This can be any [reporters + supported by + Istanbul](https://github.com/gotwarlost/istanbul/tree/master/lib/report). + Reporters can be configured with array-style syntax, for example, here are + options to `lcov` with a different `projectRoot`: `[['lcov', { projectRoot: + '/packages/addon' }], 'html']` - `excludes`: Defaults to `['*/mirage/**/*']`. An array of globs to exclude from instrumentation. Useful to exclude files from coverage statistics.