Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit 0311f6d

Browse files
committed
feat: Add test filename to html report's metaInfo section
1 parent fefd44f commit 0311f6d

File tree

7 files changed

+183
-100
lines changed

7 files changed

+183
-100
lines changed

lib/reporters/html/view-model.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ module.exports = class ViewModel {
123123
const suite = result.suite;
124124
const metaInfo = suite ? suite.metaInfo : {};
125125
metaInfo.sessionId = result.sessionId || 'unknown session id';
126+
metaInfo.file = suite.file;
126127

127128
const testResult = _.assign({
128129
suiteUrl: rootUrl + suite.url,

lib/suite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = class Suite {
2424
this.afterActions = [];
2525
this.browsers = [];
2626
this.context = {};
27+
this.file = null;
2728
definePrivate(this);
2829
}
2930

lib/test-reader.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const _ = require('lodash');
4+
const path = require('path');
45
const SetsBuilder = require('gemini-core').SetsBuilder;
56
const Suite = require('./suite');
67
const Events = require('./constants/events');
@@ -9,15 +10,17 @@ const utils = require('./utils');
910

1011
const DEFAULT_DIR = require('../package').name;
1112

12-
const loadSuites = (sets, emitter) => {
13+
const loadSuites = (sets, emitter, projectRoot) => {
1314
const rootSuite = Suite.create('');
1415

15-
_.forEach(sets.groupByFile(), (browsers, path) => {
16-
global.gemini = testsApi(rootSuite, browsers);
16+
_.forEach(sets.groupByFile(), (browsers, filePath) => {
17+
const relativeFilePath = path.relative(projectRoot, filePath);
1718

18-
emitter.emit(Events.BEFORE_FILE_READ, path);
19-
utils.requireWithNoCache(path);
20-
emitter.emit(Events.AFTER_FILE_READ, path);
19+
global.gemini = testsApi(rootSuite, browsers, relativeFilePath);
20+
21+
emitter.emit(Events.BEFORE_FILE_READ, filePath);
22+
utils.requireWithNoCache(filePath);
23+
emitter.emit(Events.AFTER_FILE_READ, filePath);
2124

2225
delete global.gemini;
2326
});
@@ -32,5 +35,5 @@ module.exports = (emitter, config, opts) => {
3235
.useFiles(opts.paths)
3336
.useBrowsers(opts.browsers)
3437
.build(config.system.projectRoot, {ignore: config.system.exclude})
35-
.then((setCollection) => loadSuites(setCollection, emitter));
38+
.then((setCollection) => loadSuites(setCollection, emitter, config.system.projectRoot));
3639
};

lib/tests-api/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Suite = require('../suite');
44
const SuiteBuilder = require('./suite-builder');
55
const keysCodes = require('./keys-codes');
66

7-
module.exports = (suite, browsers) => {
7+
module.exports = (suite, browsers, file) => {
88
let suiteId = 1;
99
const testsAPI = keysCodes;
1010

@@ -25,9 +25,17 @@ module.exports = (suite, browsers) => {
2525
suite = Suite.create(name, parent);
2626
parent.addChild(suite);
2727
suite.id = suiteId++;
28-
if (browsers && suite.parent.isRoot) {
29-
suite.browsers = browsers;
28+
29+
if (suite.parent.isRoot) {
30+
if (browsers) {
31+
suite.browsers = browsers;
32+
}
33+
34+
if (file) {
35+
suite.file = file;
36+
}
3037
}
38+
3139
callback(new SuiteBuilder(suite));
3240
if (suite.hasStates) {
3341
if (!suite.url) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
5+
const ViewModel = require('lib/reporters/html/view-model');
6+
7+
describe('ViewModel', () => {
8+
const sandbox = sinon.sandbox.create();
9+
const stubTest_ = (opts) => {
10+
opts = opts || {};
11+
12+
return _.defaultsDeep(opts, {
13+
state: {name: 'name-default'},
14+
suite: {
15+
path: ['suite'],
16+
metaInfo: {sessionId: 'sessionId-default'},
17+
file: 'default/path/file.js'
18+
}
19+
});
20+
};
21+
const getResult_ = (model) => model.getResult().suites[0].children[0].browsers[0].result;
22+
const createViewModel_ = () =>{
23+
const config = {forBrowser: sandbox.stub().returns({})};
24+
return new ViewModel(config);
25+
};
26+
27+
it('should contain "file" in "metaInfo"', () => {
28+
const model = createViewModel_();
29+
30+
model.addSuccess(stubTest_({
31+
suite: {file: '/path/file.js'}
32+
}));
33+
34+
const metaInfo = JSON.parse(getResult_(model).metaInfo);
35+
36+
assert.equal(metaInfo.file, '/path/file.js');
37+
});
38+
});

test/unit/test-reader.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ describe('test-reader', () => {
135135
);
136136
});
137137
});
138+
139+
it('should call "testsApi" with relative file path', () => {
140+
const groupByFile = () => ({'/project/root/path/file1.js': []});
141+
142+
const config = mkConfigStub({
143+
system: {projectRoot: '/project/root'}
144+
});
145+
146+
SetsBuilder.prototype.build.returns(Promise.resolve({groupByFile}));
147+
148+
return readTests_({config})
149+
.then(() => assert.calledWith(testsApi, sinon.match.any, sinon.match.any, 'path/file1.js'));
150+
});
138151
});
139152

140153
describe('global "gemini" variable', () => {

0 commit comments

Comments
 (0)