diff --git a/packages/istanbul-lib-report/src/file-writer.ts b/packages/istanbul-lib-report/src/file-writer.ts index c0c5b8d..f7bb755 100644 --- a/packages/istanbul-lib-report/src/file-writer.ts +++ b/packages/istanbul-lib-report/src/file-writer.ts @@ -94,14 +94,38 @@ class ConsoleWriter extends ContentWriter { high: "32;1", }; - /* istanbul ignore next: different modes for CI and local */ - if (process.stdout.isTTY && process.stdout.hasColors() && colors[clazz as string]) { + if (colors[clazz as string] && supportsColor()) { return `\u001b[${colors[clazz as string]}m${str}\u001b[0m`; } return str; } } +/** + * Whether ANSI colour codes should be emitted on `stream`. Mirrors the + * `supports-color` semantics upstream istanbul relied on: + * + * - `FORCE_COLOR` wins over everything: `0`/`false` disables colours, any + * other value (including an empty string) enables them even when the + * stream is not a TTY, e.g. when a CI runner pipes the output. + * - `NO_COLOR` or `NODE_DISABLE_COLORS` being set disables colours. + * - Otherwise colours are used only for a TTY that reports colour support. + */ +export function supportsColor( + stream: NodeJS.WriteStream = process.stdout, + env: NodeJS.ProcessEnv = process.env, +): boolean { + const force = env.FORCE_COLOR; + if (force !== undefined) { + return force !== "0" && force !== "false"; + } + if (env.NO_COLOR !== undefined || env.NODE_DISABLE_COLORS !== undefined) { + return false; + } + /* istanbul ignore next: depends on whether the test runner's stdout is a TTY */ + return Boolean(stream.isTTY && stream.hasColors?.()); +} + /** * utility for writing files under a specific directory * @class FileWriter diff --git a/packages/istanbul-lib-report/src/reports/cobertura/index.ts b/packages/istanbul-lib-report/src/reports/cobertura/index.ts index 10c4d44..ac569d7 100644 --- a/packages/istanbul-lib-report/src/reports/cobertura/index.ts +++ b/packages/istanbul-lib-report/src/reports/cobertura/index.ts @@ -115,8 +115,11 @@ class CoberturaReport extends ReportBase { this.xml!.openTag("methods"); const fnMap = fileCoverage.fnMap; - Object.entries(fnMap).forEach(([k, { name, decl }]) => { + Object.entries(fnMap).forEach(([k, { name, decl, loc }]) => { const hits = fileCoverage.f[k]; + // Some versions of the instrumenter in the wild populate 'loc' + // but not 'decl': + const start = (decl || loc).start; this.xml!.openTag("method", { name: escape(name), hits, @@ -125,7 +128,7 @@ class CoberturaReport extends ReportBase { this.xml!.openTag("lines"); //Add the function definition line and hits so that jenkins cobertura plugin records method hits this.xml!.inlineTag("line", { - number: decl.start.line, + number: start.line, hits, }); this.xml!.closeTag("lines"); diff --git a/packages/istanbul-lib-report/test/file-writer.test.ts b/packages/istanbul-lib-report/test/file-writer.test.ts index 2d4457e..488fcd6 100644 --- a/packages/istanbul-lib-report/test/file-writer.test.ts +++ b/packages/istanbul-lib-report/test/file-writer.test.ts @@ -1,9 +1,9 @@ import fs from "node:fs"; import path from "node:path"; -import { describe, it, assert, beforeEach, afterEach } from "vitest"; +import { describe, it, assert, beforeEach, afterEach, afterAll, vi } from "vitest"; -import FileWriter from "../src/file-writer"; +import FileWriter, { supportsColor } from "../src/file-writer"; const dataDir = path.resolve(import.meta.dirname, ".data"); @@ -82,3 +82,46 @@ describe("file-writer", () => { }); }); }); + +describe("supportsColor", () => { + afterAll(() => { + vi.unstubAllEnvs(); + }); + + const tty = { isTTY: true, hasColors: () => true } as unknown as NodeJS.WriteStream; + const pipe = { isTTY: false } as unknown as NodeJS.WriteStream; + + it("uses colors for a tty that supports them", () => { + assert.isTrue(supportsColor(tty, {})); + assert.isFalse(supportsColor({ ...tty, hasColors: () => false } as NodeJS.WriteStream, {})); + }); + + it("does not use colors when the stream is not a tty", () => { + assert.isFalse(supportsColor(pipe, {})); + }); + + it("honors FORCE_COLOR even when the stream is not a tty", () => { + assert.isTrue(supportsColor(pipe, { FORCE_COLOR: "1" })); + assert.isTrue(supportsColor(pipe, { FORCE_COLOR: "" })); + assert.isTrue(supportsColor(pipe, { FORCE_COLOR: "true" })); + assert.isFalse(supportsColor(tty, { FORCE_COLOR: "0" })); + assert.isFalse(supportsColor(tty, { FORCE_COLOR: "false" })); + }); + + it("honors NO_COLOR and NODE_DISABLE_COLORS", () => { + assert.isFalse(supportsColor(tty, { NO_COLOR: "1" })); + assert.isFalse(supportsColor(tty, { NO_COLOR: "" })); + assert.isFalse(supportsColor(tty, { NODE_DISABLE_COLORS: "1" })); + // FORCE_COLOR takes precedence + assert.isTrue(supportsColor(pipe, { NO_COLOR: "1", FORCE_COLOR: "1" })); + }); + + it("colorizes console output according to FORCE_COLOR", () => { + const cw = new FileWriter("/").writeFile("-"); + vi.stubEnv("FORCE_COLOR", "1"); + assert.equal(cw.colorize("foo", "low"), "\u001b[31;1mfoo\u001b[0m"); + assert.equal(cw.colorize("foo", "unknown"), "foo"); + vi.stubEnv("FORCE_COLOR", "0"); + assert.equal(cw.colorize("foo", "low"), "foo"); + }); +}); diff --git a/packages/istanbul-lib-report/test/report-base.ts b/packages/istanbul-lib-report/test/report-base.test.ts similarity index 100% rename from packages/istanbul-lib-report/test/report-base.ts rename to packages/istanbul-lib-report/test/report-base.test.ts diff --git a/packages/istanbul-lib-report/test/reports/cobertura-regression.ts b/packages/istanbul-lib-report/test/reports/cobertura-regression.test.ts similarity index 100% rename from packages/istanbul-lib-report/test/reports/cobertura-regression.ts rename to packages/istanbul-lib-report/test/reports/cobertura-regression.test.ts diff --git a/packages/istanbul-lib-report/test/reports/cobertura/index.ts b/packages/istanbul-lib-report/test/reports/cobertura/index.test.ts similarity index 69% rename from packages/istanbul-lib-report/test/reports/cobertura/index.ts rename to packages/istanbul-lib-report/test/reports/cobertura/index.test.ts index e8e9bbc..7077033 100644 --- a/packages/istanbul-lib-report/test/reports/cobertura/index.ts +++ b/packages/istanbul-lib-report/test/reports/cobertura/index.test.ts @@ -3,7 +3,7 @@ import { createRequire } from "node:module"; import path from "node:path"; import * as istanbulLibCoverage from "@vitest/istanbul-lib-coverage"; -import { afterAll as after, beforeAll as before, beforeEach, describe, it, should } from "vitest"; +import { afterAll as after, assert, beforeAll as before, beforeEach, describe, it } from "vitest"; import * as istanbulLibReport from "../../../src/index"; import { FileWriter } from "../../../src/index"; @@ -11,7 +11,16 @@ import CoberturaReport from "../../../src/reports/cobertura/index"; const require = createRequire(import.meta.url); -should(); +/** + * `filename` attributes come from `path.relative()`, which uses backslashes + * on Windows. The fixtures were generated on POSIX, so compare with forward + * slashes on every platform. + */ +function normalizeFilenames(xml: string): string { + return xml.replace(/filename="([^"]*)"/g, (_, filename: string) => { + return `filename="${filename.replaceAll("\\", "/")}"`; + }); +} describe("CoberturaReport", () => { before(() => { @@ -26,11 +35,7 @@ describe("CoberturaReport", () => { function createTest(file: string) { const fixture = require(path.resolve(import.meta.dirname, "../fixtures/specs/" + file)); - it(fixture.title, function (this: { skip(): void }) { - if (process.platform === "win32") { - // appveyor does not render console color. - return this.skip(); - } + it(fixture.title, () => { const context = istanbulLibReport.createContext({ dir: "./", coverageMap: istanbulLibCoverage.createCoverageMap(fixture.map), @@ -43,7 +48,7 @@ describe("CoberturaReport", () => { }); tree.visit(report, context); const output = FileWriter.getOutput(); - (output as any).should.equal(fixture.coberturaCoverageData); + assert.equal(normalizeFilenames(output), fixture.coberturaCoverageData); }); } diff --git a/packages/istanbul-lib-report/test/reports/fixtures/specs/100-line-missing-branch.json b/packages/istanbul-lib-report/test/reports/fixtures/specs/100-line-missing-branch.json index c8e1ca5..fb2e60d 100644 --- a/packages/istanbul-lib-report/test/reports/fixtures/specs/100-line-missing-branch.json +++ b/packages/istanbul-lib-report/test/reports/fixtures/specs/100-line-missing-branch.json @@ -85,7 +85,7 @@ } ] }, - "coberturaCoverageData": "\n\n\n \n /Users/benjamincoe/oss/\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", + "coberturaCoverageData": "\n\n\n \n /Users/benjamincoe/oss/\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "map": { "/Users/benjamincoe/oss/test-exclude/index.js": { "path": "/Users/benjamincoe/oss/test-exclude/index.js", diff --git a/packages/istanbul-lib-report/test/reports/fixtures/specs/different-path-files.json b/packages/istanbul-lib-report/test/reports/fixtures/specs/different-path-files.json index 452dd07..3aaaf2e 100644 --- a/packages/istanbul-lib-report/test/reports/fixtures/specs/different-path-files.json +++ b/packages/istanbul-lib-report/test/reports/fixtures/specs/different-path-files.json @@ -20,6 +20,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -27,6 +28,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -34,6 +36,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -41,6 +44,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } @@ -54,6 +58,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -61,6 +66,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -68,6 +74,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -75,6 +82,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } @@ -88,6 +96,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -95,6 +104,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -102,6 +112,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -109,6 +120,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } @@ -123,6 +135,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -130,6 +143,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -137,6 +151,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -144,6 +159,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } @@ -157,6 +173,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -164,6 +181,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -171,6 +189,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -178,6 +197,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } @@ -196,6 +216,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -203,6 +224,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -210,6 +232,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -217,6 +240,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } @@ -230,6 +254,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -237,6 +262,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -244,6 +270,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -251,6 +278,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } @@ -267,6 +295,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -274,6 +303,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -281,6 +311,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" }, @@ -288,6 +319,7 @@ "total": 0, "covered": 0, "skipped": 0, + "missed": 0, "pct": 0, "classForPercent": "empty" } diff --git a/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-decl.json b/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-decl.json index 1874165..0f3f154 100644 --- a/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-decl.json +++ b/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-decl.json @@ -1,5 +1,8 @@ { "title": "100% line coverage, missing branch coverage", + "opts": { + "projectRoot": "/Users/benjamincoe/oss" + }, "textReportExpected": "----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\n\u001b[32;1mAll files\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 95.34\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[33;1m \u001b[0m \n\u001b[32;1m index.js\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 95.34\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[33;1m21,29 \u001b[0m \n----------|---------|----------|---------|---------|-------------------\n", "lcovonlyExpected": "TN:\nSF:\nFN:12,TestExclude\nFN:50,(anonymous_1)\nFN:52,(anonymous_2)\nFN:61,(anonymous_3)\nFN:71,(anonymous_4)\nFN:84,prepGlobPatterns\nFN:85,(anonymous_6)\nFN:100,(anonymous_7)\nFNF:8\nFNH:8\nFNDA:21,TestExclude\nFNDA:21,(anonymous_1)\nFNDA:95,(anonymous_2)\nFNDA:56,(anonymous_3)\nFNDA:7,(anonymous_4)\nFNDA:26,prepGlobPatterns\nFNDA:105,(anonymous_6)\nFNDA:21,(anonymous_7)\nDA:1,1\nDA:2,1\nDA:3,1\nDA:4,1\nDA:5,1\nDA:13,21\nDA:21,21\nDA:22,21\nDA:24,21\nDA:25,7\nDA:28,21\nDA:29,15\nDA:32,21\nDA:33,5\nDA:35,16\nDA:38,21\nDA:39,5\nDA:42,21\nDA:50,1\nDA:51,21\nDA:52,21\nDA:53,95\nDA:55,95\nDA:56,95\nDA:58,21\nDA:61,1\nDA:62,56\nDA:65,56\nDA:67,55\nDA:68,55\nDA:71,1\nDA:72,7\nDA:76,7\nDA:77,6\nDA:78,6\nDA:80,1\nDA:85,26\nDA:87,105\nDA:88,39\nDA:92,105\nDA:93,50\nDA:96,105\nDA:100,1\nDA:101,21\nDA:104,1\nDA:113,1\nLF:46\nLH:46\nBRDA:21,0,0,1\nBRDA:21,0,1,20\nBRDA:21,1,0,1\nBRDA:21,1,1,0\nBRDA:22,2,0,1\nBRDA:22,2,1,20\nBRDA:24,3,0,7\nBRDA:24,3,1,14\nBRDA:24,4,0,21\nBRDA:24,4,1,18\nBRDA:24,4,2,15\nBRDA:28,5,0,15\nBRDA:28,5,1,6\nBRDA:28,6,0,21\nBRDA:28,6,1,8\nBRDA:29,7,0,15\nBRDA:29,7,1,0\nBRDA:32,8,0,5\nBRDA:32,8,1,16\nBRDA:32,9,0,21\nBRDA:32,9,1,5\nBRDA:38,10,0,5\nBRDA:38,10,1,16\nBRDA:38,11,0,21\nBRDA:38,11,1,20\nBRDA:55,12,0,1\nBRDA:55,12,1,94\nBRDA:62,13,0,56\nBRDA:62,13,1,56\nBRDA:65,14,0,1\nBRDA:65,14,1,55\nBRDA:68,15,0,55\nBRDA:68,15,1,15\nBRDA:68,15,2,50\nBRDA:76,16,0,6\nBRDA:76,16,1,1\nBRDA:76,17,0,7\nBRDA:76,17,1,7\nBRDA:76,17,2,6\nBRDA:87,18,0,39\nBRDA:87,18,1,66\nBRDA:92,19,0,50\nBRDA:92,19,1,55\nBRF:43\nBRH:41\nend_of_record\n", "htmlSpaFiles": ["index.js.html", "index.html"], @@ -82,6 +85,7 @@ } ] }, + "coberturaCoverageData": "\n\n\n \n /Users/benjamincoe/oss\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "map": { "/Users/benjamincoe/oss/test-exclude/index.js": { "path": "/Users/benjamincoe/oss/test-exclude/index.js", diff --git a/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-line-missing-branch.json b/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-line-missing-branch.json index ec1840c..499cfe1 100644 --- a/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-line-missing-branch.json +++ b/packages/istanbul-lib-report/test/reports/fixtures/specs/missing-line-missing-branch.json @@ -85,7 +85,7 @@ } ] }, - "coberturaCoverageData": "\n\n\n \n /Users/benjamincoe/oss\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", + "coberturaCoverageData": "\n\n\n \n /Users/benjamincoe/oss\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n", "map": { "/Users/benjamincoe/oss/test-exclude/index.js": { "path": "/Users/benjamincoe/oss/test-exclude/index.js", diff --git a/packages/istanbul-lib-report/test/reports/html-spa/index.ts b/packages/istanbul-lib-report/test/reports/html-spa/index.test.ts similarity index 98% rename from packages/istanbul-lib-report/test/reports/html-spa/index.ts rename to packages/istanbul-lib-report/test/reports/html-spa/index.test.ts index c733ab8..4c8be0c 100644 --- a/packages/istanbul-lib-report/test/reports/html-spa/index.ts +++ b/packages/istanbul-lib-report/test/reports/html-spa/index.test.ts @@ -99,10 +99,10 @@ describe("html-spa", () => { header: "/* eslint-disable */\n", }, { + type: "copy", + source: path.join(import.meta.dirname, "../../../src/reports/html/assets/favicon.png"), dest: "./favicon.png", header: undefined, - source: path.join(import.meta.dirname, "/../../src/html/assets/favicon.png"), - type: "copy", }, { type: "copy", diff --git a/packages/istanbul-lib-report/test/reports/html-spa/src/getChildData.ts b/packages/istanbul-lib-report/test/reports/html-spa/src/getChildData.test.ts similarity index 100% rename from packages/istanbul-lib-report/test/reports/html-spa/src/getChildData.ts rename to packages/istanbul-lib-report/test/reports/html-spa/src/getChildData.test.ts diff --git a/packages/istanbul-lib-report/test/reports/html/annotator.ts b/packages/istanbul-lib-report/test/reports/html/annotator.test.ts similarity index 100% rename from packages/istanbul-lib-report/test/reports/html/annotator.ts rename to packages/istanbul-lib-report/test/reports/html/annotator.test.ts diff --git a/packages/istanbul-lib-report/test/reports/lcovonly/index.ts b/packages/istanbul-lib-report/test/reports/lcovonly/index.test.ts similarity index 75% rename from packages/istanbul-lib-report/test/reports/lcovonly/index.ts rename to packages/istanbul-lib-report/test/reports/lcovonly/index.test.ts index f64498b..a7ecb1b 100644 --- a/packages/istanbul-lib-report/test/reports/lcovonly/index.ts +++ b/packages/istanbul-lib-report/test/reports/lcovonly/index.test.ts @@ -3,7 +3,7 @@ import { createRequire } from "node:module"; import path from "node:path"; import * as istanbulLibCoverage from "@vitest/istanbul-lib-coverage"; -import { afterAll as after, beforeAll as before, beforeEach, describe, it, should } from "vitest"; +import { afterAll as after, assert, beforeAll as before, beforeEach, describe, it } from "vitest"; import * as istanbulLibReport from "../../../src/index"; import { FileWriter } from "../../../src/index"; @@ -11,8 +11,6 @@ import LcovOnlyReport from "../../../src/reports/lcovonly/index"; const require = createRequire(import.meta.url); -should(); - describe("LcovOnlyReport", () => { before(() => { FileWriter.startCapture(); @@ -26,21 +24,17 @@ describe("LcovOnlyReport", () => { function createTest(file: string) { const fixture = require(path.resolve(import.meta.dirname, "../fixtures/specs/" + file)); - it(fixture.title, function (this: { skip(): void }) { - if (process.platform === "win32") { - // appveyor does not render console color. - return this.skip(); - } + it(fixture.title, () => { const context = istanbulLibReport.createContext({ dir: "./", coverageMap: istanbulLibCoverage.createCoverageMap(fixture.map), }); const tree = context.getTree("pkg"); - const report = new LcovOnlyReport(fixture.opts); + const report = new LcovOnlyReport({ file: "-", ...fixture.opts }); tree.visit(report, context); const output = FileWriter.getOutput().replace(/SF:.*/, "SF:"); if (fixture.lcovonlyExpected) { - (output as any).should.equal(fixture.lcovonlyExpected); + assert.equal(output, fixture.lcovonlyExpected); } }); } diff --git a/packages/istanbul-lib-report/test/reports/text/index.ts b/packages/istanbul-lib-report/test/reports/text/index.test.ts similarity index 75% rename from packages/istanbul-lib-report/test/reports/text/index.ts rename to packages/istanbul-lib-report/test/reports/text/index.test.ts index b51b209..8f3cb81 100644 --- a/packages/istanbul-lib-report/test/reports/text/index.ts +++ b/packages/istanbul-lib-report/test/reports/text/index.test.ts @@ -3,7 +3,15 @@ import { createRequire } from "node:module"; import path from "node:path"; import * as istanbulLibCoverage from "@vitest/istanbul-lib-coverage"; -import { afterAll as after, beforeAll as before, beforeEach, describe, it, should } from "vitest"; +import { + afterAll as after, + assert, + beforeAll as before, + beforeEach, + describe, + it, + vi, +} from "vitest"; import * as istanbulLibReport from "../../../src/index"; import { FileWriter } from "../../../src/index"; @@ -11,14 +19,16 @@ import TextReport from "../../../src/reports/text/index"; const require = createRequire(import.meta.url); -should(); - describe("TextReport", () => { before(() => { + // the fixtures contain ANSI colour codes, so force colours regardless of + // whether the test runner's stdout is a TTY + vi.stubEnv("FORCE_COLOR", "1"); FileWriter.startCapture(); }); after(() => { FileWriter.stopCapture(); + vi.unstubAllEnvs(); }); beforeEach(() => { FileWriter.resetOutput(); @@ -26,11 +36,7 @@ describe("TextReport", () => { function createTest(file: string) { const fixture = require(path.resolve(import.meta.dirname, "../fixtures/specs/" + file)); - it(fixture.title, function (this: { skip(): void }) { - if (process.platform === "win32") { - // appveyor does not render console color. - return this.skip(); - } + it(fixture.title, () => { const context = istanbulLibReport.createContext({ dir: "./", coverageMap: istanbulLibCoverage.createCoverageMap(fixture.map), @@ -39,7 +45,7 @@ describe("TextReport", () => { const report = new TextReport(fixture.opts); tree.visit(report, context); const output = FileWriter.getOutput(); - (output as any).should.equal(fixture.textReportExpected); + assert.equal(output, fixture.textReportExpected); }); }