UNPKG

4.5 kBJavaScriptView Raw
1"use strict";
2
3const path = require('path');
4const Promise = require('bluebird');
5
6const _ = require('lodash');
7
8const reporter = require("../reports/reporter");
9const logger = require('../commons/logger').getLogger('suite-runner');
10const {ArgError} = require('../errors');
11const perf = require('../commons/performance-logger');
12
13const BaseTestPlanRunner = require('./BaseTestPlanRunner');
14
15class AnonymousTestPlanRunner extends BaseTestPlanRunner {
16 runAnonymousTestPlan(options, branchToUse) {
17 logger.info("start to run anonymous", {
18 options: Object.assign({}, options, {token: undefined}),
19 branchToUse
20 });
21
22 return this.prepareForTestResources(options)
23 .log('after prepareForTestResoutces')
24 .then(() => getSuite(options, branchToUse))
25 .log('after getSuite')
26 .then((suiteResult) => {
27 if (!suiteResult.tests[0] || suiteResult.tests[0].length === 0) {
28 if (options.rerunFailedByRunId) {
29 throw new ArgError("No failed tests found in the provided run");
30 }
31 throw new ArgError("No tests to run");
32 }
33 branchToUse = suiteResult.branch || branchToUse;
34 if (options.rerunFailedByRunId && !suiteResult.runName) {
35 if (!suiteResult.runExists) {
36 throw new ArgError("Invalid run ID - no such run.");
37 }
38 const isAnonymouslyNamedRun = suiteResult.runName === '';
39 if (isAnonymouslyNamedRun) {
40 suiteResult.runName = 'rerun-' + options.rerunFailedByRunId;
41 }
42 }
43 const testPlanName = options.overrideExecutionName || suiteResult.runName || _.concat(options.label, options.name, options.suites).join(" ");
44 const isAnonymous = true;
45 perf.log('Right before validateConfig + runTestPlan Promise.map');
46 return Promise.map(suiteResult.tests, suiteTests => {
47 //override result id for remote run mode and run only the first test data
48 if(options.resultId) {
49 const firstTest = _.first(suiteTests);
50 firstTest.resultId = options.resultId;
51 suiteTests = [firstTest];
52 }
53 return this.validateConfig(options, suiteTests)
54 .log('right before runTestPlan')
55 .then(() => this.runTestPlan([], suiteTests, [], options, testPlanName, branchToUse, isAnonymous))
56 .log('right after runTestPlan')
57 .tap(async res => {
58 const isCodeMode = options.files.length > 0;
59 await reporter.onTestPlanFinished(res.results, testPlanName, this.startTime, res.executionId, isAnonymous, isCodeMode, res.childTestResults);
60 });
61 }).then(async results => { // array of results per execution
62 const flattenResults = _(results).flattenDeep().value();
63 perf.log('right before onAllTestPlansFinished');
64 await reporter.onAllTestPlansFinished(flattenResults);
65 perf.log('right after onAllTestPlansFinished');
66 return flattenResults.map(res => res.results).reduce((total, cur) => Object.assign(total, cur), {});
67 });
68 });
69 }
70}
71
72async function getSuite(options, branchToUse) {
73 // local code test
74 if (options.files.length > 0) {
75 const { buildCodeTests } = require('./buildCodeTests');
76 let webpackConfig = {};
77 if (options.webpackConfig) {
78 const webpackConfigPath = path.join(process.cwd(), options.webpackConfig);
79 webpackConfig = require(webpackConfigPath);
80 }
81
82 return buildCodeTests(options.files, webpackConfig, { baseUrl: options.baseUrl });
83 }
84 // regular test
85 const servicesApi = require('../commons/testimServicesApi');
86 return await servicesApi.getSuiteTestList(
87 options.project,
88 options.label,
89 options.testId,
90 options.name,
91 options.testConfigNames,
92 options.suites,
93 branchToUse,
94 options.rerunFailedByRunId,
95 options.testConfigIds
96 );
97}
98module.exports = AnonymousTestPlanRunner;