Skip to content
Open
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
12 changes: 9 additions & 3 deletions packages/ember-cli-code-coverage/addon-test-support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ export function forceModulesToBeLoaded(filterFunction) {
});
}

export async function sendCoverage(callback) {
export async function sendCoverage(callback, options) {
let coverageData = window.__coverage__; //eslint-disable-line no-undef
let { coverageApiPath, isHtmlReportNotGenerated } = options; // if isHtmlReportNotGenerated is null/undefined, writeCoverageInfo will be called; if it is explicitly passed as `true`, writeCoverageInfo will not be called
if (!coverageApiPath) {
coverageApiPath = '/write-coverage';
}

if (coverageData === undefined) {
if (callback) {
Expand All @@ -77,15 +81,17 @@ export async function sendCoverage(callback) {

let body = JSON.stringify(coverageData || {});

let response = await fetch('/write-coverage', {
let response = await fetch(coverageApiPath, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body,
});
let responseData = await response.json();
writeCoverageInfo(responseData);
if (!isHtmlReportNotGenerated) {
writeCoverageInfo(responseData);
}

if (callback) {
callback();
Expand Down
20 changes: 16 additions & 4 deletions packages/ember-cli-code-coverage/lib/attach-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ const path = require('path');
const crypto = require('crypto');
const fs = require('fs-extra');

const WRITE_COVERAGE = '/write-coverage';

function logError(err, req, res, next) {
console.error(err.stack);
next(err);
}

/**
* This function fetches the API path to write coverage
* This can be passed from the client application using the key `coverageApiPath` in the file config/coverage.js
* Default value will be set to `/write-coverage`
*/

function getCoverageApiPath(configPath) {
let config = getConfig(configPath);
return config.coverageApiPath;
}

/*
* This function normalizes the relativePath to match what we get from a classical app. Its goal
* is to change any in repo paths like: app-namespace/lib/in-repo-namespace/components/foo.js to
Expand Down Expand Up @@ -183,8 +192,10 @@ function coverageHandler(map, options, req, res) {
// Used when app is in dev mode (`ember serve`).
// Creates a new coverage map on every request.
function serverMiddleware(app, options) {
let coverageApiPath = getCoverageApiPath(options.configPath);

app.post(
WRITE_COVERAGE,
coverageApiPath,
bodyParser,
(req, res) => {
let map = libCoverage.createCoverageMap();
Expand All @@ -199,9 +210,10 @@ function serverMiddleware(app, options) {
// Collects the coverage on each request and merges it into the coverage map.
function testMiddleware(app, options) {
let map = libCoverage.createCoverageMap();
let coverageApiPath = getCoverageApiPath(options.configPath);

app.post(
WRITE_COVERAGE,
coverageApiPath,
bodyParser,
(req, res) => {
coverageHandler(map, options, req, res);
Expand Down
1 change: 1 addition & 0 deletions packages/ember-cli-code-coverage/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function getDefaultConfig() {
coverageFolder: 'coverage',
excludes: ['*/mirage/**/*'],
reporters: ['html', 'lcov'],
coverageApiPath: '/write-coverage',
};
}

Expand Down