Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove isTestFile check #38

Merged
merged 2 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"homepage": "https://github.com/JoinColony/eslint-import-resolver-jest",
"dependencies": {
"find-root": "^1.0.0",
"micromatch": "^3.1.6",
"resolve": "^1.5.0"
},
"files": [
Expand Down
44 changes: 10 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const path = require('path');
const findRoot = require('find-root');
const mm = require('micromatch');
const resolve = require('resolve');

type ResolverConfig = {
Expand Down Expand Up @@ -48,11 +47,12 @@ exports.resolve = function resolver(
config?: ResolverConfig
): ResolverResult {
const jestConfig = getJestConfig(config, file);
if (!isTestFile(jestConfig, file)) {
return NOTFOUND;
}
const pathToResolve = applyModuleNameMapper(jestConfig, source) || source;
const resolvedPath = resolvePath(jestConfig, path.dirname(file), pathToResolve);
const resolvedPath = resolvePath(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change (as well as one below) was to satisfy an eslint error I was seeing in my editor thanks to the "prettier/prettier" rule config in this project.

jestConfig,
path.dirname(file),
pathToResolve
);
if (resolvedPath) {
return {
found: true,
Expand Down Expand Up @@ -90,34 +90,6 @@ function getJestConfig(config?: ResolverConfig = {}, file: Path): JestConfig {
return Object.assign({}, JEST_DEFAULT_CONFIG, jestConfig);
}

/*
* Check if we're on a Windows system
*/
function isWindows() {
return process.platform === 'win32' || path.sep === '\\';
}

/*
* Transform given string to a POSIX-style path.
*/
function toPosixSlashes(str) {
return str.replace(/\\/g, '/');
}

/*
* Check whether a file is a test file to see whether this resolver is applicable
*/
function isTestFile(config: JestConfig, file: Path): boolean {
if (config.testRegex) {
return new RegExp(config.testRegex).test(file);
}
const testGlobs = config.testMatch.map(pattern => {
const expandedPattern = getAbsolutePath(config, pattern);
return isWindows() ? toPosixSlashes(expandedPattern) : expandedPattern;
});
return !!mm(file, testGlobs).length;
}

/**
* See whether an import has a corresponding moduleNameMapper, map it and return the path
*/
Expand All @@ -139,7 +111,11 @@ function applyModuleNameMapper(jestConfig: JestConfig, source: Path): Path {
* Will also attempt to resolve to an index file
* Furthermore it'll look in moduleDirectories, if supplied
*/
function resolvePath(jestConfig: JestConfig, basedir: Path, pathToResolve: Path): Path {
function resolvePath(
jestConfig: JestConfig,
basedir: Path,
pathToResolve: Path
): Path {
const { moduleDirectories, moduleFileExtensions, modulePaths } = jestConfig;
const absoluteModulePaths = modulePaths.map(
mPath =>
Expand Down
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1352,9 +1352,10 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"

flow-bin@^0.66.0:
version "0.66.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.66.0.tgz#a96dde7015dc3343fd552a7b4963c02be705ca26"
flow-bin@^0.67.1:
version "0.67.1"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.67.1.tgz#eabb7197cce870ac9442cfd04251c7ddc30377db"
integrity sha512-jZwanQrtgVRDKs3Le3MmD44YTZSzWkpuUeMv4ZckDOuaa2VQ41ttuxqzxErRdTr/ZSs+J1L2SQW6NKO+D4t2Vw==

flow-copy-source@^1.3.0:
version "1.3.0"
Expand Down