ts-nameof icon indicating copy to clipboard operation
ts-nameof copied to clipboard

[Question] Compatibility with jest-preset-angular

Open lonix1 opened this issue 6 years ago • 5 comments

Has anyone used this library in a monorepo project with the jest-preset-angular preset, which configures ts-jest in a way that conflicts with this library.

I tried this which doesn't work (nameof is detected, but then the preset fails and so the tests don't run):

  globals: {
    "ts-jest": {
      tsConfig: '<rootDir>/tsconfig.spec.json',
      stringifyContentPathRegex: '\\.html$',
      astTransformers: [
        "ts-nameof",
        require.resolve("jest-preset-angular/InlineHtmlStripStylesTransformer"),
      ],
    }
  },

In fact is it even possible to use multiple presets or transformers?

lonix1 avatar Mar 29 '19 10:03 lonix1

Hey @dsherret, it appears I'm the only one using this combination of libraries, so I'd like to ask a quick "theoretical" question before I close this.

This library adds to the astTransformers array, and I assume it plays nice with other transformers. So I thought the problem was with the jest-preset-angular library, and asked about it there, though I've confirmed that library works in a multi-transformer setup without problem.

So there's a limitation in ts-jest or ts-nameof that prevents multiple transformers at the same time, or, a config problem (and ts+jest+angular config is notoriously brittle, so I'm guessing it's that).

So in summary: have you ever managed to get your library working together with other ast transformers, at the same time?

lonix1 avatar Apr 02 '19 10:04 lonix1

Based on briefly looking at this, it seems that ts-jest will actually provide its own compiler to the factory function. I didn't find this documentation until now and previously I was looking through the source code to try to figure this out:

https://github.com/kulshekhar/ts-jest/blob/2852078d83cc58eed3986aa2e3e210cd5d306b23/src/transformers/README.md

So it's possible that ts-nameof for jest is using a different version of the compiler. If it were, then the syntax kind values would be different and there would be unexpected errors.

I'll have to make some changes to this in order to use that compilerModule property.

dsherret avatar Apr 02 '19 14:04 dsherret

@lonix1 Actually, this might not be the issue in this case if it works on its own. I'll open up a separate bug for that later. Have you tried changing the order of the transforms? Does using require.resolve for ts-nameof work?

FWIW, I don't see anything wrong in InlineHtmlStripStylesTransformer. Perhaps it's an issue with ts-jest.

dsherret avatar Apr 02 '19 14:04 dsherret

I tried many things, like:

  • changing the order of the transformers
  • using require.resolve
  • using clone and merge routines to merge configs together (idea from the preset repo linked above)
  • swear at, cajole with and pray to the compiler

In all cases the jest-angular stuffs works, but nameof fails. But if I use nameof alone it works brilliantly.

I think you're right it's a problem with ts-jest. Please don't do anything about this as because you don't use angular, so I don't want you to waste your time. I see there are quite a few angular users besides me, so hopefully one of them will join this issue with some ideas!

Should I go ahead and close?

lonix1 avatar Apr 02 '19 17:04 lonix1

The compiler never listens to my prayers either 😞

No, it's fine—keep it open. I'll look into it eventually.

Also, opened #52.

dsherret avatar Apr 08 '19 15:04 dsherret