nx/packages/eslint/src/generators/lint-project/lint-project-convert-monorepo.spec.ts
Jack Hsu c24c20e990
fix(linter): ensure that @nx/eslint-plugin is installed when we add an extracted base eslintrc file (#26679)
The current eslint logic doesn't add the necessary `@nx/eslint-plugin`
package when we extract root config. This PR fixes the issue. This
applies to projects not generated using CNW but rather using something
like `npm create vite`, where the eslint setup isn't what we expect.

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-06-26 12:38:31 +02:00

93 lines
2.1 KiB
TypeScript

import {
addProjectConfiguration,
ProjectGraph,
readJson,
Tree,
} from '@nx/devkit';
import { Linter } from '../utils/linter';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { lintProjectGenerator } from './lint-project';
let projectGraph: ProjectGraph;
jest.mock('@nx/devkit', () => ({
...jest.requireActual<any>('@nx/devkit'),
createProjectGraphAsync: jest
.fn()
.mockImplementation(async () => projectGraph),
}));
describe('@nx/eslint:lint-project (convert to monorepo style)', () => {
let tree: Tree;
const defaultOptions = {
skipFormat: false,
addPlugin: true,
};
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
const rootpkg = {
root: '.',
projectType: 'library' as const,
targets: {
'eslint:lint': {
executor: 'nx:run-commands',
options: {
command: 'eslint .',
},
},
},
};
projectGraph = {
nodes: {
rootpkg: {
type: 'lib',
name: 'rootpkg',
data: rootpkg,
},
},
dependencies: {},
};
addProjectConfiguration(tree, 'rootpkg', rootpkg);
tree.write(
'.eslintrc.cjs',
`
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
}
`
);
});
it('should generate a eslint config and configure the target in project configuration', async () => {
addProjectConfiguration(tree, 'nestedpkg', {
root: 'nestedpkg',
projectType: 'library',
targets: {},
});
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'nestedpkg',
setParserOptionsProject: false,
});
expect(readJson(tree, 'package.json')).toMatchObject({
devDependencies: {
'@nx/eslint-plugin': expect.any(String),
},
});
});
});