Skip to content

Commit

Permalink
added custom metadata support (#2242) (#2298)
Browse files Browse the repository at this point in the history
* Add suport for custom metadata in fixtures and tests

* Fix remarks
  • Loading branch information
MatthewNielsen27 authored and miherlosev committed Apr 24, 2018
1 parent fa9e412 commit d4be3b1
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/api/structure/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { flattenDeep as flatten } from 'lodash';

export default class Fixture extends TestingUnit {
constructor (testFile) {
super(testFile);
super(testFile, 'fixture');

this.path = testFile.filename;

Expand Down
2 changes: 1 addition & 1 deletion src/api/structure/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { flattenDeep as flatten, union } from 'lodash';

export default class Test extends TestingUnit {
constructor (testFile) {
super(testFile);
super(testFile, 'test');

this.fixture = testFile.currentFixture;

Expand Down
18 changes: 16 additions & 2 deletions src/api/structure/testing-unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { assertType, is } from '../../errors/runtime/type-assertions';


export default class TestingUnit {
constructor (testFile) {
this.testFile = testFile;
constructor (testFile, unitTypeName) {
this.testFile = testFile;
this.unitTypeName = unitTypeName;

this.name = null;
this.pageUrl = null;
this.authCredentials = null;
this.meta = {};
this.only = false;
this.skip = false;

Expand Down Expand Up @@ -66,6 +68,18 @@ export default class TestingUnit {
return this.apiOrigin;
}

_meta$ (...args) {
assertType([is.string, is.nonNullObject], 'meta', `${this.unitTypeName}.meta`, args[0]);

const data = typeof args[0] === 'string' ? { [args[0]]: args[1] } : args[0];

Object.keys(data).forEach(key => {
this.meta[key] = data[key];
});

return this.apiOrigin;
}

static _makeAPIListForChildClass (ChildClass) {
ChildClass.API_LIST = TestingUnit.API_LIST.concat(getDelegatedAPIList(ChildClass.prototype));
}
Expand Down
1 change: 0 additions & 1 deletion src/compiler/test-file/formats/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Fixture from '../../../api/structure/fixture';
import Test from '../../../api/structure/test';
import createCommandFromObject from '../../../test-run/commands/from-object';


export default class RawTestFileCompiler extends TestFileCompilerBase {
static _createTestFn (commands) {
return async t => {
Expand Down
6 changes: 3 additions & 3 deletions src/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export default class Reporter {
reportItem = this.reportQueue.shift();
currentFixture = reportItem.fixture;

this.plugin.reportTestDone(reportItem.test.name, reportItem.testRunInfo);
this.plugin.reportTestDone(reportItem.test.name, reportItem.testRunInfo, reportItem.test.meta);

// NOTE: here we assume that tests are sorted by fixture.
// Therefore, if the next report item has a different
// fixture, we can report this fixture start.
nextReportItem = this.reportQueue[0];

if (nextReportItem && nextReportItem.fixture !== currentFixture)
this.plugin.reportFixtureStart(nextReportItem.fixture.name, nextReportItem.fixture.path);
this.plugin.reportFixtureStart(nextReportItem.fixture.name, nextReportItem.fixture.path, nextReportItem.fixture.meta);
}
}

Expand All @@ -74,7 +74,7 @@ export default class Reporter {
var first = this.reportQueue[0];

this.plugin.reportTaskStart(startTime, userAgents, this.testCount);
this.plugin.reportFixtureStart(first.fixture.name, first.fixture.path);
this.plugin.reportFixtureStart(first.fixture.name, first.fixture.path, first.fixture.meta);
});

task.on('test-run-start', testRun => {
Expand Down
73 changes: 73 additions & 0 deletions test/server/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,43 @@ describe('API', function () {
' 4 |test(\'test\', async t => {\n' +
' 5 |});\n' +
' 6 |'

});
});
});

it('Should collect meta data', function () {
return compile('test/server/data/test-suites/meta/testfile.js')
.then(function (compiled) {
expect(compiled.tests[0].fixture.meta.metaField1).eql('fixtureMetaValue1');
expect(compiled.tests[0].fixture.meta.metaField2).eql('fixtureMetaUpdatedValue2');
expect(compiled.tests[0].fixture.meta.metaField3).eql('fixtureMetaValue3');
expect(compiled.tests[1].fixture.meta.emptyField).eql(void 0);
});
});

it('Should raise an error if fixture.meta is undefined', function () {
var file = resolve('test/server/data/test-suites/meta/incorrect-fixture-meta.js');

return compile(file)
.then(function () {
throw new Error('Promise rejection expected');
})
.catch(function (err) {
assertAPIError(err, {
stackTop: file,

message: 'Cannot prepare tests due to an error.\n\n' +
'fixture.meta is expected to be a string or a non-null object, but it was undefined.',

callsite: ' 1 |fixture(\'Fixture1\')\n' +
' 2 | .page(\'http://example.com\')\n' +
' > 3 | .meta();\n' +
' 4 |\n' +
' 5 |test\n' +
' 6 | (\'Fixture1Test1\', async () => {\n' +
' 7 | // do nothing\n' +
' 8 | });'
});
});
});
Expand Down Expand Up @@ -409,6 +446,42 @@ describe('API', function () {
expect(test.requestHooks.length).eql(3);
});
});

it('Should collect meta data', function () {
return compile('test/server/data/test-suites/meta/testfile.js')
.then(function (compiled) {
expect(compiled.tests[0].meta.metaField1).eql('testMetaValue1');
expect(compiled.tests[0].meta.metaField4).eql('testMetaUpdatedValue4');
expect(compiled.tests[0].meta.metaField5).eql('testMetaValue5');
expect(compiled.tests[1].meta.emptyField).eql(void 0);
});
});

it('Should raise an error if test.meta is null', function () {
var file = resolve('test/server/data/test-suites/meta/incorrect-test-meta.js');

return compile(file)
.then(function () {
throw new Error('Promise rejection expected');
})
.catch(function (err) {
assertAPIError(err, {
stackTop: file,

message: 'Cannot prepare tests due to an error.\n\n' +
'test.meta is expected to be a string or a non-null object, but it was null.',

callsite: ' 1 |fixture(\'Fixture1\')\n' +
' 2 | .page(\'http://example.com\');\n' +
' 3 |\n' +
' 4 |test\n' +
' > 5 | .meta(null)\n' +
' 6 | (\'Fixture1Test1\', async () => {\n' +
' 7 | // do nothing\n' +
' 8 | });'
});
});
});
});

describe('Selector', function () {
Expand Down
1 change: 1 addition & 0 deletions test/server/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Compiler', function () {
expect(compiled.tests.length).eql(2);

expect(compiled.tests[0].name).eql('1.Test');
expect(compiled.tests[0].meta).eql({ run: 'run-001' });
expect(compiled.tests[0].isLegacy).to.be.undefined;

expect(compiled.tests[1].name).eql('2.LegacyTest');
Expand Down
8 changes: 8 additions & 0 deletions test/server/data/test-suites/meta/incorrect-fixture-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fixture('Fixture1')
.page('http://example.com')
.meta();

test
('Fixture1Test1', async () => {
// do nothing
});
8 changes: 8 additions & 0 deletions test/server/data/test-suites/meta/incorrect-test-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fixture('Fixture1')
.page('http://example.com');

test
.meta(null)
('Fixture1Test1', async () => {
// do nothing
});
20 changes: 20 additions & 0 deletions test/server/data/test-suites/meta/testfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fixture('Fixture1')
.meta('metaField1', 'fixtureMetaValue1')
.page('http://example.com')
.meta({ metaField2: 'fixtureMetaValue2', metaField3: 'fixtureMetaValue3' })
.meta('metaField2', 'fixtureMetaUpdatedValue2');

test
.meta('metaField1', 'testMetaValue1')
('Fixture1Test1', async () => {
// do nothing
})
.meta({ metaField4: 'testMetaValue4', metaField5: 'testMetaValue5' })
.meta('metaField4', 'testMetaUpdatedValue4');

fixture('Fixture2')
.meta('emptyField');

test('Fixture2Test1', async () => {
// do nothing
}).meta('emptyField');
6 changes: 4 additions & 2 deletions test/server/data/test-suites/mixed-content/testfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
fixture `Fixture`;

test('1.Test', () => {
test
.meta({ run: 'run-001' })
('1.Test', () => {

});
});
Loading

0 comments on commit d4be3b1

Please sign in to comment.