Home > Enterprise >  Simple CDK Testing Failing
Simple CDK Testing Failing

Time:12-18

this is my set up //bin

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import {Testing} from '../lib/index';

const app = new cdk.App();
new Testing(app, 'Testing');

//lib

import {Duration, Stack, StackProps} from 'aws-cdk-lib'
export class Testing extends Stack {

  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // Define construct contents here

    // example resource
    const queue = new sqs.Queue(this, 'TestingQueue', {
      visibilityTimeout: Duration.seconds(300)
    });
  }
}

//test

import {Stack} from 'aws-cdk-lib/core';
import sqs = require ('../lib/index');
import'@aws-cdk/assert/jest'
test('SQS Queue Created', () => {
    const stack = new Stack();
    new sqs.Testing(stack, 'sqs');
    expect(stack).toHaveResource('AWS::SQS::Queue')
});

//npm-package

  "devDependencies": {
    "@types/jest": "^26.0.10",
    "@types/node": "10.17.27",
    "aws-cdk-lib": "2.1.0",
    "constructs": "^10.0.0",
    "jest": "^26.4.2",
    "ts-jest": "^26.2.0",
    "typescript": "~3.9.7"
  },
  "peerDependencies": {
    "@aws-cdk/assert": "^2.1.0",
    "aws-cdk-lib": "2.1.0",
    "constructs": "^10.0.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js"
    ]
  }

I get this when I run: npm run build; npm run test. None of 0 resources matches resource 'AWS::SQS::Queue' with { "$anything": true }.

I don't understand??? This should be straight forward. I can see the resource in cdk.out, the stack synthesisez, the stack deploys.

It only happens with fine grained assertions. The snapshot works.

CodePudding user response:

You are asserting on the empty stack. Assert on Testing instead.

test('SQS Queue Created', () => {
    const stack = new Stack();  // stack is empty, has no queue
    const iHaveAQueue = new sqs.Testing(stack, 'sqs');
    expect(stack).toHaveResource('AWS::SQS::Queue')  // will fail
    
    expect(iHaveAQueue).toHaveResource('AWS::SQS::Queue') // will pass
});
  • Related