import defaultBuilder, { type Builder, TestCase, TestSuite } from '../dist';
//@ts-ignore
import rmdir from 'rimraf';
import fs from 'fs';
describe('JUnit Report builder', () => {
let builder: Builder;
beforeEach(() => (builder = defaultBuilder.newBuilder()));
beforeAll((done) =>
rmdir('build/tmp/test_resources', (error: any) => {
if (error) {
throw new Error(error);
}
done();
}),
);
const reportWith = (content: string) => '\n' + content;
it('should produce a report identical to the expected one', () => {
builder.testCase().className('root.test.Class1');
const suite1: TestSuite = builder.testSuite().name('first.Suite');
suite1.testCase().name('Second test');
const case2: TestCase = suite1.testCase();
case2
.className('suite1.test.Class2')
.name('Third test')
.file('./path-to/the-test-file.coffee')
.property('property name', 'property value')
.multilineProperty('property name 2', 'property value 2');
const suite2 = builder.testSuite().name('second.Suite');
suite2.testCase().failure('Failure message');
suite2.testCase().stacktrace('Stacktrace');
suite2.testCase().skipped();
builder.writeTo('build/tmp/test_resources/actual_report.xml');
const actual = fs.readFileSync('build/tmp/test_resources/actual_report.xml').toString().trim();
const expected = fs.readFileSync('spec/expected_report.xml').toString().trim();
expect(actual).toBe(expected);
});
it('should produce an empty list of test suites when nothing reported', () => {
expect(builder.build()).toBe(
// prettier-ignore
'\n' +
'',
);
});
it('should set testsuites name', () => {
builder.name('testSuitesName');
expect(builder.build()).toBe(
// prettier-ignore
'\n' +
'',
);
});
it('should produce an empty test suite when a test suite reported', () => {
builder.testSuite();
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
'',
),
);
});
it('should produce a root test case when reported', () => {
builder.testCase();
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
'',
),
);
});
it('should produce a root test case with failure when reported', () => {
builder.testCase().failure('it failed');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should produce a root test case with failure and type when reported', () => {
builder.testCase().failure('it failed', 'the type');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should produce a root test case with error when reported', () => {
builder.testCase().error('it errored');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should produce a root test case with error, type and content when reported', () => {
builder.testCase().error('it errored', 'the type', 'the content');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should produce a test suite with a test case when reported', () => {
builder.testSuite().testCase();
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should produce a test suite with a failed test case when reported', () => {
builder.testSuite().testCase().failure();
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should produce a test suite with an errored test case when reported', () => {
builder.testSuite().testCase().error();
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should produce a test suite with a skipped test case when reported', () => {
builder.testSuite().testCase().skipped();
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should add the reported time to the test sute', () => {
builder.testSuite().time(2.5);
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
'',
),
);
});
it('should add the reported timestamp to the test sute', () => {
builder.testSuite().timestamp(new Date(2015, 10, 22, 13, 37, 59, 123));
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
'',
),
);
});
it('should add the reported time to the test case', () => {
builder.testSuite().testCase().time(2.5);
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should print the reported standard output log to system-out', () => {
builder.testSuite().testCase().standardOutput('This was written to stdout!');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should print the reported standard error log to system-err', () => {
builder.testSuite().testCase().standardError('This was written to stderr!');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should print the reported attachment to system-err', () => {
builder
.testSuite()
.testCase()
.standardError('This was written to stderr!')
.errorAttachment('absolute/path/to/attachment');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
' \n' +
' [[ATTACHMENT|absolute/path/to/attachment]]\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should output test suites and test cases in the order reported', () => {
builder.testCase().name('1');
builder.testSuite().name('2');
builder.testCase().name('3');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should builder supports emojis in cdata tags', () => {
builder.testCase().standardOutput('Emoji: 🤦');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should escape quotes', () => {
builder.testCase().error('it is "quoted"');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
it('should remove invalid characters', () => {
builder.testCase().error('Invalid\x00Characters\x08Stripped');
expect(builder.build()).toBe(
reportWith(
// prettier-ignore
'\n' +
' \n' +
' \n' +
' \n' +
'',
),
);
});
});