Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"scripts": {
"pretest": "npm run build",
"test": "poku test/e2e",
"test": "poku --sequential test/e2e",
"prebuild": "rm -rf lib",
"build": "tsc",
"lint": "prettier --check .",
Expand All @@ -36,7 +36,9 @@
},
"dependencies": {
"c8": "^10.1.3",
"jsonc.min": "^1.1.2"
"jsonc.min": "^1.1.2",
"toml.min": "^1.0.0",
"yaml.min": "^1.0.0"
},
"peerDependencies": {
"monocart-coverage-reports": "^2.12.9",
Expand Down
43 changes: 42 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@ import type { CoverageOptions } from './types.js';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { JSONC } from 'jsonc.min';
import { parse as tomlParse } from 'toml.min';
import { parse as yamlParse } from 'yaml.min';

const scriptExtensions = new Set([
'.js',
'.mjs',
'.cjs',
'.ts',
'.mts',
'.cts',
]);

const isScript = (path: string): boolean =>
scriptExtensions.has(getExtension(path));

const isToml = (path: string): boolean => getExtension(path) === '.toml';

const isYaml = (path: string): boolean => {
const ext = getExtension(path);
return ext === '.yml' || ext === '.yaml';
};

const getExtension = (filePath: string): string => {
const dotIndex = filePath.lastIndexOf('.');
if (dotIndex === -1) return '';
return filePath.slice(dotIndex);
};

const parseConfig = (content: string, filePath: string): CoverageOptions => {
if (isToml(filePath)) return tomlParse<CoverageOptions>(content);
if (isYaml(filePath)) return yamlParse<CoverageOptions>(content);
return JSONC.parse<CoverageOptions>(content);
};

const kebabMap: Record<string, string> = {
'reports-dir': 'reportsDirectory',
Expand Down Expand Up @@ -40,20 +73,28 @@ export const loadConfig = (
'.c8rc',
'.c8rc.json',
'.c8rc.jsonc',
'.c8rc.toml',
'.c8rc.yaml',
'.c8rc.yml',
'.nycrc',
'.nycrc.json',
'.nycrc.jsonc',
'.nycrc.toml',
'.nycrc.yaml',
'.nycrc.yml',
];

for (const file of expectedFiles) {
if (isScript(file)) continue;

const filePath = join(cwd, file);

if (!existsSync(filePath)) continue;

try {
const content = readFileSync(filePath, 'utf8');

return mapKeys(JSONC.parse(content) as Record<string, unknown>);
return mapKeys(parseConfig(content, file));
} catch {}
}

Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ export const coverage = (

setup(context) {
if (options.requireFlag && !process.argv.includes('--coverage')) return;
if (context.runtime !== 'node')
console.warn(
`[@pokujs/c8] V8 coverage is only supported on Node.js (current runtime: ${context.runtime}). Coverage data may not be collected.`
);

enabled = true;

const cliConfig = process.argv
.find((arg) => arg.startsWith('--coverageConfig'))
?.split('=')[1];

const fileConfig = loadConfig(context.cwd, cliConfig ?? options.config);
options = { ...fileConfig, ...options };

if (context.runtime !== 'node')
console.warn(
`[@pokujs/c8] V8 coverage is only supported on Node.js (current runtime: ${context.runtime}). Coverage data may not be collected.`
);

options = { ...fileConfig, ...options };
originalEnv = process.env.NODE_V8_COVERAGE;
userProvidedTempDir = typeof options.tempDirectory === 'string';

Expand Down
5 changes: 5 additions & 0 deletions test/__fixtures__/e2e/configs/.c8rc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
// JSONC: comments should be supported in .jsonc files
"include": ["src/**"],
"reporter": ["text"]
}
2 changes: 2 additions & 0 deletions test/__fixtures__/e2e/configs/.c8rc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include = ["src/**"]
reporter = ["text"]
4 changes: 4 additions & 0 deletions test/__fixtures__/e2e/configs/.c8rc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include:
- 'src/**'
reporter:
- text
11 changes: 11 additions & 0 deletions test/__fixtures__/e2e/configs/config-file-jsonc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { coverage } = require('../../../../lib/index.js');

/** @type {import('poku').PokuConfig} */
module.exports = {
include: ['test/'],
plugins: [
coverage({
config: 'configs/.c8rc.jsonc',
}),
],
};
11 changes: 11 additions & 0 deletions test/__fixtures__/e2e/configs/config-file-toml.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { coverage } = require('../../../../lib/index.js');

/** @type {import('poku').PokuConfig} */
module.exports = {
include: ['test/'],
plugins: [
coverage({
config: 'configs/.c8rc.toml',
}),
],
};
11 changes: 11 additions & 0 deletions test/__fixtures__/e2e/configs/config-file-yaml.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { coverage } = require('../../../../lib/index.js');

/** @type {import('poku').PokuConfig} */
module.exports = {
include: ['test/'],
plugins: [
coverage({
config: 'configs/.c8rc.yaml',
}),
],
};
17 changes: 17 additions & 0 deletions test/e2e/coverage-config-file-jsonc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assert, test } from 'poku';
import { inspectPoku } from 'poku/plugins';

const fixtureDir = 'test/__fixtures__/e2e';
const pokuBin = 'node_modules/poku/lib/bin/index.js';

test('loads JSONC config file with .jsonc extension', async () => {
const result = await inspectPoku({
command: '-c=configs/config-file-jsonc.config.js',
spawnOptions: { cwd: fixtureDir },
bin: pokuBin,
});

assert.strictEqual(result.exitCode, 0);
assert(result.stdout.includes('math.ts'));
assert(result.stdout.includes('%'));
});
17 changes: 17 additions & 0 deletions test/e2e/coverage-config-file-toml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assert, test } from 'poku';
import { inspectPoku } from 'poku/plugins';

const fixtureDir = 'test/__fixtures__/e2e';
const pokuBin = 'node_modules/poku/lib/bin/index.js';

test('loads TOML config file', async () => {
const result = await inspectPoku({
command: '-c=configs/config-file-toml.config.js',
spawnOptions: { cwd: fixtureDir },
bin: pokuBin,
});

assert.strictEqual(result.exitCode, 0);
assert(result.stdout.includes('math.ts'));
assert(result.stdout.includes('%'));
});
17 changes: 17 additions & 0 deletions test/e2e/coverage-config-file-yaml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assert, test } from 'poku';
import { inspectPoku } from 'poku/plugins';

const fixtureDir = 'test/__fixtures__/e2e';
const pokuBin = 'node_modules/poku/lib/bin/index.js';

test('loads YAML config file', async () => {
const result = await inspectPoku({
command: '-c=configs/config-file-yaml.config.js',
spawnOptions: { cwd: fixtureDir },
bin: pokuBin,
});

assert.strictEqual(result.exitCode, 0);
assert(result.stdout.includes('math.ts'));
assert(result.stdout.includes('%'));
});
Loading