Skip to content

Commit 7e237a5

Browse files
committed
Enable Jest ESLint plugin with recommended config
1 parent 1d52482 commit 7e237a5

11 files changed

+93
-87
lines changed

eslint.config.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path';
22
import { globalIgnores } from 'eslint/config';
3+
import jest from 'eslint-plugin-jest';
34
import prettierRecommended from 'eslint-plugin-prettier/recommended';
45
import globals from 'globals';
56
import tsEslint from 'typescript-eslint';
@@ -52,10 +53,6 @@ const config = tsEslint.config([
5253
globals: {
5354
...globals.browser,
5455
...globals.node,
55-
...globals.mocha,
56-
...globals.jest,
57-
__DEBUG_SERVER_ERRORS__: true,
58-
__SERVER_ERRORS__: true,
5956
},
6057

6158
parserOptions: {
@@ -190,6 +187,16 @@ const config = tsEslint.config([
190187
'react/no-deprecated': 'off',
191188
},
192189
},
190+
{
191+
files: ['node_package/tests/**', '**/*.test.{js,jsx,ts,tsx}'],
192+
193+
extends: [jest.configs['flat/recommended'], jest.configs['flat/style']],
194+
195+
rules: {
196+
// Allows Jest mocks before import
197+
'import/first': 'off',
198+
},
199+
},
193200
// must be the last config in the array
194201
// https://github.com/prettier/eslint-plugin-prettier?tab=readme-ov-file#configuration-new-eslintconfigjs
195202
prettierRecommended,

node_package/tests/Authenticity.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ meta.content = testToken;
88
document.head.appendChild(meta);
99

1010
describe('authenticityToken', () => {
11-
expect.assertions(2);
1211
it('exists in ReactOnRails API', () => {
1312
expect.assertions(1);
14-
expect(typeof ReactOnRails.authenticityToken).toEqual('function');
13+
expect(typeof ReactOnRails.authenticityToken).toBe('function');
1514
});
1615

1716
it('can read Rails CSRF token from <meta>', () => {
@@ -22,10 +21,9 @@ describe('authenticityToken', () => {
2221
});
2322

2423
describe('authenticityHeaders', () => {
25-
expect.assertions(2);
2624
it('exists in ReactOnRails API', () => {
2725
expect.assertions(1);
28-
expect(typeof ReactOnRails.authenticityHeaders).toEqual('function');
26+
expect(typeof ReactOnRails.authenticityHeaders).toBe('function');
2927
});
3028

3129
it('returns valid header with CSRF token', () => {

node_package/tests/ComponentRegistry.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,12 @@ describe('ComponentRegistry', () => {
176176

177177
it('handles timeout for unregistered components', async () => {
178178
expect.assertions(1);
179+
let error;
179180
try {
180181
await ComponentRegistry.getOrWaitForComponent('NonExistent');
181-
} catch (error) {
182-
expect(error.message).toMatch(/Could not find component/);
182+
} catch (e) {
183+
error = e;
183184
}
185+
expect(error.message).toMatch(/Could not find component/);
184186
});
185187
});

node_package/tests/ReactOnRails.test.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as createReactClass from 'create-react-class';
77
import ReactOnRails from '../src/ReactOnRails.client.ts';
88

99
describe('ReactOnRails', () => {
10-
expect.assertions(14);
1110
it('render returns a virtual DOM element for component', () => {
1211
expect.assertions(1);
1312
const R1 = createReactClass({
@@ -25,7 +24,7 @@ describe('ReactOnRails', () => {
2524
document.body.appendChild(root);
2625
ReactOnRails.render('R1', {}, 'root');
2726

28-
expect(document.getElementById('root').textContent).toEqual(' WORLD ');
27+
expect(document.getElementById('root').textContent).toBe(' WORLD ');
2928
});
3029

3130
it('accepts traceTurbolinks as an option true', () => {

node_package/tests/StoreRegistry.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ function storeGenerator2(props) {
1414
return createStore(reducer, props);
1515
}
1616

17-
describe('', () => {
18-
expect.assertions(11);
17+
describe('StoreRegistry', () => {
18+
beforeEach(() => {
19+
StoreRegistry.stores().clear();
20+
});
21+
1922
it('StoreRegistry throws error for registering null or undefined store', () => {
2023
expect.assertions(2);
21-
StoreRegistry.stores().clear();
2224
expect(() => StoreRegistry.register({ storeGenerator: null })).toThrow(
2325
/Called ReactOnRails.registerStoreGenerators with a null or undefined as a value/,
2426
);
@@ -29,7 +31,6 @@ describe('', () => {
2931

3032
it('StoreRegistry throws error for retrieving unregistered store', () => {
3133
expect.assertions(1);
32-
StoreRegistry.stores().clear();
3334
expect(() => StoreRegistry.getStore('foobar')).toThrow(
3435
/There are no stores hydrated and you are requesting the store/,
3536
);
@@ -46,7 +47,7 @@ describe('', () => {
4647
expect(actual2).toEqual(expected2);
4748
});
4849

49-
it('StoreRegistry throws error for retrieving unregistered store', () => {
50+
it('StoreRegistry throws error for retrieving unregistered store generator', () => {
5051
expect.assertions(1);
5152
expect(() => StoreRegistry.getStoreGenerator('foobar')).toThrow(
5253
/Could not find store generator registered with name foobar\. Registered store generator names include/,

node_package/tests/buildConsoleReplay.test.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import buildConsoleReplay, { consoleReplay } from '../src/buildConsoleReplay.ts';
22

33
describe('consoleReplay', () => {
4-
expect.assertions(8);
54
it('does not throw an exception if no console.history object', () => {
65
expect.assertions(1);
76
expect(() => consoleReplay()).not.toThrow(/Error/);
@@ -14,13 +13,7 @@ describe('consoleReplay', () => {
1413
expect(actual).toEqual(expected);
1514
});
1615

17-
it('does not throw an exception if console.history.length is zero', () => {
18-
expect.assertions(1);
19-
console.history = [];
20-
expect(() => consoleReplay()).not.toThrow(/Error/);
21-
});
22-
23-
it('returns empty string if no console.history object', () => {
16+
it('returns empty string if console.history is empty', () => {
2417
expect.assertions(1);
2518
console.history = [];
2619
const actual = consoleReplay();
@@ -52,7 +45,7 @@ console.warn.apply(console, ["other message","{\\"c\\":3,\\"d\\":4}"]);`;
5245
expect(actual).toEqual(expected);
5346
});
5447

55-
it('replays converts script tag inside of object string to be safe ', () => {
48+
it('replays converts script tag inside of object string to be safe', () => {
5649
expect.assertions(1);
5750
console.history = [
5851
{

node_package/tests/renderFunction.test.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as createReactClass from 'create-react-class';
88
import isRenderFunction from '../src/isRenderFunction.ts';
99

1010
describe('isRenderFunction', () => {
11-
expect.assertions(6);
1211
it('returns false for a ES5 React Component', () => {
1312
expect.assertions(1);
1413

node_package/tests/scriptSanitizedVal.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import scriptSanitizedVal from '../src/scriptSanitizedVal.ts';
22

33
describe('scriptSanitizedVal', () => {
4-
expect.assertions(5);
54
it('returns no </script if spaces, uppercase 1', () => {
65
expect.assertions(1);
76
const input = '[SERVER] This is a script:"</div>"</script> <script>alert(\'WTF\')</ SCRIPT >';

node_package/tests/serverRenderReactComponent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('serverRenderReactComponent', () => {
245245
});
246246
assertIsPromise(renderResult);
247247
const result = await renderResult;
248-
expect(result.html).toEqual('<div>Hello</div>');
248+
expect(result.html).toBe('<div>Hello</div>');
249249
});
250250

251251
it('serverRenderReactComponent renders an error if attempting to render a renderer', () => {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
"eslint-config-shakacode": "^19.0.0",
4040
"eslint-import-resolver-alias": "^1.1.2",
4141
"eslint-plugin-import": "^2.31.0",
42+
"eslint-plugin-jest": "^28.11.0",
4243
"eslint-plugin-jsx-a11y": "^6.10.2",
4344
"eslint-plugin-prettier": "^5.2.3",
4445
"eslint-plugin-react": "^7.37.4",
4546
"eslint-plugin-react-hooks": "^5.2.0",
46-
"typescript-eslint": "^8.26.1",
47+
"typescript-eslint": "^8.29.1",
4748
"globals": "^16.0.0",
4849
"jest": "^29.7.0",
4950
"jest-environment-jsdom": "^29.7.0",

0 commit comments

Comments
 (0)