Skip to content

Commit 925fa87

Browse files
committed
Enable rule for TypeScript test files only
1 parent f1dcf49 commit 925fa87

4 files changed

Lines changed: 81 additions & 33 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is only used to test the config.
2+
console.log('Hello, world!');

packages/jest/src/index.mjs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,54 @@ import globals from 'globals';
55
/**
66
* @type {import('eslint').Linter.Config[]}
77
*/
8-
const config = createConfig({
9-
name: '@metamask/eslint-config-jest',
8+
const config = createConfig([
9+
{
10+
name: '@metamask/eslint-config-jest',
1011

11-
extends: [jest.configs['flat/recommended'], jest.configs['flat/style']],
12+
extends: [jest.configs['flat/recommended'], jest.configs['flat/style']],
1213

13-
languageOptions: {
14-
globals: {
15-
...globals.jest,
14+
languageOptions: {
15+
globals: {
16+
...globals.jest,
17+
},
18+
},
19+
20+
rules: {
21+
'jest/consistent-test-it': ['error', { fn: 'it' }],
22+
'jest/no-duplicate-hooks': 'error',
23+
'jest/no-conditional-in-test': 'error',
24+
'jest/no-test-return-statement': 'error',
25+
'jest/prefer-hooks-on-top': 'error',
26+
'jest/prefer-lowercase-title': ['error', { ignore: ['describe'] }],
27+
'jest/prefer-spy-on': 'error',
28+
'jest/prefer-strict-equal': 'error',
29+
'jest/prefer-todo': 'error',
30+
'jest/require-top-level-describe': 'error',
31+
'jest/require-to-throw-message': 'error',
32+
'jest/valid-expect': ['error', { alwaysAwait: true }],
33+
'jest/no-restricted-matchers': [
34+
'error',
35+
{
36+
resolves: 'Use `expect(await promise)` instead.',
37+
toBeFalsy: 'Avoid `toBeFalsy`',
38+
toBeTruthy: 'Avoid `toBeTruthy`',
39+
toMatchSnapshot: 'Use `toMatchInlineSnapshot()` instead',
40+
toThrowErrorMatchingSnapshot:
41+
'Use `toThrowErrorMatchingInlineSnapshot()` instead',
42+
},
43+
],
1644
},
1745
},
46+
{
47+
name: '@metamask/eslint-config-jest/typescript',
1848

19-
rules: {
20-
'@typescript-eslint/unbound-method': 'off',
21-
'jest/consistent-test-it': ['error', { fn: 'it' }],
22-
'jest/no-duplicate-hooks': 'error',
23-
'jest/no-conditional-in-test': 'error',
24-
'jest/no-test-return-statement': 'error',
25-
'jest/prefer-hooks-on-top': 'error',
26-
'jest/prefer-lowercase-title': ['error', { ignore: ['describe'] }],
27-
'jest/prefer-spy-on': 'error',
28-
'jest/prefer-strict-equal': 'error',
29-
'jest/prefer-todo': 'error',
30-
'jest/require-top-level-describe': 'error',
31-
'jest/require-to-throw-message': 'error',
32-
'jest/unbound-method': 'error',
33-
'jest/valid-expect': ['error', { alwaysAwait: true }],
34-
'jest/no-restricted-matchers': [
35-
'error',
36-
{
37-
resolves: 'Use `expect(await promise)` instead.',
38-
toBeFalsy: 'Avoid `toBeFalsy`',
39-
toBeTruthy: 'Avoid `toBeTruthy`',
40-
toMatchSnapshot: 'Use `toMatchInlineSnapshot()` instead',
41-
toThrowErrorMatchingSnapshot:
42-
'Use `toThrowErrorMatchingInlineSnapshot()` instead',
43-
},
44-
],
49+
files: ['**/*.test.ts', '**/*.test.tsx', '**/*.test.mts', '**/*.test.cts'],
50+
51+
rules: {
52+
'@typescript-eslint/unbound-method': 'off',
53+
'jest/unbound-method': 'error',
54+
},
4555
},
46-
});
56+
]);
4757

4858
export default config;

packages/jest/src/index.test.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { ESLint } from 'eslint';
2+
import globals from 'globals';
3+
import { resolve } from 'path';
24
import { describe, it, expect } from 'vitest';
35

46
import config from './index.mjs';
@@ -15,4 +17,32 @@ describe('index', () => {
1517
expect(result[0].warningCount).toBe(0);
1618
expect(result[0].errorCount).toBe(0);
1719
});
20+
21+
it('is a valid ESLint config for TypeScript files', async () => {
22+
const api = new ESLint({
23+
baseConfig: config,
24+
overrideConfig: {
25+
languageOptions: {
26+
globals: {
27+
...globals.node,
28+
},
29+
parserOptions: {
30+
tsconfigRootDir: resolve(import.meta.dirname, '..'),
31+
project: 'tsconfig.json',
32+
},
33+
},
34+
},
35+
});
36+
37+
// In order to test rules that require type information, we need to actually
38+
// compile the file with TypeScript, so rather than using `api.lintText()`,
39+
// we use `api.lintFiles()` and pass in a file that we know will pass.
40+
const result = await api.lintFiles(
41+
resolve(import.meta.dirname, '__test__/dummy.test.ts'),
42+
);
43+
44+
expect(result[0].messages).toStrictEqual([]);
45+
expect(result[0].warningCount).toBe(0);
46+
expect(result[0].errorCount).toBe(0);
47+
});
1848
});

packages/jest/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"strictNullChecks": true
4+
},
5+
"include": ["src"]
6+
}

0 commit comments

Comments
 (0)