Skip to content

Commit

Permalink
fix(no-deprecated-functions): remove process.cwd from resolve paths (
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Sep 29, 2021
1 parent ffc9392 commit 6940488
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 180 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,43 @@ doing:
This is included in all configs shared by this plugin, so can be omitted if
extending them.

The behaviour of some rules (specifically `no-deprecated-functions`) change
depending on the version of `jest` being used.
### Jest `version` setting

This setting is detected automatically based off the version of the `jest`
package installed in `node_modules`, but it can also be provided explicitly if
desired:
The behaviour of some rules (specifically [`no-deprecated-functions`][]) change
depending on the version of Jest being used.

By default, this plugin will attempt to determine to locate Jest using
`require.resolve`, meaning it will start looking in the closest `node_modules`
folder to the file being linted and work its way up.

Since we cache the automatically determined version, if you're linting
sub-folders that have different versions of Jest, you may find that the wrong
version of Jest is considered when linting. You can work around this by
providing the Jest version explicitly in nested ESLint configs:

```json
{
"settings": {
"jest": {
"version": 26
"version": 27
}
}
}
```

To avoid hard-coding a number, you can also fetch it from the installed version
of Jest if you use a JavaScript config file such as `.eslintrc.js`:

```js
module.exports = {
settings: {
jest: {
version: require('jest/package.json').version,
},
},
};
```

## Shareable configurations

### Recommended
Expand Down Expand Up @@ -226,3 +246,4 @@ https://github.com/istanbuljs/eslint-plugin-istanbul
[suggest]: https://img.shields.io/badge/-suggest-yellow.svg
[fixable]: https://img.shields.io/badge/-fixable-green.svg
[style]: https://img.shields.io/badge/-style-blue.svg
[`no-deprecated-functions`]: docs/rules/no-deprecated-functions.md
5 changes: 5 additions & 0 deletions docs/rules/no-deprecated-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ either been renamed for clarity, or replaced with more powerful APIs.
While typically these deprecated functions are kept in the codebase for a number
of majors, eventually they are removed completely.

This rule requires knowing which version of Jest you're using - see
[this section of the readme](../../README.md#jest-version-setting) for details
on how that is obtained automatically and how you can explicitly provide a
version if needed.

## Rule details

This rule warns about calls to deprecated functions, and provides details on
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const importDefault = (moduleName: string) =>
interopRequireDefault(require(moduleName)).default;

const rulesDir = join(__dirname, 'rules');
const excludedFiles = ['__tests__', 'utils'];
const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];

const rules = readdirSync(rulesDir)
.map(rule => parse(rule).name)
Expand Down
227 changes: 227 additions & 0 deletions src/rules/__tests__/detectJestVersion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package';
import { create } from 'ts-node';
import { detectJestVersion } from '../detectJestVersion';

const compileFnCode = (pathToFn: string) => {
const fnContents = fs.readFileSync(pathToFn, 'utf-8');

return create({
transpileOnly: true,
compilerOptions: { sourceMap: false },
}).compile(fnContents, pathToFn);
};
const compiledFn = compileFnCode(require.resolve('../detectJestVersion.ts'));
const relativePathToFn = 'eslint-plugin-jest/lib/rules/detectJestVersion.js';

const runNodeScript = (cwd: string, script: string) => {
return spawnSync('node', ['-e', script.split('\n').join(' ')], {
cwd,
encoding: 'utf-8',
});
};

const runDetectJestVersion = (cwd: string) => {
return runNodeScript(
cwd,
`
try {
console.log(require('${relativePathToFn}').detectJestVersion());
} catch (error) {
console.error(error.message);
}
`,
);
};

/**
* Makes a new temp directory, prefixed with `eslint-plugin-jest-`
*
* @return {Promise<string>}
*/
const makeTempDir = () =>
fs.mkdtempSync(path.join(os.tmpdir(), 'eslint-plugin-jest-'));

interface ProjectStructure {
[key: `${string}/package.json`]: JSONSchemaForNPMPackageJsonFiles;
[key: `${string}/${typeof relativePathToFn}`]: string;
[key: `${string}/`]: null;
'package.json'?: JSONSchemaForNPMPackageJsonFiles;
}

const setupFakeProject = (structure: ProjectStructure): string => {
const tempDir = makeTempDir();

for (const [filePath, contents] of Object.entries(structure)) {
if (contents === null) {
fs.mkdirSync(path.join(tempDir, filePath), { recursive: true });

continue;
}

const folderPath = path.dirname(filePath);

// make the directory (recursively)
fs.mkdirSync(path.join(tempDir, folderPath), { recursive: true });

const finalContents =
typeof contents === 'string' ? contents : JSON.stringify(contents);

fs.writeFileSync(path.join(tempDir, filePath), finalContents);
}

return tempDir;
};

describe('detectJestVersion', () => {
describe('basic tests', () => {
const packageJsonFactory = jest.fn<JSONSchemaForNPMPackageJsonFiles, []>();

beforeEach(() => {
jest.resetModules();
jest.doMock(require.resolve('jest/package.json'), packageJsonFactory);
});

describe('when the package.json is missing the version property', () => {
it('throws an error', () => {
packageJsonFactory.mockReturnValue({});

expect(() => detectJestVersion()).toThrow(
/Unable to detect Jest version/iu,
);
});
});

it('caches versions', () => {
packageJsonFactory.mockReturnValue({ version: '1.2.3' });

const version = detectJestVersion();

jest.resetModules();

expect(detectJestVersion).not.toThrow();
expect(detectJestVersion()).toBe(version);
});
});

describe('when in a simple project', () => {
it('finds the correct version', () => {
const projectDir = setupFakeProject({
'package.json': { name: 'simple-project' },
[`node_modules/${relativePathToFn}` as const]: compiledFn,
'node_modules/jest/package.json': {
name: 'jest',
version: '21.0.0',
},
});

const { stdout, stderr } = runDetectJestVersion(projectDir);

expect(stdout.trim()).toBe('21');
expect(stderr.trim()).toBe('');
});
});

describe('when in a hoisted mono-repo', () => {
it('finds the correct version', () => {
const projectDir = setupFakeProject({
'package.json': { name: 'mono-repo' },
[`node_modules/${relativePathToFn}` as const]: compiledFn,
'node_modules/jest/package.json': {
name: 'jest',
version: '19.0.0',
},
'packages/a/package.json': { name: 'package-a' },
'packages/b/package.json': { name: 'package-b' },
});

const { stdout, stderr } = runDetectJestVersion(projectDir);

expect(stdout.trim()).toBe('19');
expect(stderr.trim()).toBe('');
});
});

describe('when in a subproject', () => {
it('finds the correct versions', () => {
const projectDir = setupFakeProject({
'backend/package.json': { name: 'package-a' },
[`backend/node_modules/${relativePathToFn}` as const]: compiledFn,
'backend/node_modules/jest/package.json': {
name: 'jest',
version: '24.0.0',
},
'frontend/package.json': { name: 'package-b' },
[`frontend/node_modules/${relativePathToFn}` as const]: compiledFn,
'frontend/node_modules/jest/package.json': {
name: 'jest',
version: '15.0.0',
},
});

const { stdout: stdoutBackend, stderr: stderrBackend } =
runDetectJestVersion(path.join(projectDir, 'backend'));

expect(stdoutBackend.trim()).toBe('24');
expect(stderrBackend.trim()).toBe('');

const { stdout: stdoutFrontend, stderr: stderrFrontend } =
runDetectJestVersion(path.join(projectDir, 'frontend'));

expect(stdoutFrontend.trim()).toBe('15');
expect(stderrFrontend.trim()).toBe('');
});
});

describe('when jest is not installed', () => {
it('throws an error', () => {
const projectDir = setupFakeProject({
'package.json': { name: 'no-jest' },
[`node_modules/${relativePathToFn}` as const]: compiledFn,
'node_modules/pack/package.json': { name: 'pack' },
});

const { stdout, stderr } = runDetectJestVersion(projectDir);

expect(stdout.trim()).toBe('');
expect(stderr.trim()).toContain('Unable to detect Jest version');
});
});

describe('when jest is changed on disk', () => {
it('uses the cached version', () => {
const projectDir = setupFakeProject({
'package.json': { name: 'no-jest' },
[`node_modules/${relativePathToFn}` as const]: compiledFn,
'node_modules/jest/package.json': { name: 'jest', version: '26.0.0' },
});

const { stdout, stderr } = runNodeScript(
projectDir,
`
const { detectJestVersion } = require('${relativePathToFn}');
const fs = require('fs');
console.log(detectJestVersion());
fs.writeFileSync(
'node_modules/jest/package.json',
JSON.stringify({
name: 'jest',
version: '25.0.0',
}),
);
console.log(detectJestVersion());
`,
);

const [firstCall, secondCall] = stdout.split('\n');

expect(firstCall).toBe('26');
expect(secondCall).toBe('26');
expect(stderr.trim()).toBe('');
});
});
});
Loading

0 comments on commit 6940488

Please sign in to comment.