Set filetype association for LightJavaCodeInsightFixtureTestCase


I have succesfully created an extension that creates references from 'Angular HTML Template' elements to my DSL. When I want to unit-test these references the fixture always considers the file a Plain HTML file so the PsiTree is not as expected and I cannot test my references.

Is there anyway to set the file type association for a LightJavaCodeInsightFixtureTestCase so something with '*.component.html' is considered an Angular HTML Template FileType. 

Comment actions Permalink

Did you define a dependency on Angular plugin in your plugin.xml?

Comment actions Permalink

Yes, as an optional dependency. Everything works fine when I use it in runIde or when actually deploying the plugin. The only situation where it doesn't work is in a LightJavaCodeInsightFixtureTestCase where the PsiFile is identified as a plain HTML file instead of Angular HTML template

Comment actions Permalink

Tim, the Angular HTML language replaces the regular HTML under certain circumstances. One of the easiest ways to reproduce the regular project setup is to add `package.json` file with `@angular/core` dependency in it. E.g.:


You can also register your own context provider (`Angular2ContextProvider` in 2020.3 and older or `WebFrameworkContext` in 2021.1 and newer) in `setUp()` method, which always returns `true` for the test duration. E.g.:

.registerExtension(psiDir -> CachedValueProvider.Result.create(true, ModificationTracker.NEVER_CHANGED),

Let me know if you have any questions!

Comment actions Permalink

Hi Piotr,

thank you for your answer. Unfortunately it didn't do the trick for me. I have upgraded to 2021.1 and to my surprise now my .ts files in my unit-tests are considered plain-text. Also, the icon for the Angular HTML file type is missing. Don't know if this might be a bug in the release or something went wrong during my upgrade.

I have tried your package.json example, that one didn't work. The WebFrameworkContext example I couldn't get to work, I'm not sure what I should register, the line in your example isn't working (probably also deprecated) and the interface for registerExtension shows little to no documentation. I do see a lot of references to a PlatformTestUtil, does this mean these tests should not setup using a LightJavaCodeInsightFixtureTestCase but rather a heavier one?

Kr, Tim

Comment actions Permalink

Tim, I am sorry, I haven't payed enough attention to the class you mentioned. Indeed, these tests should be based on `BasePlatformTestCase`. You can see Angular tests here and take the most appropriate as an example: .

Comment actions Permalink

Hi Piotr,

thanks for your response. I have tried it quickly but so far nothing seems to get it working in a test environment. I have created a very simpel setup with 2 tests: 

import com.intellij.lang.javascript.TypeScriptFileType;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import org.angular2.lang.html.Angular2HtmlFileType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class Angular2TSReferenceTest extends BasePlatformTestCase {

protected void setUp() throws Exception {

protected void tearDown() throws Exception {

void testTSFile() {
String content = "@Component({\n" +
" templateUrl: './test.html',\n" +
final PsiFile psiFile = myFixture.configureByText("myFile.ts", content);
assertEquals(TypeScriptFileType.INSTANCE, psiFile.getFileType());

void testHTMLFile() {
String content = "<div (click)=\"domeSomeActionInTheTsComponent();\"></div>";
final PsiFile psiFile = myFixture.configureByText("test.html", content);
assertEquals(Angular2HtmlFileType.INSTANCE, psiFile.getFileType());

Both tests fail on their assertion:


Expected :com.intellij.lang.javascript.TypeScriptFileType@658dcf73
Actual :com.intellij.openapi.fileTypes.PlainTextFileType@22ac8c64


Expected :org.angular2.lang.html.Angular2HtmlFileType@1b2f727a
Actual :com.intellij.ide.highlighter.HtmlFileType@7d291106

In a few days I'll give it another go when I have some more time, perhaps you can have a quick look to see if you can reproduce the above to see if I'm missing something in my setup. The fact that my typescript file is recognized as plain text makes me think that it somehow is running against a community edition.


Please sign in to leave a comment.