Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80635 views
1
/**
2
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
*
4
* This source code is licensed under the BSD-style license found in the
5
* LICENSE file in the root directory of this source tree. An additional grant
6
* of patent rights can be found in the PATENTS file in the same directory.
7
*/
8
'use strict';
9
10
jest.autoMockOff().mock('fs');
11
12
var q = require('q');
13
14
describe('TestRunner', function() {
15
var TestRunner;
16
17
beforeEach(function() {
18
TestRunner = require('../TestRunner');
19
});
20
21
describe('_isTestFilePath', function() {
22
var runner;
23
var utils;
24
25
beforeEach(function() {
26
utils = require('../lib/utils');
27
runner = new TestRunner(utils.normalizeConfig({
28
rootDir: '.',
29
testPathDirs: []
30
}));
31
});
32
33
it('supports ../ paths and unix separators', function() {
34
var path = '/path/to/__tests__/foo/bar/baz/../../../test.js';
35
var isTestFile = runner._isTestFilePath(path);
36
37
return expect(isTestFile).toEqual(true);
38
});
39
40
it('supports ../ paths and windows separators', function() {
41
var path = 'c:\\path\\to\\__tests__\\foo\\bar\\baz\\..\\..\\..\\test.js';
42
var isTestFile = runner._isTestFilePath(path);
43
44
return expect(isTestFile).toEqual(true);
45
});
46
47
it('supports unix separators', function() {
48
var path = '/path/to/__tests__/test.js';
49
var isTestFile = runner._isTestFilePath(path);
50
51
return expect(isTestFile).toEqual(true);
52
});
53
54
it('supports windows separators', function() {
55
var path = 'c:\\path\\to\\__tests__\\test.js';
56
var isTestFile = runner._isTestFilePath(path);
57
58
return expect(isTestFile).toEqual(true);
59
});
60
});
61
62
describe('streamTestPathsRelatedTo', function() {
63
var fakeDepsFromPath;
64
var fs;
65
var runner;
66
var utils;
67
68
function pathStreamToPromise(pathStream) {
69
var deferred = q.defer();
70
71
var paths = [];
72
pathStream.on('data', function(pathStr) {
73
paths.push(pathStr);
74
});
75
76
pathStream.on('error', function(err) {
77
deferred.reject(err);
78
});
79
80
pathStream.on('end', function() {
81
deferred.resolve(paths);
82
});
83
84
return deferred.promise;
85
}
86
87
beforeEach(function() {
88
fs = require('graceful-fs');
89
utils = require('../lib/utils');
90
runner = new TestRunner(utils.normalizeConfig({
91
rootDir: '.',
92
testPathDirs: []
93
}));
94
95
fakeDepsFromPath = {};
96
runner._constructModuleLoader = function() {
97
return q({
98
getDependentsFromPath: function(modulePath) {
99
return fakeDepsFromPath[modulePath] || [];
100
}
101
});
102
};
103
});
104
105
pit('finds no tests when no tests depend on the path', function() {
106
var path = '/path/to/module/not/covered/by/any/tests.js';
107
fakeDepsFromPath[path] = [];
108
109
// Mock out existsSync to return true, since our test path isn't real
110
fs.existsSync = function() { return true; };
111
112
return pathStreamToPromise(runner.streamTestPathsRelatedTo([path]))
113
.then(function(relatedTests) {
114
expect(relatedTests).toEqual([]);
115
});
116
});
117
118
pit('finds tests that depend directly on the path', function() {
119
var path = '/path/to/module/covered/by/one/test.js';
120
var dependentTestPath = '/path/to/test/__tests__/asdf-test.js';
121
fakeDepsFromPath[path] = [dependentTestPath];
122
123
// Mock out existsSync to return true, since our test path isn't real
124
fs.existsSync = function() { return true; };
125
126
return pathStreamToPromise(runner.streamTestPathsRelatedTo([path]))
127
.then(function(relatedTests) {
128
expect(relatedTests).toEqual([dependentTestPath]);
129
});
130
});
131
132
pit('finds tests that depend indirectly on the path', function() {
133
var path = '/path/to/module/covered/by/module/covered/by/test.js';
134
var dependentModulePath = '/path/to/dependent/module.js';
135
var dependentTestPath = '/path/to/test/__tests__/asdf-test.js';
136
fakeDepsFromPath[path] = [dependentModulePath];
137
fakeDepsFromPath[dependentModulePath] = [dependentTestPath];
138
139
// Mock out existsSync to return true, since our test path isn't real
140
fs.existsSync = function() { return true; };
141
142
return pathStreamToPromise(runner.streamTestPathsRelatedTo([path]))
143
.then(function(relatedTests) {
144
expect(relatedTests).toEqual([dependentTestPath]);
145
});
146
});
147
148
pit('finds multiple tests that depend indirectly on the path', function() {
149
var path = '/path/to/module/covered/by/modules/covered/by/test.js';
150
var dependentModulePath1 = '/path/to/dependent/module1.js';
151
var dependentModulePath2 = '/path/to/dependent/module2.js';
152
var dependentTestPath1 = '/path/to/test1/__tests__/asdf1-test.js';
153
var dependentTestPath2 = '/path/to/test2/__tests__/asdf2-test.js';
154
fakeDepsFromPath[path] = [dependentModulePath1, dependentModulePath2];
155
fakeDepsFromPath[dependentModulePath1] = [dependentTestPath1];
156
fakeDepsFromPath[dependentModulePath2] = [dependentTestPath2];
157
158
// Mock out existsSync to return true, since our test path isn't real
159
fs.existsSync = function() { return true; };
160
161
return pathStreamToPromise(runner.streamTestPathsRelatedTo([path]))
162
.then(function(relatedTests) {
163
expect(relatedTests).toEqual([
164
dependentTestPath1,
165
dependentTestPath2
166
]);
167
});
168
});
169
170
pit('flattens circular dependencies', function() {
171
var path = '/path/to/module/covered/by/modules/covered/by/test.js';
172
var directDependentModulePath = '/path/to/direct/dependent/module.js';
173
var indirectDependentModulePath = '/path/to/indirect/dependent/module.js';
174
var dependentTestPath = '/path/to/test/__tests__/asdf-test.js';
175
fakeDepsFromPath[path] = [directDependentModulePath];
176
fakeDepsFromPath[directDependentModulePath] =
177
[indirectDependentModulePath];
178
fakeDepsFromPath[indirectDependentModulePath] = [
179
directDependentModulePath,
180
dependentTestPath
181
];
182
183
// Mock out existsSync to return true, since our test path isn't real
184
fs.existsSync = function() { return true; };
185
186
return pathStreamToPromise(runner.streamTestPathsRelatedTo([path]))
187
.then(function(relatedTests) {
188
expect(relatedTests).toEqual([dependentTestPath]);
189
});
190
});
191
192
pit('filters test paths that don\'t exist on the filesystem', function() {
193
var path = '/path/to/module/covered/by/one/test.js';
194
var existingTestPath = '/path/to/test/__tests__/exists-test.js';
195
var nonExistantTestPath = '/path/to/test/__tests__/doesnt-exist-test.js';
196
fakeDepsFromPath[path] = [existingTestPath, nonExistantTestPath];
197
198
// Mock out existsSync to return true, since our test path isn't real
199
fs.existsSync = function(path) {
200
return path !== nonExistantTestPath;
201
};
202
203
return pathStreamToPromise(runner.streamTestPathsRelatedTo([path]))
204
.then(function(relatedTests) {
205
expect(relatedTests).toEqual([existingTestPath]);
206
});
207
});
208
});
209
});
210
211