Encountered an issue during the Jest test where the error message states 'Cannot call Class constructor Stack without using the keyword 'new''

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

Answer №1

My solution was to simply update my node version.

node --version
14.x...
nvm install --lst
node --version
16.13.0

Just that small update did the trick for me.

Is it necessary to have node version over 14 for "module": "commonjs"?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display additional javascript code for expanding a marquee

Currently, I am working on a stock ticker project using JavaScript. It's progressing well, and now I am focusing on adding a "show more" button to style the ticker. The button should be placed outside of the marquee. When clicked, it will expand the m ...

Issue with accessing $index.$parent in function parameter within ng-repeat in AngularJS

Can anyone with more experience please explain to me why this particular code compiles successfully: <li class="btn dropdown top-stack breadcrumb-btn" ng-repeat="nodeName in selectedNodeNames"> <a class="dropdown-toggle btn-anchor"> ...

What could be causing my code to lag by 2 ticks instead of just 1?

Apologies for any spacing issues. Player = { move: function(cycle, opponent) { switch(cycle.current_direction) { case 'up': cycle.y -= cycle.height; break; case 'down': cycle.y += cycle.hei ...

I am seeking to redirect to a different page within an ejs template by clicking on a link

I am having trouble navigating to the next page using a link and keep getting a 404 error. I recently switched my template from jade to ejs. <html> <body> <div> <ul style="color:white; float: right;" class="nav navbar-nav ...

The search is on for the elusive object that Angular 2/4

Why am I encountering the error message saying "Cannot find name 'object'"? The error message is: Error: core.service.ts (19,23): Cannot find name 'object'. This error occurs in the following line of code: userChange: Subject<ob ...

Discover the step-by-step guide to creating a dynamic and adaptable gallery layout using

I have encountered an issue with my gallery where the images stack once the window is resized. Is there a way to ensure that the layout remains consistent across all screen sizes? <div class="container"> <div class="gallery"> &l ...

What is the best way to ensure that this JavaScript code functions properly when dealing with business operating hours

Using this javascript code allows me to check if a business is open based on its operating hours. It's effective for times like 17:00-23:00, but how can I modify it to work for hours past midnight, such as 0:30 or 1:00 in the morning? var d = new D ...

What happens if I don't associate a function or method in the React class component?

Take a look at this straightforward code snippet that updates a count using two buttons with different values. import "./App.css"; import React, { Component } from "react"; class App extends React.Component { // Initializing state ...

Ways to ensure that a function operates independently for each cloned div and not the original

I'm facing an issue where a jquery function needs to be executed when the drop-down is changed. However, the problem arises when I clone the div element and make changes in the drop-down of the newly cloned element, the effect takes place in the first ...

Difficulty in detecting state changes without refreshing the component when using Redux and React

I'm encountering difficulties detecting state changes from my Redux reducer in a React application. When I modify the state within one component, the other component in the app does not get the update unless the component is reloaded or refreshed. Let ...

How can I remove unnecessary components from an API result in discord.js before sending it?

The particular API being discussed is a pun-themed one, specifically this one. Here is the code snippet I am currently utilizing: const superagent = require("superagent") module.exports = { run: async(client, message, args) => { const pun = a ...

Using jQuery's .grep() method on an array will only return the final digit

Could someone help me understand the behavior of jQuery's .grep() method? I'm creating a jQuery object array based on the names of these elements: <div class="small1 other">S1</div> <div class="small2">S2</div> <div c ...

Supabase's newly uploaded audio blobs are experiencing difficulties when it comes to

I've integrated Supabase storage for storing audio blobs. After retrieving the blob from an API call, it is uploaded successfully to the storage bucket. However, when attempting to play the audio file, nothing happens and the duration shows as 0:00. S ...

Can you help me transform this javascript code into a JSON format?

Is there a way for me to organize my data so that I have one main question with 5 choices, each associated with a vote? It's like thinking of it as a tree where the question is the root and has 5 leaves, representing the choices, and each choice furth ...

Exploring AngularJS Navigation in Windows 8 App Store Applications

Hello, I recently started learning about developing Windows Store apps. I found a way to incorporate AngularJS into my Windows 8 store app by making some adjustments in the AngularJS library. However, I'm now struggling with implementing AngularJS rou ...

"Encountering a strange behavior in Vue.js: the created() hook

I made a custom feature to refresh my data from the database by clicking a button: <template> <base-projects :projects="projects" /> </template> <script> import { mapGetters } from 'vuex'; import Projects from './ ...

Transformation of firebug console information into a function()

Snippet of JavaScript code: KT_initKeyHandler(b) Firebug console output: KT_initKeyHandler(b=keydown charCode=0, keyCode=90) Corresponding JavaScript function call: KT_initKeyHandler(?) Example: Snippet of JavaScript code: KT_event(b,c) Firebug ...

Transmitting various pieces of information using AJAX

Is it possible to send both "credit_uri" and "address" strings in one AJAX request? Currently, only the second string is being sent. How can I include both in the data of the request? $.ajax({ url: '#{add_cards_path}', type: 'POST&apo ...

Oops! Looks like the connection has been abruptly cut off from the ASYNC node

Is there a way to properly close an async connection once all data has been successfully entered? Despite attempting to end the connection outside of the loop, it seems that the structure is being finalized after the first INSERT operation CODE require( ...

javascript how to trigger a change or input event when handling a paste event

I have successfully overridden the paste event on the document object, but now I want to trigger the onchange, oninput, and other input events in the event handler for the paste event. document.addEventListener('paste', function (e) { ...