Testing - Not detecting that is running a test
# typescript
b
Testing - Not detecting that is running a test
Here’s my code, I’m using
jest
but same happens with
mocha
so it’s not the testing framework.
Somehow
pulumi
is requiring my configuration values..
Is there any way to avoid this issue?
I’m using the
config.require('somevalue')
in some places of my code. to inject configuration from the
.yaml
files, however I don’t want this behavior in unit testing. But even initializing the
setMocks
function with the
project
and
stack
arguments, it doesn’t get the values from the
yaml
files. 😕
b
Hopefully none of my colleagues are going to correct me, but it's still running your pulumi program, just without hitting the provider's api. So if you've got config somewhere in there, it means you'll need to have that config value in your stack config
👍 1
b
But I have the config in there, maybe because it’s running from a test is not detecting it?
f
h
So the only solution is either providing environment variables with the
config
values to the test, or read the
yaml
config into
json
and export it as a variable? 😕
f
Correct, if you want to test with a specific config, then those are currently the primary options. One thing I would ask is what are you testing w.r.t. reading from config since config is generally stack-specific and I would typically expect the unit test to be testing the program itself and not relying on any specific config values?
b
Yeah I think i’m just going to mock the required configuration and that’s it 🙂
I would like to just take from
index.ts
a single constant, like my
resourceGroup
for azure, but this code actually makes it so I cannot do it:
Copy code
let infra: typeof import('../index');

  beforeAll(async function () {
    infra = await import('../index');
  });
If I try to adapt it slightly to just take the exported constant it doesn’t work. 😕 Also when I’m trying to import
nested
config objects I get this error:
Copy code
Configuration 'project:services' value '[object Object]' is not a valid JSON object
Example of this
services
object:
Copy code
project:services:
   myapp1Version: 23
   myapp2Version: 24
JSON string:
Copy code
{
  "project:baseDomain": "mydomain.dev",
  "project:services": {
    "myapp1Version": 23,
    "myapp2Version": 24
  }
}
Isn’t there a better way to inject configuration? Some
pulumi.runtime
method or something?