Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-duplicate-hooks]: allow subsequent describe.each? #642

Closed
kwonoj opened this issue Aug 12, 2020 · 4 comments · Fixed by #797
Closed

[no-duplicate-hooks]: allow subsequent describe.each? #642

kwonoj opened this issue Aug 12, 2020 · 4 comments · Fixed by #797

Comments

@kwonoj
Copy link

kwonoj commented Aug 12, 2020

Is there way to allow no-duplicate-hooks allow to work with describe.each, or is there recommended hook setup conforms those rules?

image

Interestingly, this doesn't error out for all describe.each but only if top-level describe includes subsequent nested describe.each.

repo: https://github.com/kwonoj/eslint-jest-duplicated-hooks

code:

describe('top', () => {
  describe.each(['approve', 'decline'])('inner1', () => {
		beforeEach(() => {
      console.log('set');
		});

		it('renders correctly', () => {
      expect(true).toBeTruthy();
		});
	});

	describe.each(['approve', 'decline'])('inner2', () => {
		beforeEach(() => {
			console.log('set2');
		});

		it('renders correctly 2', () => {
			expect(true).toBeTruthy
		});
	});
});

config:

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "es2020": true,
        "jest/globals": true
    },
    "extends": "eslint:recommended",
    "parserOptions": {
        "ecmaVersion": 11
    },
    "plugins": [
        "jest"
    ],
    "rules": {
        "jest/no-duplicate-hooks": "error"
    }
};
G-Rath added a commit that referenced this issue Mar 20, 2021
@G-Rath G-Rath self-assigned this Mar 20, 2021
G-Rath added a commit that referenced this issue Apr 2, 2021
github-actions bot pushed a commit that referenced this issue Apr 2, 2021
## [24.3.3](v24.3.2...v24.3.3) (2021-04-02)

### Bug Fixes

* **no-duplicate-hooks:** support `describe.each` ([#797](#797)) ([243cb4f](243cb4f)), closes [#642](#642)
* **prefer-expect-assertions:** support `.each` ([#798](#798)) ([f758243](f758243)), closes [#676](#676)
@github-actions
Copy link

github-actions bot commented Apr 2, 2021

🎉 This issue has been resolved in version 24.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@brewster1134
Copy link

I use the context alias for describe (as mentioned here), but i get a false negative for jest/no-duplicate-hooks if i have multiple setup/tear-down functions defined

describe('X, () => {
  context('when foo', () => {
    beforeEach(() => {});
  });

  context('when bar', () => {
    beforeEach(() => {});
  });
});

the tests run fine, just eslint complains. is there a way to configure it to treat context as it's own scope?

@G-Rath
Copy link
Collaborator

G-Rath commented Feb 3, 2022

@brewster1134 as you know, support for general context as an alias of describe is tracked by #83

However, what you're describing looks like a bug to me - ultimately, I think no-duplicate-hooks should just look for multiple uses of the same hook within the same scope (so regardless of if it's in a describe, context, it, etc)

@brewster1134
Copy link

that makes sense. i had assumed the same that it would work with any function name VS a white/black list of approved scopes, but when i only had the issue of beforeEach incorrectly flagged as being defined multiple times when using context, i thought maybe it needed explicit support.

let me know if i can help out by filing a bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants