Skip to content
Draft
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
7 changes: 4 additions & 3 deletions package-lock.json

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

21 changes: 19 additions & 2 deletions packages/middleware-code-coverage/lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import path from "node:path";
import serveStatic from "serve-static";
import {promisify} from "node:util";

import {
AnyMap,
} from "@jridgewell/trace-mapping";

/**
* Custom middleware to instrument JS files with Istanbul.
*
Expand Down Expand Up @@ -46,7 +50,10 @@ export default async function({log, middlewareUtil, options={}, resources}) {

// Instrumenter instance
const instrumenter = createInstrumenter(instrumenterConfig);
const instrument = promisify(instrumenter.instrument.bind(instrumenter));
// Switch callback parameters to match promisify signature
const callbackStyleInstrumenter = (code, filename, inputSourceMap, callback) =>
instrumenter.instrument(code, filename, callback, inputSourceMap);
const instrument = promisify(callbackStyleInstrumenter);

const router = new Router();

Expand Down Expand Up @@ -134,8 +141,18 @@ export default async function({log, middlewareUtil, options={}, resources}) {
return;
}

// TODO: Add actual source map lookup (like in UI5 builder)
const inputSourceMapResource = await resources.all.byPath(`${pathname}.map`);
let inputSourceMap = inputSourceMapResource ?
JSON.parse(await inputSourceMapResource.getString()) :
undefined;

if (inputSourceMap) {
inputSourceMap = new AnyMap(inputSourceMap);
}

const source = await matchedResource.getString();
let instrumentedSource = await instrument(source, pathname);
let instrumentedSource = await instrument(source, pathname, inputSourceMap);

log.verbose(`...${pathname} instrumented!`);

Expand Down
1 change: 1 addition & 0 deletions packages/middleware-code-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"node": "^20.11.0 || >=22.0.0"
},
"dependencies": {
"@jridgewell/trace-mapping": "^0.3.31",
"body-parser": "^2.2.2",
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-instrument": "^6.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const resources = {
byGlob() {
return [];
},
async byPath() {
async byPath(filePath) {
if (!filePath.endsWith(".js")) {
return null;
}
return {
async getString() {
return sampleJS;
Expand Down
Loading