简单的 CDK 测试失败

Simple CDK Testing Failing

这是我的设置 //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');

//库

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)
    });
  }
}

//测试

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-包

  "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"
    ]
  }

我在 运行: npm 运行 build 时得到了这个; npm 运行 测试。 None of 0 resources matches resource 'AWS::SQS::Queue' with { "$anything": true }.

我没看懂??? 这应该是直截了当的。我可以在 cdk.out、堆栈 synthesez、堆栈部署中看到资源。

它只发生在细粒度的断言中。快照有效。

您是在空 stack 上断言。改为断言 Testing

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
});