Skip to content

Commit 8b96204

Browse files
committed
Try this combination of ESM and CJS to see if it works
1 parent b0cb71b commit 8b96204

File tree

7 files changed

+29
-26
lines changed

7 files changed

+29
-26
lines changed

lib/vitest-reporter/build/coverage-reporter.js renamed to lib/vitest-reporter/build/coverage-reporter.cjs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// src/coverage-reporter.ts
2-
import fs from "fs";
3-
import { ReportBase } from "istanbul-lib-report";
1+
"use strict";
2+
3+
// src/coverage-reporter.cts
4+
var fs = require("fs");
5+
var report = require("istanbul-lib-report");
46
function isFull(metrics) {
57
return metrics.statements.pct === 100 && metrics.branches.pct === 100 && metrics.functions.pct === 100 && metrics.lines.pct === 100;
68
}
@@ -37,12 +39,12 @@ function getUncoveredLines(node) {
3739
return ranges;
3840
}
3941
var headers = ["Statements", "Branches", "Functions", "Lines"];
40-
var GithubActionsCoverageReporter = class extends ReportBase {
42+
module.exports = class GithubActionsCoverageReporter extends report.ReportBase {
4143
skipEmpty;
4244
skipFull;
4345
results = {};
4446
cw = null;
45-
watermarks = null;
47+
watermarks = {};
4648
constructor(opts) {
4749
super(opts);
4850
this.skipEmpty = Boolean(opts.skipEmpty);
@@ -84,7 +86,7 @@ var GithubActionsCoverageReporter = class extends ReportBase {
8486
return this.onSummary(node);
8587
}
8688
formatter(pct, watermark) {
87-
if (!this.watermarks || this.watermarks[watermark] === void 0) return `<td>${pct}%</td>`;
89+
if (this.watermarks[watermark] === void 0) return `<td>${pct}%</td>`;
8890
const [low, high] = this.watermarks[watermark];
8991
if (pct < low) {
9092
return `<td><p style="color:red">${pct}%</p></td>`;
@@ -123,6 +125,3 @@ var GithubActionsCoverageReporter = class extends ReportBase {
123125
this.cw.close();
124126
}
125127
};
126-
export {
127-
GithubActionsCoverageReporter as default
128-
};

lib/vitest-reporter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"scripts": {
2121
"build": "yarn build:coverage-reporter && yarn build:test-reporter",
22-
"build:coverage-reporter": "esbuild --outfile=./build/coverage-reporter.js --format=esm --bundle=true --minify=false --external:\"istanbul-lib-report\" --platform=node src/coverage-reporter.ts",
22+
"build:coverage-reporter": "esbuild --outfile=./build/coverage-reporter.cjs --format=cjs --bundle=true --minify=false --external:\"istanbul-lib-report\" --platform=node src/coverage-reporter.cts",
2323
"build:test-reporter": "esbuild --outfile=./build/test-reporter.js --format=esm --bundle=true --minify --external:\"vitest*\" --platform=node src/test-reporter.ts",
2424
"test": "vitest"
2525
}

lib/vitest-reporter/src/__tests__/coverage.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import fs from 'fs';
22
import { describe, expect, it as baseIt, vi } from 'vitest';
3-
import CoverageReporter from '../coverage-reporter.js';
3+
import CoverageReporter from '../coverage-reporter.cjs'
44

55
vi.spyOn(fs, 'createWriteStream').mockReturnValue({
66
write: () => true,
77
close: () => {}
88
} as any);
99

1010
describe(CoverageReporter, () => {
11-
const it = baseIt.extend<{ reporter: CoverageReporter }>({
11+
const it = baseIt.extend<{ reporter: typeof CoverageReporter }>({
1212
reporter: new CoverageReporter({})
1313
});
1414

lib/vitest-reporter/src/__tests__/test-reporter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, expect, it as baseIt, vi } from 'vitest';
33
import TestReporter from '../test-reporter.js';
44

55
vi.spyOn(fs, 'createWriteStream').mockImplementation(() => ({
6-
write: () => {},
6+
write: () => true,
77
close: () => {}
88
} as any));
99

lib/vitest-reporter/src/coverage-reporter.ts renamed to lib/vitest-reporter/src/coverage-reporter.cts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/**
2-
* For whatever reason, this reporter doesn't really work if its compiled to ESM
3-
* so its compiled to CJS
2+
* For whatever reason, this reporter doesn't really work if its written in ESM
3+
* so its written in and compiled to CJS
44
*
55
* Heavily based on the default text reporter
66
*/
7-
import fs from 'fs';
7+
8+
import fs = require('fs')
9+
import report = require('istanbul-lib-report');
810
import type { CoverageSummary } from 'istanbul-lib-coverage';
9-
import { ReportBase, type ContextOptions, type ReportNode, type Watermarks } from 'istanbul-lib-report';
1011

1112
/**
1213
* Determines if the coverage summary has full coverage
@@ -23,7 +24,7 @@ function isFull(metrics: CoverageSummary) {
2324
/**
2425
* Determines the uncovered lines
2526
*/
26-
function getUncoveredLines(node: ReportNode) {
27+
function getUncoveredLines(node: report.ReportNode) {
2728
if (node.isSummary()) {
2829
return [];
2930
}
@@ -77,12 +78,12 @@ interface ResultObject {
7778
/**
7879
* A Vitest coverage reporter that writes to the Github Actions summary
7980
*/
80-
export default class GithubActionsCoverageReporter extends ReportBase {
81+
module.exports = class GithubActionsCoverageReporter extends report.ReportBase {
8182
private readonly skipEmpty: boolean;
8283
private readonly skipFull: boolean;
8384
private readonly results: Record<string, ResultObject> = {};
8485
private cw: fs.WriteStream | null = null;
85-
private watermarks: Partial<Watermarks> | null = null;
86+
private watermarks: Partial<report.Watermarks> = {};
8687

8788
constructor(opts: any) {
8889
super(opts);
@@ -91,7 +92,7 @@ export default class GithubActionsCoverageReporter extends ReportBase {
9192
this.skipFull = Boolean(opts.skipFull);
9293
}
9394

94-
onStart(_node: any, context: ContextOptions) {
95+
onStart(_node: any, context: report.ContextOptions) {
9596
if (!process.env.GITHUB_STEP_SUMMARY) {
9697
console.log('Reporter not being executed in Github Actions environment');
9798
return;
@@ -108,7 +109,7 @@ export default class GithubActionsCoverageReporter extends ReportBase {
108109
this.cw.write('</tr></thead><tbody>');
109110
}
110111

111-
onSummary(node: ReportNode) {
112+
onSummary(node: report.ReportNode) {
112113
const nodeName = node.getRelativeName() || 'All Files';
113114
const rawMetrics = node.getCoverageSummary(false);
114115
const isEmpty = rawMetrics.isEmpty();
@@ -129,12 +130,12 @@ export default class GithubActionsCoverageReporter extends ReportBase {
129130
};
130131
}
131132

132-
onDetail(node: ReportNode) {
133+
onDetail(node: report.ReportNode) {
133134
return this.onSummary(node);
134135
}
135136

136-
private formatter(pct: number, watermark: keyof Watermarks) {
137-
if (!this.watermarks || this.watermarks[watermark] === undefined) return `<td>${pct}%</td>`;
137+
private formatter(pct: number, watermark: keyof report.Watermarks) {
138+
if (this.watermarks[watermark] === undefined) return `<td>${pct}%</td>`;
138139
const [low, high] = this.watermarks[watermark];
139140

140141
if (pct < low) {

lib/vitest-reporter/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Repotools regular tsconfig
22
{
33
"extends": ["../tsconfig.json"],
4-
"include": ["src"],
4+
"include": ["src/coverage-reporter.cts", "src/**/*.ts"],
55
"compilerOptions": {
66
"module": "nodenext",
77
"moduleResolution": "nodenext",

lib/vitest-reporter/vitest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { defineConfig } from 'vitest/config';
44
import rootConfig from '../../vitest.config.js';
55

66
export default defineConfig({
7+
esbuild: {
8+
include: ['**/*.{cts,ts}']
9+
},
710
optimizeDeps: {
811
include: ['istanbul-lib-report'],
912
},

0 commit comments

Comments
 (0)