pkl icon indicating copy to clipboard operation
pkl copied to clipboard

Allow to Mock Resource in testing

Open lucsoft opened this issue 7 months ago • 3 comments

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

lucsoft avatar May 05 '25 12:05 lucsoft

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

StefMa avatar May 05 '25 17:05 StefMa

@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 avatar May 06 '25 10:05 lucsoft

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 avatar May 06 '25 23:05 bioball