UNPKG

3.81 kBJavaScriptView Raw
1"use strict";
2
3require('@babel/register'); // to be able to use non-transpiled '@kiwicom/monorepo-utils' here
4
5
6var _require = require('create-jest-runner'),
7 pass = _require.pass,
8 fail = _require.fail,
9 skip = _require.skip;
10
11var CLIEngine = require('eslint').CLIEngine;
12
13var isCI = require('is-ci');
14
15var _require2 = require('@kiwicom/monorepo-utils'),
16 Git = _require2.Git;
17
18var formatter = require('./stylish');
19
20var isEslintConfigFile = require('./isEslintConfigFile');
21
22var PERFORM_FIXES = isCI === false;
23var cliEngine = new CLIEngine({
24 fix: PERFORM_FIXES,
25 reportUnusedDisableDirectives: true
26});
27// TODO: we should eventually get rid of 'create-jest-runner' dependency and use our own
28// implementation with this built-in mechanism. This way we don't have to iterate all the files
29// and skipping them.
30var changedFiles = Git.getChangesToTest();
31
32module.exports = function (_ref) {
33 var testPath = _ref.testPath,
34 extraOptions = _ref.extraOptions;
35 var start = Date.now();
36 var runAll = extraOptions.runAll;
37 var _iteratorNormalCompletion = true;
38 var _didIteratorError = false;
39 var _iteratorError = undefined;
40
41 try {
42 for (var _iterator = changedFiles[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
43 var changedFile = _step.value;
44
45 if (isEslintConfigFile(changedFile)) {
46 runAll = true;
47 break;
48 }
49 }
50 } catch (err) {
51 _didIteratorError = true;
52 _iteratorError = err;
53 } finally {
54 try {
55 if (!_iteratorNormalCompletion && _iterator.return != null) {
56 _iterator.return();
57 }
58 } finally {
59 if (_didIteratorError) {
60 throw _iteratorError;
61 }
62 }
63 }
64
65 if (cliEngine.isPathIgnored(testPath)) {
66 return skip({
67 start: start,
68 end: start,
69 test: {
70 path: testPath
71 }
72 });
73 }
74
75 if (runAll === false) {
76 var normalizedPath = testPath.replace(process.cwd(), '').replace(/^\//, '');
77
78 if (changedFiles.includes(normalizedPath) === false) {
79 return skip({
80 start: start,
81 end: start,
82 test: {
83 path: testPath
84 }
85 });
86 }
87 }
88
89 var report = cliEngine.executeOnFiles([testPath]);
90
91 if (PERFORM_FIXES) {
92 CLIEngine.outputFixes(report);
93 }
94
95 var end = Date.now();
96 var result = report.results[0];
97
98 if (result.errorCount === 0 && result.warningCount > 0) {
99 return passWithWarning({
100 start: start,
101 end: end,
102 test: {
103 path: testPath,
104 warningMessage: formatter(report.results)
105 }
106 });
107 } else if (result.errorCount > 0) {
108 return fail({
109 start: start,
110 end: end,
111 test: {
112 path: testPath,
113 errorMessage: formatter(report.results)
114 }
115 });
116 }
117
118 return pass({
119 start: start,
120 end: end,
121 test: {
122 path: testPath
123 }
124 });
125};
126
127function passWithWarning(_ref2) {
128 var start = _ref2.start,
129 end = _ref2.end,
130 test = _ref2.test;
131 return {
132 console: null,
133 failureMessage: test.warningMessage,
134 numFailingTests: 0,
135 numPassingTests: 1,
136 numPendingTests: 0,
137 numTodoTests: 0,
138 perfStats: {
139 end: new Date(start).getTime(),
140 start: new Date(end).getTime()
141 },
142 skipped: false,
143 snapshot: {
144 added: 0,
145 fileDeleted: false,
146 matched: 0,
147 unchecked: 0,
148 unmatched: 0,
149 updated: 0
150 },
151 sourceMaps: {},
152 testExecError: null,
153 testFilePath: test.path,
154 testResults: [{
155 ancestorTitles: [],
156 duration: end - start,
157 failureMessages: [test.warningMessage],
158 fullName: test.path,
159 numPassingAsserts: test.warningMessage ? 1 : 0,
160 status: test.warningMessage ? 'failed' : 'passed',
161 title: ''
162 }]
163 };
164}
\No newline at end of file