(core): Can't mock Stack with jest
See original GitHub issueWhen attempting to mock Stack with jest, a TypeError “Class extends value undefined is not a constructor or null” is thrown. I need to be able to mock the stack to test that the an inheriting class is calling super() parameters correctly.
Reproduction Steps
import { Stack } from "@aws-cdk/core";
import "@aws-cdk/assert/jest"; // <-- tried with and without this line
test("Can mock stack", () => {
console.log(Stack);
});
jest.mock("@aws-cdk/core");
What did you expect to happen?
When I ran jest test
I expected it not to fail with error. Ultimately it will be a call to an inheriting class type which calls the Stack mock. I’m able to do this with other classes such as Bucket.
What actually happened?
TypeError: Class extends value undefined is not a constructor or null
> 1 | import { Stack } from "@aws-cdk/core"; (Line 1:1)
Environment
- CDK CLI Version : 1.89.0
- Framework Version: 1.89.0
- Node.js Version: 14.15.5
- OS : Windows 10.1909
- Language (Version): Typescript 4.1.4
Other
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Jest not using my mocks but is for functions imported via ...
I have a Jest test which is building a redux component, part of that component calls a number of sagas which in turn...
Read more >Manual Mocks - Jest
In order to mock properly, Jest needs jest. mock('moduleName') to be in the same scope as the require/import statement. Here's a contrived ...
Read more >Testing constructs - AWS Cloud Development Kit (AWS CDK) v2
The standard approach to testing AWS CDK apps uses the AWS CDK's assertions module and popular test frameworks like Jest for TypeScript and...
Read more >"jest" task | Rush Stack
The ts-jest transform will magically "hoist" calls to jest.mock(); . Heft does not consider this a best practice. Instead, use the @rushstack/hoist-jest-mock ......
Read more >Understanding Jest Mocks - Medium
Learn about the Jest Mock Function and the different strategies for creating ... Often that is not the case, so we will need...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
🤔 hm… yeah i had a typo when i did that last night:
jest.mock("aws-cdk/aws-s3");
should’ve beenjest.mock("@aws-cdk/aws-s3");
which DOES pass… interestingVery interesting… I’d never tried that before. I’m interested to hear what the cdk team says about it.
This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.