Skip to content

fix: work around xml2js null object prototypes #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import columnify from 'columnify';
import { red, green, bold } from 'colorette';
import { EOL } from 'os';

const toString = (v) => Object.prototype.toString.call(v);

export const generateSummary = (summary) => {
const data = {
'> Name:': summary.name,
Expand Down Expand Up @@ -59,7 +61,7 @@ const generateTestcaseResult = (testcase) => {
if (!isTestcaseSuccess(testcase)) {
let errorLines = '';
if (testcase.failure.join) {
errorLines = testcase.failure.join(EOL).split(EOL).join(EOL + '\t');
errorLines = testcase.failure.map(toString).join(EOL).split(EOL).join(EOL + '\t');
} else {
errorLines = testcase.failure;
}
Expand Down
44 changes: 44 additions & 0 deletions lib.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
import assert from 'assert';

import * as lib from './lib.js';
import parseString from 'xml2js';

const xmlStr = `
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite name="someName" time="0.004" errors="0" tests="2" skipped="0" failures="1">
<testcase classname="className" name="someName" time="0.002">
<failure type="toBe" message="Expected 1 to be 2.">
</failure>
</testcase>
<testcase classname="clasName2" name="someName2" time="0">
</testcase>
</testsuite>
</testsuites>
`;

describe('lib', () => {

describe('from parsed XML', () => {
let result, err;
beforeEach((done) => {
parseString.parseString(xmlStr, (outErr, outResult) => {
result = outResult;
err = outErr;
done();
});
});
it('should parse', () => {
assert.equal(err, null);
});
it('should not throw with parsed XML', () => {
if (!result.testsuites.$) {
result.testsuites.$ = lib.findSummaryFromTestsuites(result.testsuites.testsuite);
} else {
result.testsuites.$ = {
...result.testsuites.$,
...lib.findSummaryFromTestsuites(result.testsuites.testsuite),
};
}
assert.doesNotThrow(() => lib.generateSummary(result.testsuites.$));
result.testsuites.testsuite.forEach(t => {
assert.doesNotThrow(() => lib.generateTestsuiteSummary(t));
assert.doesNotThrow(() => lib.generateTestsuiteResult(t));
});
});
});
describe('generateSummary()', () => {
const summary = {
name: 'test',
Expand Down