-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
jest-file-exists
and jest-resolve-dependencies
.
- Loading branch information
Showing
18 changed files
with
264 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "jest-file-exists", | ||
"version": "13.2.2", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/jest.git" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"main": "build/index.js", | ||
"jest": { | ||
"automock": false, | ||
"rootDir": "./src", | ||
"scriptPreprocessor": "../../babel-jest", | ||
"testEnvironment": "node" | ||
}, | ||
"scripts": { | ||
"test": "../../packages/jest-cli/bin/jest.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
'use strict'; | ||
|
||
const fileExists = require('../'); | ||
const path = require('path'); | ||
|
||
test('file exists', () => { | ||
expect(fileExists(__filename)).toBe(true); | ||
}); | ||
|
||
test('file exists if module map is provided', () => { | ||
expect(fileExists('/random-string.js', { | ||
'/random-string.js': true, | ||
})).toBe(true); | ||
}); | ||
|
||
test('file does not exist', () => { | ||
expect(fileExists( | ||
path.join(path.basename(__filename), 'does-probably-not-exist.js'), | ||
{ | ||
'/random-string.js': true, | ||
}, | ||
)).toBe(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {Path} from 'types/Config'; | ||
import type {FileMetaData} from 'types/HasteMap'; | ||
|
||
const fs = require('fs'); | ||
|
||
module.exports = ( | ||
filePath: Path, | ||
files: ?{[filepath: string]: FileMetaData}, | ||
): boolean => { | ||
if (files && files[filePath]) { | ||
return true; | ||
} | ||
|
||
try { | ||
return fs.statSync(filePath).isFile(); | ||
} catch (e) {} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "jest-resolve-dependencies", | ||
"version": "13.2.2", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/jest.git" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"main": "build/index.js", | ||
"dependencies": { | ||
"jest-file-exists": "13.2.2", | ||
"jest-resolve": "13.2.2" | ||
}, | ||
"jest": { | ||
"rootDir": "./src", | ||
"scriptPreprocessor": "../../babel-jest", | ||
"testEnvironment": "node" | ||
}, | ||
"scripts": { | ||
"test": "../../packages/jest-cli/bin/jest.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {HasteMap} from 'types/HasteMap'; | ||
import type {Path} from 'types/Config'; | ||
import type Resolver from '../../jest-resolve/src'; | ||
|
||
const fileExists = require('jest-file-exists'); | ||
|
||
export type ResolveModuleConfig = {skipNodeResolution?: boolean}; | ||
|
||
function compact(array: Array<?Path>): Array<Path> { | ||
const result = []; | ||
for (let i = 0; i < array.length; ++i) { | ||
const element = array[i]; | ||
if (element != null) { | ||
result.push(element); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* DependencyResolver is used to resolve the direct dependencies of a module or | ||
* to retrieve a list of all transitive inverse dependencies. | ||
*/ | ||
class DependencyResolver { | ||
_moduleMap: HasteMap; | ||
_resolver: Resolver; | ||
|
||
constructor(resolver: Resolver, moduleMap: HasteMap) { | ||
this._resolver = resolver; | ||
this._moduleMap = moduleMap; | ||
} | ||
|
||
resolve( | ||
file: Path, | ||
options?: ResolveModuleConfig, | ||
): Array<Path> { | ||
if (!this._moduleMap.files[file]) { | ||
return []; | ||
} | ||
return compact( | ||
this._moduleMap.files[file][3].map(dependency => { | ||
if (this._resolver.isCoreModule(dependency)) { | ||
return null; | ||
} | ||
try { | ||
return this._resolver.resolveModule(file, dependency, options); | ||
} catch (e) {} | ||
return this._resolver.getMockModule(dependency) || null; | ||
}), | ||
); | ||
} | ||
|
||
resolveInverse( | ||
paths: Set<Path>, | ||
filter: (file: Path) => boolean, | ||
options?: ResolveModuleConfig, | ||
): Array<Path> { | ||
const collectModules = (relatedPaths, moduleMap, changed) => { | ||
const visitedModules = new Set(); | ||
while (changed.size) { | ||
changed = new Set(moduleMap.filter(module => ( | ||
!visitedModules.has(module.file) && | ||
module.dependencies.some(dep => dep && changed.has(dep)) | ||
)).map(module => { | ||
const file = module.file; | ||
if (filter(file)) { | ||
relatedPaths.add(file); | ||
} | ||
visitedModules.add(file); | ||
return module.file; | ||
})); | ||
} | ||
return relatedPaths; | ||
}; | ||
|
||
if (!paths.size) { | ||
return []; | ||
} | ||
|
||
const relatedPaths = new Set(); | ||
const changed = new Set(); | ||
for (const path of paths) { | ||
if (fileExists(path, this._moduleMap.files)) { | ||
const module = this._moduleMap.files[path]; | ||
if (module) { | ||
changed.add(path); | ||
if (filter(path)) { | ||
relatedPaths.add(path); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const modules = []; | ||
for (const file in this._moduleMap.files) { | ||
modules.push({ | ||
file, | ||
dependencies: this.resolve(file, options), | ||
}); | ||
} | ||
return Array.from(collectModules(relatedPaths, modules, changed)); | ||
} | ||
|
||
} | ||
|
||
module.exports = DependencyResolver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.