Skip to content

Commit

Permalink
Report tests separately even if their descriptions match (#31)
Browse files Browse the repository at this point in the history
Fixes issue #30

Individual changes:

* Add tests for issue #30

* Add test configurations with mocha/chai and jasmine to `examples/`

* Use `result.id`, when available, and a work around otherwise

* Add comment
  • Loading branch information
sth authored Jan 7, 2024
1 parent e69f962 commit 5e4efdc
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 12 deletions.
11 changes: 11 additions & 0 deletions examples/issue-#30/jasmine/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [ 'test.js' ],
reporters: ['progress', 'summary'],
summaryReporter: {
show: 'all',
},
})
}
4 changes: 4 additions & 0 deletions examples/issue-#30/jasmine/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
describe('truthy-tests', () => {
it('should be truthy', () => { expect(true).toBeTruthy(); });
it('should be truthy', () => { expect(1).toBeTruthy(); });
});
11 changes: 11 additions & 0 deletions examples/issue-#30/mocha-chai/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

module.exports = function(config) {
config.set({
frameworks: ['mocha', 'chai'],
files: [ 'test.js' ],
reporters: ['progress', 'summary'],
summaryReporter: {
show: 'all',
},
})
}
6 changes: 6 additions & 0 deletions examples/issue-#30/mocha-chai/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Mocha/Chai doesn't supply `result.id`

describe('truthy-tests', () => {
it('should be truthy', () => { assert.equal(true, true); });
it('should be truthy', () => { assert.equal(1, true); });
});
8 changes: 6 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
"description": "Examples for karma-summary-reporter.",
"scripts": {
"bugs": "karma start bugs/karma.conf.js",
"features": "karma start features/karma.conf.js"
"features": "karma start features/karma.conf.js",
"issue-#30/mocha-chai": "karma start issue-#30/mocha-chai/karma.conf.js",
"issue-#30/jasmine": "karma start issue-#30/jasmine/karma.conf.js"
},
"author": "Stephan Hohe <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/sth/karma-summary-reporter#readme",
"dependencies": {
"chai": "^4.1.2",
"jasmine": "^5.1.0",
"karma": "^6.3.14",
"karma-chai": "^0.1.0",
"karma-jasmine": "^5.1.0",
"karma-mocha": "^2.0.0",
"karma-summary-reporter": "file:..",
"karma-summary-reporter": "link:..",
"mocha": "^9.0.0"
}
}
193 changes: 185 additions & 8 deletions examples/yarn.lock

Large diffs are not rendered by default.

26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,27 @@ var SummaryReporter = function(baseReporterDecorator, config) {
return;
}

var specid = result.suite.join('/') + '/' + result.description;
var specid;
if (result.id) {
// For example jasmine sets `result.id`
specid = result.id;
}
else {
// mocha/chai doesn't set `result.id`, so we need to improvise to
// distinguish test cases.
var specid_base = result.suite.join('/') + '/' + result.description;
specid = specid_base;
// Check if we already saw the spec id (which would mean there is
// more than one test with the same description)
// We cannot guarantee that we see specs in the same order for multiple
// browsers, so results might get mixed up for "duplicate" testcases,
// but it's the best we can do if there isn't a `result.id`.
var num = 0;
while (specid in specresults && browser.id in specresults[specid].results) {
num++;
specid = specid_base + "~" + num;
}
}
if (!(specid in specresults)) {
specorder.push(specid);
specresults[specid] = {
Expand All @@ -62,7 +82,9 @@ var SummaryReporter = function(baseReporterDecorator, config) {
if (i < currentPath.length && s != currentPath[i]) {
currentPath.length = i;
}
if (i >= currentPath.length) {
// Print the parts after the common prefix.
// If we don't have anything more than the common prefix, print the last path element.
if (i >= currentPath.length || i == path.length - 1) {
var label = indent + s;
if (label.length > specLength) {
label = label.slice(0, specLength-3) + '...';
Expand Down
30 changes: 30 additions & 0 deletions test/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,34 @@ describe('Summary reporter', function () {
'formats the header correctly');
});
});

describe("duplicate test names", function() {
beforeEach(function() {
setupReporter({
summaryReporter: {
show: "all"
}
});
});

it('should show all tests with different result.id', function() {
const result = resultSuccess(1);
reporter.onRunStart([b1])
reporter.onSpecComplete(b1, {id: "specid1", ...result});
reporter.onSpecComplete(b1, {id: "specid2", ...result});
reporter.onRunComplete([b1])
chai.assert.equal(writeOutput.match(/test1/g).length, 2,
"shows test1 label two times");
});

it('should show all tests if result.id is missing', function() {
const result = resultSuccess(1);
reporter.onRunStart([b1])
reporter.onSpecComplete(b1, result);
reporter.onSpecComplete(b1, result);
reporter.onRunComplete([b1])
chai.assert.equal(writeOutput.match(/test1/g).length, 2,
"shows test1 label two times");
});
});
});

0 comments on commit 5e4efdc

Please sign in to comment.