I have encountered an issue with my Jest test for an AWS CDK configuration
import { expect as expectCDK, matchTemplate, MatchStyle } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import { KmsMultiregionPrincipalKey } from '../lib/kms_multiregion_principalkey-stack';
test('Not Empty Stack', () => {
const app = new cdk.App();
// WHEN
const stack = new KmsMultiregionPrincipalKey(app, 'MyTest_KmsMultiRegionKeyStack');
// THEN
// expectCDK(stack).to(matchTemplate({
// "Resources": {}
// }, MatchStyle.EXACT))
});
The implementation of the KmsMultiregionPrincipalKey class is as follows:
export class KmsMultiregionPrincipalKey extends cdk.Stack {
public readonly principalKeyArn: string;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// REST OF THE CLASS CODE HERE
}
}}
Additional files included in the project are tsconfig.json and jest.config.json:
tsconfig.json
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": [
"es2018"
],
// Additional options here...
},
"exclude": [
"node_modules",
"cdk.out"
]
}
jest.config.json
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
However, upon running the test, I encountered the following error:
"Class constructor Stack cannot be invoked without 'new'"
My question is: What is causing this error in my test or jest/typescript configurations?
To provide more context, when all parts of the test except for the app definition are commented out, the test passes successfully:
PASSING TEST
import { expect as expectCDK, matchTemplate, MatchStyle } from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import { KmsMultiregionPrincipalKey } from '../lib/kms_multiregion_principalkey-stack';
test('Not Empty Stack', () => {
const app = new cdk.App();
// WHEN
//const stack = new KmsMultiregionPrincipalKey(app, 'MyTest_KmsMultiRegionKeyStack');
// THEN
// expectCDK(stack).to(matchTemplate({
// "Resources": {}
// }, MatchStyle.EXACT))
});