question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(core): Can't mock Stack with jest

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
martzcodescommented, Feb 12, 2021

🤔 hm… yeah i had a typo when i did that last night: jest.mock("aws-cdk/aws-s3"); should’ve been jest.mock("@aws-cdk/aws-s3"); which DOES pass… interesting

import { Bucket } from '@aws-cdk/aws-s3';
jest.mock("@aws-cdk/aws-s3");
const mockBucket = (Bucket as unknown) as jest.Mock<Stack>;

class ExtendedBucket extends Bucket {
  constructor(scope: Construct, id: string) {
    super(scope, id);
  }
}

test("Calls super with correct params", () => {
    const app = new App();
    // when
    new ExtendedBucket(app, `Temp Bucket`);
    // then
    expect(mockBucket).toHaveBeenCalledWith(app, "Temp Bucket");
    expect(mockBucket).toBeCalledTimes(1);
  });

Very interesting… I’d never tried that before. I’m interested to hear what the cdk team says about it.

0reactions
github-actions[bot]commented, Jun 3, 2022

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found