Triggered invalid code smell in Jest for typescript:S6481

Template for a good new topic, formatted with Markdown:

  • ALM used (Azure DevOps)
  • CI system used ( Azure DevOps)
  • Languages of the repository (React)

Triggered: " The array passed as the value prop to the Context provider changes every render. To fix this consider wrapping it in a useMemo hook." for Jest test.

We only could use react hook in function component, so this rule shouldn’t apply to test?

SonarCloud is asking “<StateContext.Provider value={[testState, () => {}]}>” and " <NotificationContext.Provider value={[{}, () => {}]}>" to useMemo hook

Rule: typescript:S6481

Code:

test('open new window when click quote id from exception search result', () => {
  render(
    <Router history={createMemoryHistory()}>
      <StateContext.Provider value={[testState, () => {}]}>
        <NotificationContext.Provider value={[{}, () => {}]}>
The array passed as the value prop to the Context provider changes every render. To fix this consider wrapping it in a useMemo hook.
          <Exceptions />
        </NotificationContext.Provider>
      </StateContext.Provider>
    </Router>
  )
})

Thanks for your help in advance.

Having sonar-project.properties setup as well and this file is added to the sonar.test.inclusion path.
example:

sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.test.tsx
sonar.exclusions=**/*.test.tsx

Hi @T_Z,

Thanks for bringing this to our attention. Indeed, the React performance rules should not consider test files. I have created a ticket to improve this: #3677

Regards,
François

Hi @T_Z, the React performance rules are not applied to test files after verification. And using the same inclusions/exclusions configuration, I don’t have React issues in test files. To investigate further, I would need the scanner logs. Could you send them, please?

Hi Francois , thanks a lot for looking into this, yes, sure, please find the log attached
log.txt (67.8 KB)
.

Hi @T_Z, thanks for the logs. They confirm that, as expected, the files with the extension .test.tsx should not raise React issues.
It’s possible to have a visual confirmation that a file should not have React issues by checking that Lines of Code is in the Code section of SonarCloud, as in the example below. :
Screenshot 2023-02-07 at 10.56.00

To investigate further, would you have a similar screenshot of a test file raising React issues, please?

Hi Francois, yes, sure, please find the screenshot attached.

code smell lint:

Thank you!

Hi @T_Z ,

I have managed to replicate the issue you are asking about locally and will try and get back to you with a solution.

Hi Shane,

Thanks a lot for your help!

Hi @T_Z , I have been investigating further into this issue over the past days, initially I was experiencing a similar issue, however it seemed to be related to my setup of the azure-pipeline and the sonar-project which were having conflicting issues.

On my side I have replicated the setup you mentioned:

# Define the same root directory for sources and tests
sonar.sources = src
sonar.tests = src
sonar.test.inclusions = **/*.test.*
sonar.exclusions = **/*.test.*

This resulted in my tests being ignored. In my case I am using .test. but it should behave the same in the case you have mentioned above.

Could you share with me the two files so I might compare them with what we are using as currently I am not experiencing specific issues

Also @T_Z can I check if you have any settings currently being applied in the general settings section of your project for test exclusions which could be conflicting with the values which you are passing in the sonar-project.properties file. This could be causing side effects which are not obvious from the logs which you have sent through already.

Hi Shane,

Yeah sure, here is those two files. sorry I don’t have permission to see the project settings, but I am asking our devops team to checking it, will report back later.

Thank you!
Exceptions.test.tsx.txt (7.6 KB)
Exceptions.tsx.txt (7.1 KB)

Hi Shane,

I just tried this again, looks like the code smell isn’t triggered for the test file anymore, I think this case is resolved now, I will try more test files in the following days when I have time.

Thanks a lot for your guys help along the way!

Cheers!

Hi @T_Z , that is great news, if the issue comes back let me know and i’ll continue investigating :slight_smile:

Thanks a lot Shane :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.