Allow to Mock Resource in testing #308

Open
opened 2025-12-30 01:23:22 +01:00 by adam · 3 comments
Owner

Originally created by @lucsoft on GitHub (May 5, 2025).

i would to mock read's like read("env:MY_CUSTOM_ENV") for a specific test case/(or test file at least)

Context: I have a Global Config object which reads env variables and i would like to define them in a test file.

Mocking Resources could be a way i think

Originally created by @lucsoft on GitHub (May 5, 2025). i would to mock read's like `read("env:MY_CUSTOM_ENV")` for a specific test case/(or test file at least) Context: I have a Global Config object which reads env variables and i would like to define them in a test file. Mocking Resources could be a way i think
Author
Owner

@StefMa commented on GitHub (May 5, 2025):

You might wanna check this discussion that provides a solution: https://github.com/apple/pkl/discussions/1032

@StefMa commented on GitHub (May 5, 2025): You might wanna check this discussion that provides a solution: https://github.com/apple/pkl/discussions/1032
Author
Owner

@lucsoft commented on GitHub (May 6, 2025):

@StefMa i don't think that it actually solves my issue as this is local mocking and not global

i have a global config file, and i want to test a specific file which imports the config file
i don't think i can currently mock the response of that "internal" import of that config file

@lucsoft commented on GitHub (May 6, 2025): @StefMa i don't think that it actually solves my issue as this is local mocking and not global i have a global config file, and i want to test a specific file which imports the config file i don't think i can currently mock the response of that "internal" import of that config file
Author
Owner

@bioball commented on GitHub (May 7, 2025):

I have the same advice that I did in the discussion that @StefMa linked to.

The best way to stub out that value is to expose it as an API, and replace it in your tests.

If you really want to, you can set env vars when running pkl test, e.g. run shell command FOO=1 pkl test

@bioball commented on GitHub (May 7, 2025): I have the same advice that I did in the discussion that @StefMa linked to. The best way to stub out that value is to expose it as an API, and replace it in your tests. If you really want to, you can set env vars when running `pkl test`, e.g. run shell command `FOO=1 pkl test`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#308