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

Question - how to make ts-jest happy at test runtime? #132

Closed
stkiller opened this issue Jul 26, 2021 · 1 comment
Closed

Question - how to make ts-jest happy at test runtime? #132

stkiller opened this issue Jul 26, 2021 · 1 comment

Comments

@stkiller
Copy link

Hello there!

I'm struggling with setting up unit tests that depend on Screeps types.

For example, when trying to extend Creeps like this:

Creep.prototype.someFunction = ...

My IDE is happy, the compiled code runs in game just fine, all types are resolved. But, when executing a unit test that imports the above code, it always fails with

ReferenceError: Creep is not defined

How to teach ts-jest to look at the right place and resolve Creep and other types located in node_modules/@types/screeps/index.d.ts ?

Thank you in advance!

@stkiller stkiller changed the title Question - how to make ts-jest happy at runtime? Question - how to make ts-jest happy at test runtime? Jul 26, 2021
@stkiller
Copy link
Author

Below is based on my own understanding, take with a grain of salt.

Anything found in *.d.ts is just a declaration to help your IDE and TypeScript figure out if the code is wrong. None of these types are available at runtime.

With Screeps, where the majority of implementations are provided at runtime, testing is tough-er than usual - you need to mock everything. There are good starting projects, though.
https://github.com/eduter/screeps-typescript-jest-starter
https://github.com/eduter/screeps-jest

Take a look at https://github.com/eduter/screeps-jest/blob/master/src/setupGlobals.ts#L1601

Specifically, everything added to global before the start of the test will be available at test runtime.

Closing this issue

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

1 participant