Hi! I am trying to write a simple unit test for my...
# typescript
n
Hi! I am trying to write a simple unit test for my ts project. And I am stuck with configuration parsing. Maybe some of you already faced such problem and can help. me somehow? Here is my test:
Copy code
import { NodeApplication } from "../src/nodeApplication";
import * as pulumi from "@pulumi/pulumi";

describe("Node Application", () => {
  test("Read from config", () => {
    const config = JSON.stringify({
      config: {
        'test:image': 'test',
        'test:team': 'test',
        'test:app': {
          redis: {
            url: '<redis://some-host:6379>',
          },
        },
      },
    });
    process.env.PULUMI_NODEJS_STACK = "unit";
    process.env.PULUMI_NODEJS_PROJECT = "test"
    process.env.PULUMI_CONFIG = config;
    console.log(process.env.PULUMI_CONFIG);
    console.log(pulumi.getProject()); // returns 'project' instead of 'test'
    console.log(pulumi.getStack()); // returns 'stack' instead of 'unit'
    const appConfig = new pulumi.Config();
    console.log(appConfig);
    console.log(process.env.PULUMI_CONFIG);
    let app = new NodeApplication(); // fails to find required configuration values
  });
});
Inside
NodeApplication
class there is a config parsing:
Copy code
. . .
let config = new pulumi.Config();
this.name = pulumi.getProject();
this.image = config.require('image');
this.team = config.require('team');
. . .
I am running tests with jest. And here is an output (simplified):
Copy code
$ npm run test

> test
> jest

  console.log
    {"config":{"test:image":"test","test:team":"test","test:app":{"redis":{"url":"<redis://some-host:6379>"}}}}

  console.log
    project

  console.log
    stack

  console.log
    Config { name: 'project' }

 FAIL  test/redis.test.ts (19.335 s)
  Node Application
    ✕ Read from config (59 ms)

  ● Node Application › Read from config

    Missing required configuration variable 'project:image'
    	please set a value using the command `pulumi config set project:image <value>`


Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        19.505 s
Ran all test suites.
l
Wouldn't unit tests for Pulumi config be the responsibility of Pulumi? You shouldn't need to write unit tests that use Pulumi config.
If you have code that you want to test as a unit, and that code depends on getting values from Pulumi config, then you can (and absolutely should) make it unit testable by not getting values from Pulumi config in that code.
The code that gets values from Pulumi config should be your top-level project only. This is not a testable unit, so you won't be writing unit tests for it.
This code will pass config values into units (resources, functions, etc.). Those resources and functions will not be directly using Pulumi config.
n
Yeah. This code can also take values as an arguments. Without depending on Pulumi config. I thought, it would be useful to test cases when I’m using some values from Pulumi Comfig instead of arguments