https://pulumi.com logo
#typescript
Title
# typescript
b

blue-translator-21668

02/28/2024, 3:59 PM
Is there a known requirement that if you are mocking pulumi resources for unit testing that your pulumi CLI version needs to be the same as the SDK version? I get weird errors when they aren’t the same, but it makes it hard to maintain consistent versions across multiple projects.
l

little-cartoon-10569

02/28/2024, 6:49 PM
When you're unit testing, the CLI isn't used, so it doesn't matter. You don't even need it to be installed.
Uh... I think. Now I'm having sudden pangs of uncertainty about this...
b

blue-translator-21668

02/28/2024, 6:50 PM
lol I agree in theory but the only fix I’ve found is installing the same CLI version as the @pulumi/pulumi version in package.json 😕
l

little-cartoon-10569

02/28/2024, 6:54 PM
I don't know how to easily uninstall Pulumi, so I renamed the
~/.pulumi/bin/pulumi
file. My tests all pass 100%.
b

blue-translator-21668

02/28/2024, 6:54 PM
hm
l

little-cartoon-10569

02/28/2024, 6:55 PM
I'll rename a few more things... maybe the language engine is used directly.
I've renamed ~/.pulumi/bin, and tests all still pass.
I've renamed ~/.pulumi/plugins, still passign. Looks like the CLI isn't used at all. Have you tracked down the code with the error? Maybe there's a particular call that invokes the CLI inside one of your resources? If there is, you can move that out of the tested code, and into your index.ts or similar.
For example, I've learned never to call a function in the
pulumi
namespace from tested code. E.g.
pulumi.getProject()
,
pulumi.isDryRun()
, anything like that. Those are not handled by the Pulumi test engine.
So I always call those from index.ts and pass the values around.
b

blue-translator-21668

02/28/2024, 7:11 PM
ah, that’s definitely in my code. We grab pulumi stack config in the tested function
l

little-cartoon-10569

02/28/2024, 7:12 PM
We grab config in index.ts and pass the relevant bits into component constructors. Inversion of control, and all that.
It also means we don't need to stub out config: tests can pass in values, the same as config does.