Skip to content

TypeError: Chalk is not a constructor #243

@EHadoux

Description

@EHadoux

Looks like this line is problematic:

const chalk = new Chalk({ level: 3 });

aws-sdk-client-mock: 4.1.0
aws-sdk-client-mock-jest: 4.1.0

Replacing with:

import { Instance } from 'chalk';

const chalk = new Instance({ level: 3 });

seems to fix it. I can open a PR if you're happy with this change 👍

Activity

m-radzikowski

m-radzikowski commented on Oct 25, 2024

@m-radzikowski
Owner

Hey, can you provide a repro details when you encounter this issue?

slcp

slcp commented on Oct 25, 2024

@slcp

I saw the same behaviour and I have resolved this issue by installing chalk 5.3.0 (just latest at the time) as a dev dependency of my project (not ideal).

I can see that chalk is installed in the jest matcher package as a devDependency, is that intentional? It isn't being declared as a dependency of the installed package which maybe it should be? And if there is another version of chalk around (I think that was where my issue was) unexpected behaviour like this issue describes might be seen.

travi

travi commented on Oct 25, 2024

@travi

i just ran into this issue as well and agree that making chalk a prod dependency instead of a dev-dependency is the correct fix if the use of chalk is needed there

EHadoux

EHadoux commented on Oct 25, 2024

@EHadoux
Author

Yeah sorry, my initial post could have been better. The solution is indeed to have the latest version of chalk and not to change the code to comply with a previous version as suggested in my initial post.

cawofeso

cawofeso commented on Nov 1, 2024

@cawofeso

I am also facing this problem as well so created a pr to move chalk as a dependency instead of a dev dependency
@m-radzikowski if you could take a look that would be great

alexbaileyuk

alexbaileyuk commented on Nov 11, 2024

@alexbaileyuk

Same issue here today. For now have installed chalk 5.3.0 as a dev dependency in my project. Unfortunately that did not solve the problem for me.

import 'aws-sdk-client-mock-jest/vitest';
import { mockClient } from 'aws-sdk-client-mock';
import { beforeEach, describe, expect, test } from 'vitest';
import { DynamoDBDocumentClient, PutCommand, QueryCommand } from '@aws-sdk/lib-dynamodb';
import { recordCodeSentHistory } from '../../src/login-codes/code-history';

describe('tests', () => {
  const ddbMock = mockClient(DynamoDBDocumentClient);

  beforeEach(() => {
    ddbMock.reset();
  });

  test('it should record login code history in dynamodb', async () => {
    ddbMock.on(PutCommand).resolves({});

    await recordCodeSentHistory('dynamo_table', 'joe@bloggs.com', 2);

    expect(true).toBeTruthy();
  });
});
 FAIL  functions/cognito-create-auth-challenge/test/login-codes/code-history.test.ts [ functions/cognito-create-auth-challenge/test/login-codes/code-history.test.ts ]
TypeError: Chalk is not a constructor
 ❯ node_modules/aws-sdk-client-mock-jest/src/vitest.ts:23:5
 ❯ functions/cognito-create-auth-challenge/test/login-codes/code-history.test.ts:1:1
      1| import 'aws-sdk-client-mock-jest/vitest';
       | ^
      2| import { mockClient } from 'aws-sdk-client-mock';
      3| import { beforeEach, describe, expect, test } from 'vitest';

I've tried importing in different orders and re-installing node_modules with no success.

Would be great to get this PR merged to remove the issue.

bluefeet

bluefeet commented on May 20, 2025

@bluefeet

After running npm i -D chalk to get the latest version installed I found that npm ls chalk still showed v4, so I had to add this to my package.json:

  "overrides": {
    "chalk": "^5.4.1"
  },

Then npm i then npm ls chalk showed v5 and things seem to be working.

Curious, is this library still being maintained? Seeing these open issues sitting around for half a year makes me uneasy.

EHadoux

EHadoux commented on Jun 26, 2025

@EHadoux
Author

Sorry to ping again but can we perhaps get this sorted @m-radzikowski ? The PR looks ready to go #244

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bluefeet@travi@slcp@EHadoux@m-radzikowski

        Issue actions

          TypeError: Chalk is not a constructor · Issue #243 · m-radzikowski/aws-sdk-client-mock