-
-
Notifications
You must be signed in to change notification settings - Fork 163
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
Question: Jest + Typescript + threads.js how can it work together ? #269
Comments
Sorry, I forgot to mention that my project is in vue and electron but it is separate from the tests. (Jest works without any electron or vue dependency (and threads.js works with electron+vue (thanks to webpack) but it cant get compiled in jest)) Jest only tests the parts of my program that are only related to typescript, everything else is getting mocked. So puppeter will not help me in this case. When i run the jest tests its like running plain typescript. (ts-node) But i dont know how threads.js works without beeing compiled to js. |
You would just use I guess either that mechanic doesn't work in your case for some reason or maybe your TypeScript config (might be a different one for Jest than when running without Jest?) has Or seen from a different angle: Do you use ES modules in node.js? (I just assumed you would use CommonJS modules) |
esnext
CommonJS modules Jest and Electron uses the same tsconfig: tsconfig.ts:
|
Then that's why it doesn't work: You live in a CommonJS world, but tell the TypeScript compiler to compile to ES modules. Don't know if Jest does some magic on top of that, so that your master thread code runs in the first place, but it cannot work like this. There is one thing that needs to be fixed in threads.js, though: If you are using ES modules in node.js, you should be able to spawn a TypeScript worker ES module. I think this will break right now in the way that you showed as soon as ES modules and automatic |
Is there a solution to this? I am on the same problem right now |
I'm having exactly the same issue with jest + ts-jest + ts-config... I can't really configure it so that it can import ts file for webworker... |
Just ran into the same issue, typescript workers don't play well with jest. |
Same problem for me, is documentation available? |
Sorry, got no solution for Jest ready yet. If anyone got an idea, please share! |
I got it to work eventually quite a while ago and the thing that did the trick was to add this to tsconfig.json:
|
For anyone having this issue, I haven't found a true solution but I was able to get my other tests running by conditionally importing any modules using threads at runtime based on an environment variable. This let me exclude it in the tests. I also found that setting |
I faced same "SyntaxError: Cannot use import statement outside a modul" when running on Jest, but I was able solve it with Jest uses ts-jest for transpile while thread.js wants ts-node, so you need to install that in addition (ts-jest won't transpile files for thread.js, only ts-node suppored as mentioned above ) - maybe that's what is causing the confusion. |
Using ts-node slows down running my tests to 3x. So is there another way? |
It seems that const { expose } = require("threads/worker");
const json_parse = require("./parse");
expose(json_parse({ storeAsString: true })); |
I have implemented threads.js in my "vue + typescript + electron" project and it works, but my test now fails with the error:
"worker.ts":
"main":
"test":
(You can turn Series to a object with just a id for reproducing):
{id:''}
JestConfig:
The text was updated successfully, but these errors were encountered: