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

Jest testing with Lingui and TypeScript #407

Closed
danielkcz opened this issue Dec 2, 2018 · 7 comments
Closed

Jest testing with Lingui and TypeScript #407

danielkcz opened this issue Dec 2, 2018 · 7 comments

Comments

@danielkcz
Copy link
Contributor

I've finally got some time allocated to a catch up on unit tests with our Lingui powered TypeScript app and it turned out to be quite a challenge. I do realize it's not something everyone has experience with, but perhaps some ideas will come out of this.

The issue is that Jest cannot use Webpack configuration to transpile the code. It has it's own ts-jest transform and adding Babel to the mix is supported only for JS files. Currently, I am trying to use "old" transformation Babel plugins as I don't feel that macros are ready for a production yet and I don't like current syntax particularly.

I've prepared a repo with minimal showcase and attempt at a solution based on composing ts-jest & babel-jest together, but for some reason, I am unable to make it work. The TypeScript code gets transpiled, but Lingui plugins won't do its part.

https://github.com/FredyC/ts-jest-lingui

There is also a log file included with an output from both transformations. I am not sure why is it happening.

Also I would like to point out that I don't want to use Babel with TypeScript transform as there are some caveats and I am not willing to accept those.

cc @huan086 @quentez @ghostd
(in case you have some insight here since you made typescript extractor)

@tricoder42
Copy link
Contributor

How does ts-jest work? Does it support Babel plugins or it transform typescript and then let Babel plugins do their part?

@danielkcz
Copy link
Contributor Author

Yes, it's fairly similar. I've decided to make my own transform like that because of the transparency of the config otherwise it's rather entangled code in ts-jest.

ts-jest using babel
my transform using babel

There seems to be something odd happening on Lingui plugin side as it won't recognize that i18n.t call and leaves it as it is.

@danielkcz
Copy link
Contributor Author

danielkcz commented Dec 8, 2018

@tricoder42 Do you have possibly any ideas? It's starting to be fairly limiting as I am getting warning in tests which ends up with no text to be rendered :/

@lingui/babel-preset-react is probably missing in babel config, but you are using < Trans > component in a way which requires it.

Do you possibly see any reason why the following output coming from Typescript won't be correctly handled by Lingui Babel plugins in the next step? In both tests it just stays the exactly same.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const react_testing_library_1 = require("react-testing-library");
const i18n_1 = require("./i18n");
const react_2 = require("@lingui/react");
test('using i18n.t', () => {
    const message = i18n_1.i18n.t `Test message`;
    expect(message).toBe("Translated message");
});
test('using Trans', () => {
    const { container } = react_testing_library_1.render(<react_2.Trans>Test message</react_2.Trans>);
    expect(container.textContent).toBe("Translated message");
});

@danielkcz
Copy link
Contributor Author

Ok, I think I got some lead. The plugin needs ESM syntax, right? Well, that turned out to be an issue as ts-jest is forcing compilation to CommonJS because Jest doesn't understand anything else. I created an issue if there some way around that, but after some digging I don't think it would be possible.

So I am wondering, since Macros are still a long way to go, how difficult or if even possible would be to modify plugin to work with CommonJS as well? I can understand that you don't have a capacity for that, so I might give it a try, but I just would like to hear from you some assessment on complexity of such change.

@evdama
Copy link

evdama commented Apr 2, 2019

is there a resolution to this issue now?

@danielkcz
Copy link
Contributor Author

Get rid of ts-jest :D I just switched to Babel Typescript and don't worry about it anymore.

@kopax-polyconseil
Copy link

How do you get ride of ts-jest ? That's the only way for I to get the test running.

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

4 participants