Gidday. I'm trying to get mocha+chai working with ...
# typescript
l
Gidday. I'm trying to get mocha+chai working with tests that are not in the same directory as the code/stack under test. I'm perpetually getting
Error: Program run without the Pulumi engine available; re-run using the
pulumi
CLI
I've tried
pulumi login
and I've copied Pulumi.yaml from the source directory (btw it's not documented anywhere that you have to do this in order to use the same project/stack in multiple places - all the docs assume you're creating a new project and stack every time).
I can't see any docs on how to use
setMockOptions
or how to check if I'm in testing mode. I am using the example code from the docs, so I'm calling
setMocks
, which should call
setMockOptions
, but I haven't been able to prove it. I don't think I can debug.. I certainly don't know how (using js/ts/vscode, but a beginner in all of them).
Has anyone got unit tests working with Pulumi from a directory that doesn't have the Pulumi infra code? Are there any extra hoops to jump through?
I've just moved my tests to the source directory: no change 😞 That's not the problem.
w
The guide and example at https://www.pulumi.com/docs/guides/testing/unit/ should hopefully help here. Does that help address what’s needed? If not - anything in specific you are looking for that’s not addressed there?
l
I used that as my template and it's not working.. 😞
Specific question is, how do I get
mocha -r ts-node/register
or
npm test
to run the Pulumi engine? It looks like it's almost doing the right thing, but the mock option testModeEnabled is false... maybe?
My main downfall is that I don't have experience in JS, TS or Mocha...
Interesting.. I've added a
console.log
outputting
pulumi.runtime.isTestModeEnabled()
before and after the call to
pulumi.runtime.setMocks()
and it's going from false to true.. good. But now I'm getting
Error: The root stack resource was referenced before it was initialized.
at Object.registerStackTransformation (/app/node_modules/@pulumi/pulumi/runtime/stack.js21115)
And removing the call to
registerStackTransformation
from my Pulumi code brings me back to
Error: Program run without the Pulumi engine available; re-run using the
pulumi
CLI
😡
Breakthrough. Crosswalk isn't compatible. Example code:
Copy code
describe('Vpc', function () {
  it('should contain tags', function (done) {
    const vpc = new aws.ec2.Vpc("VPC", {
      "cidrBlock": "10.10.10.10/20",
      "tags": { "name": "steve" }
    });
    pulumi.output(vpc.tags).apply((tags) => {
      console.log(tags); // Logged fine.
      done(); // Test passes.
    });
  });
});

describe('VpcX', function () {
  it('should contain tags', function (done) {
    const vpc = new awsx.ec2.Vpc("VPC", {
      "cidrBlock": "10.10.10.10/20",
      "tags": { "name": "steve" }
    });
    pulumi.output(vpc.vpc.tags).apply((tags) => {
      console.log(tags); // Doesn't get logged.
      done(); // Test errors out.
    });
  });
});
(And yes, all my VPCs are called
steve
, it stops them complaining about all the others having better names.)
It must use a different
pulumi.runtime
... I'll raise an issue in pulumi/pulumi-awsx. ¯\_(ツ)_/¯
Turns out the problem was dependencies. Same module (pulumi/pulumi) appeared in a few places in my dependency graph and I think it had a hard time with that.
w
Same module (pulumi/pulumi) appeared in a few places in my dependency graph and I think it had a hard time with that.
Ahh - got it - sorry for the troubles there. We are trying to shift to a model that will avoid this class of problem in the future (more use of
peerDependencies
in our own libraries).
l
Yes I was pointed that way in the ticket. I got it working in my code, then learned that I wasn't using the latest Crosswalk library. I think I'm all caught up now.. sporadic issues in various directories, I'm guessing I need to delete more node_modules folders or something. Thanks for helping!