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

False positives in test functions #89

Open
kioan000 opened this issue Sep 23, 2022 · 2 comments
Open

False positives in test functions #89

kioan000 opened this issue Sep 23, 2022 · 2 comments

Comments

@kioan000
Copy link

kioan000 commented Sep 23, 2022

Using "in-module" test configuration if my test is using an update function it can happen that rule reports a false positive
image

@kioan000 kioan000 changed the title False positive in case of test function False positives in test functions Sep 23, 2022
@jfmengels
Copy link

Hi @kioan000

I'm not sure I understand the issue. Can you clarify what you mean with "in-module" test configuration? And would it be possible for you to provide an SSCCE?

@kioan000
Copy link
Author

kioan000 commented Sep 23, 2022

You're right sorry.
In this project I've configured elm-test to keep test functions within the source modules instead of having an external test folder. The project have a quite strict elm-review configuration (almost 30-35 review rules)

Unluckily it regards code from a private organization so I can't share the repository and despite my efforts I didn't succeed in reproducing a simplified example since I can't find a common pattern. It's a very bizarre case

For example in the following situation

port module Lib.CookieBar exposing (CookieBar, init, subscriptions, view, update, test)

import Lib.Logger as Logger
...

update : Msg -> CookieBar msg -> ( CookieBar msg, Cmd msg )
update msg model =
    case msg of
        ...

        SubError error ->
            model
                |> PrimaUpdate.withCmds
                    [ Logger.devConsoleErrorForJsonDecodeError
                        { location = "CookieBar subscription port is used wrongly: "
                        , error = error
                        }
                    ]

test : Test
test =
    Test.describe "Lib.CookieBar tests"
        [ updateTest
        ]
        
updateTest : Test
updateTest =
    Test.describe "Test over update function"
        [ Test.test "Tests init state"
            (\() ->
                init (always ())
                    |> isVisible
                    |> Expect.false "Cookie bar should be invisibile at init"
            )
        ] 

everything works fine. But if I add a test that calls my update:

updateTest : Test
updateTest =
    Test.describe "Test over update function"
        [ Test.test "Tests init state"
            (\() ->
                init (always ())
                    |> isVisible
                    |> Expect.false "Cookie bar should be invisibile at init"
            )
       , Test.test "Visible flag should be True when cookies acceptance is not present"
            (\() ->
                init (always ())
                    |> update (HasCookiesAcceptance False)
                    |> Tuple.first
                    |> isVisible
                    |> Expect.true "Cookie bar should be visible"
            )
        ] 

Then the rule notices not only that CookieBar.test calls a port inside Logger module, but suddenly reports the same thing in Lib.Authenticator.test (which calls the same port through its update function) that previously wasn't noticed

-- ELM-REVIEW ERROR --------------------------------- web-app/Lib/Logger.elm:8:6

NoUnusedPorts: Port `loggerPort_` is never used (Warning: can cause JS runtime
errors)

8| port loggerPort_ : JsonEncode.Value -> Cmd msg
        ^^^^^^^^^^^

Unused ports are not available in the compiled JavaScript and can cause runtime
errors when you try to access them.

You should either use this port somewhere, or remove it at the location I
pointed at. This may highlight some other unused code in your project too.

Warning: If you remove this port, remember to remove any calls to it in your
JavaScript code too.

I found this port called by the following functions, but none of them trace back
to a `main` function:

-> Lib.CookieBar.test
-> **Lib.Http.Authenticator.test**
-> Lib.Logger.devConsoleErrorForHttp
-> Lib.Logger.devConsoleErrorForJsonDecodeError
-> Lib.Logger.devConsoleLog
-> Lib.Logger.devConsoleWarn

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

No branches or pull requests

2 participants