Testing custom JS inspections

Answered

Hi there,

I'm writing a test class for my custom Javascript inspection (extending JSInspection), but I'm having some trouble there. Perhaps someone over here can give me some tips.

The issue is that the inspection itself works fine in production, but doesn't seem to work when running test. While debugging the test, I can see breakpoints getting hit in my JSInspection#createVisitor() override, which tells me that my inspection is being registered properly. However, the visitor itself is never called after that.

Diving deeper into the code with the debugger suggests that my test file (MyTestData.js) is not recognised as a js file; its fileType is AbstractFileType (toString() does say Javascript though), and LanguageUtil#getLanguageForPsi() retuns PlainTextrLanguage. I'm assuming this is why my js visitor is not being used, and that if the fileType were JavaScriptFileType, getLanguageForPsi() would return JavascriptLanguage and my visitor would get called; all would be well.

Now why the heck is the fileType AbstractFileType and not JavascriptFileType here? I'm probably missing something in my setup, but I can't figure out what it is. Couldn't find much example code for js tests, so I'm a bit at a loss here. Hope someone else knows more :)

I'll add relevant details of the test setup below. If more are needed please let me know.


Many thanks,
Guno

 

File tree of the test directory:

src/test
├── kotlin
│   └── com
│       └── example
│           └── MyJsInspectionTest.kt
└── testData
    └── annotator
        └── js
            └── MyTestData.js

Contents of MyJsInspectionTest.kt:


package com.example

import com.intellij.testFramework.fixtures.BasePlatformTestCase

class MyJsInspectionTest : BasePlatformTestCase() {

    override fun getTestDataPath(): String = "src/test/testData"

    override fun isWriteActionRequired() = false

    override fun setUp() {
        super.setUp()
        myFixture.enableInspections(MyJsInspection())
    }

    fun testHighlighting() {
        myFixture.copyDirectoryToProject(".", ".")
        myFixture.testHighlighting(true, false, true, "annotator/js/MyTestData.js")
    }
}

MyTestData.js just contains a few random lines of js code.

Inspection declaration in plugin.xml:

<localInspection language="JavaScript"
        enabledByDefault="true"
        groupName="My inspections"
        displayName="MyJsInspection"
        implementationClass="com.example.MyJsInspection"
        level="ERROR"/>
0
2 comments

Hi Guno,

What IDE do you run your tests against? Is it WebStorm? Could you please share your Gradle build script (or the whole project, or minimal project to reproduce)?

0

Hi Karol,

Thanks for responding.

While creating a minimal project to reproduce the issue, I seem te have resolved it myself! It turns out this was in my plugin.xml:

<depends config-file="intellilang-js-support.xml">JavaScript</depends>

The referenced config file does not exist in my project though, so I just removed the attribute and that fixed it.
I find it strange that this apparently only had effect in my unit tests and not with actual plugin usage, but I'm not complaining :-)

Thanks,

Guno

0

Please sign in to leave a comment.