https://pulumi.com logo
Title
l

lively-oil-7382

05/15/2020, 2:40 AM
I am trying to registerAutoTags to aws resources and getting error while running unit tests (npm test). Error: The root stack resource was referenced before it was initialized. Any pointer on what might be causing this issue?
l

little-cartoon-10569

05/15/2020, 3:29 AM
I was just about to ask the same thing.. I've tried everything I can think of, short of duplicating the registerStackTransformation code in setMocks.call....
In fact I asked the question a few days ago before I got distracted... https://pulumi-community.slack.com/archives/CJ909TL6P/p1589237046248300
The error seems to be pretty accurate; I can get registerStackTransformation to not cause the error by running it inside of
@pulumi/pulumi/runtime/stack/runInPulumiStack
, but then the resources I create aren't seen by my stack transformation (obviously).
If I put my entire test inside
runInPulumiStack
, it all works. I feel that shouldn't be necessary, but it'll do for now, I guess...
My code now looks something like this:
import { runInPulumiStack } from "@pulumi/pulumi/runtime/stack";

runInPulumiStack(() => {
  pulumi.runtime.registerStackTransformation(myStackTransformation);
  describe('test stuff', function() {
     ...
  });
  return new Promise<void>(() => {});
});
Which seems like a lot of boilerplate... 😞
(I'm new to Javascript / Typescript; gotta say that figuring this stuff out is a lot easier than it would be in C# / Kotlin...)
l

lively-oil-7382

05/15/2020, 2:43 PM
@little-cartoon-10569 thanks for your answers, but the problem i am facing is i am getting this error caused by function call i am making from index.ts and not from a unit test code
@little-cartoon-10569 i looked at your issue https://pulumi-community.slack.com/archives/CJ909TL6P/p1589237046248300 and it is very similar to my issue, only difference is i have defined registerAutoTags with in the same module in a file e.g. Tags.ts inside src directory, but still getting the error Error: The root stack resource was referenced before it was initialized.
@little-cartoon-10569 you mentioned that you were able to get it run by directly declaring registerAutoTags in index.js, but even that is not working for me. any pointers how you got it work?
l

little-cartoon-10569

05/15/2020, 8:42 PM
I ran
registerAutoTags
and all the tests inside the same
runInPulumiStack
callback. It doesn't have to be in index.js, but it does have to be called from inside the function that's passed as a parameter to
runInPulumiStack
.
The code above shows how it works.
Did you get it to work, @lively-oil-7382?
l

lively-oil-7382

05/18/2020, 8:36 PM
yes, i was able to run it from a index-test.ts file with your code snippet.
import { runInPulumiStack } from "@pulumi/pulumi/runtime/stack";

runInPulumiStack(() => {
  pulumi.runtime.registerStackTransformation(myStackTransformation);
  describe('test stuff', function() {
     ...
  });
  return new Promise<void>(() => {});
});
👍 1
l

little-cartoon-10569

06/09/2020, 8:47 PM
Looks like this problem has been noticed: https://github.com/pulumi/pulumi/issues/4669