nx/packages/linter/src/executors/eslint/utility/eslint-utils.spec.ts

139 lines
3.8 KiB
TypeScript

// Force module scoping
export default {};
jest.mock('eslint', () => ({
ESLint: jest.fn(),
}));
const { ESLint } = require('eslint');
(<jest.SpyInstance>ESLint).mockImplementation(() => ({
lintFiles: (args: string[]) => args,
}));
const { lint } = require('./eslint-utils');
describe('eslint-utils', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should create the ESLint instance with the proper parameters', async () => {
await lint('./.eslintrc.json', <any>{
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
}).catch(() => {});
expect(ESLint).toHaveBeenCalledWith({
overrideConfigFile: './.eslintrc.json',
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
ignorePath: undefined,
useEslintrc: true,
resolvePluginsRelativeTo: undefined,
rulePaths: [],
errorOnUnmatchedPattern: false,
});
});
it('should create the ESLint instance with the proper parameters', async () => {
await lint(undefined, <any>{
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
}).catch(() => {});
expect(ESLint).toHaveBeenCalledWith({
overrideConfigFile: undefined,
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
ignorePath: undefined,
useEslintrc: true,
resolvePluginsRelativeTo: undefined,
rulePaths: [],
errorOnUnmatchedPattern: false,
});
});
describe('noEslintrc', () => {
it('should create the ESLint instance with "useEslintrc" set to false', async () => {
await lint(undefined, <any>{
fix: true,
cache: true,
cacheLocation: '/root/cache',
noEslintrc: true,
}).catch(() => {});
expect(ESLint).toHaveBeenCalledWith({
overrideConfigFile: undefined,
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: undefined,
ignorePath: undefined,
useEslintrc: false,
resolvePluginsRelativeTo: undefined,
rulePaths: [],
errorOnUnmatchedPattern: false,
});
});
});
describe('rulesdir', () => {
it('should create the ESLint instance with "rulePaths" set to the given value for rulesdir', async () => {
const extraRuleDirectories = ['./some-rules', '../some-more-rules'];
await lint(undefined, {
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
rulesdir: extraRuleDirectories,
}).catch(() => {});
expect(ESLint).toHaveBeenCalledWith({
overrideConfigFile: undefined,
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
ignorePath: undefined,
useEslintrc: true,
resolvePluginsRelativeTo: undefined,
rulePaths: extraRuleDirectories,
errorOnUnmatchedPattern: false,
});
});
});
describe('resolvePluginsRelativeTo', () => {
it('should create the ESLint instance with "resolvePluginsRelativeTo" set to the given value for resolvePluginsRelativeTo', async () => {
await lint(undefined, {
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
resolvePluginsRelativeTo: './some-path',
}).catch(() => {});
expect(ESLint).toHaveBeenCalledWith({
overrideConfigFile: undefined,
fix: true,
cache: true,
cacheLocation: '/root/cache',
cacheStrategy: 'content',
ignorePath: undefined,
useEslintrc: true,
resolvePluginsRelativeTo: './some-path',
rulePaths: [],
errorOnUnmatchedPattern: false,
});
});
});
});