Dalle FAQ:
Would casting detect magic be considered an attack for
the purpose of ending an invisibility spell?
The invisibility spell states that “for purposes of the spell,
an attack includes any spell targeting a foe or whose area or
effect includes a foe.” [Emphasis added.] Detect magic and
similar spells have an area, so if this area included a foe, it
would count as an “attack” for this purpose.
Would an invisible character counterspelling an
enemy’s spell count as making an attack and thus end the
invisibility? What about a targeted dispel magic?
According to page 150 of the Player’s Handbook, “to use a
counterspell, you must select an opponent as the target of a
counterspell.” Thus, by definition, this falls into the category of
spells “targeting a foe,” so a counterspell would end the
invisibility.
Likewise, a dispel magic targeted on a foe is a spell
“targeting a foe,” so it would end the invisibility.