Skip to content

Commit

Permalink
chore(no-deprecated-functions): cache jest version
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed May 5, 2020
1 parent 91ba077 commit 27e3251
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/rules/__tests__/no-deprecated-functions.test.ts
Expand Up @@ -3,7 +3,10 @@ import * as os from 'os';
import * as path from 'path';
import { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule, { JestVersion } from '../no-deprecated-functions';
import rule, {
JestVersion,
_clearCachedJestVersion,
} from '../no-deprecated-functions';

const ruleTester = new TSESLint.RuleTester();

Expand Down Expand Up @@ -89,6 +92,8 @@ const generateInvalidCases = (
];
};

beforeEach(() => _clearCachedJestVersion());

// a few sanity checks before doing our massive loop
ruleTester.run('no-deprecated-functions', rule, {
valid: [
Expand Down
11 changes: 10 additions & 1 deletion src/rules/no-deprecated-functions.ts
Expand Up @@ -30,7 +30,16 @@ interface EslintPluginJestSettings {
version: JestVersion;
}

let cachedJestVersion: JestVersion | null = null;

/** @internal */
export const _clearCachedJestVersion = () => (cachedJestVersion = null);

const detectJestVersion = (): JestVersion => {
if (cachedJestVersion) {
return cachedJestVersion;
}

try {
const jestPath = require.resolve('jest/package.json', {
paths: [process.cwd()],
Expand All @@ -42,7 +51,7 @@ const detectJestVersion = (): JestVersion => {
if (jestPackageJson.version) {
const [majorVersion] = jestPackageJson.version.split('.');

return parseInt(majorVersion);
return (cachedJestVersion = parseInt(majorVersion, 10));
}
} catch {}

Expand Down

0 comments on commit 27e3251

Please sign in to comment.