m-radzikowski / aws-sdk-client-mock

AWS JavaScript SDK v3 mocks for easy unit testing. 🖋️ Typed 🔬 Tested 📄 Documented 🛠️ Maintained
https://m-radzikowski.github.io/aws-sdk-client-mock/
MIT License
812 stars 40 forks source link

TypeError: Chalk is not a constructor #243

Open EHadoux opened 1 month ago

EHadoux commented 1 month ago

Looks like this line is problematic:

https://github.com/m-radzikowski/aws-sdk-client-mock/blob/3981b97a249a05610f8751d86a5c3428959f0112/packages/aws-sdk-client-mock-jest/src/vitest.ts#L18

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 👍

m-radzikowski commented 1 month ago

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

slcp commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 3 weeks ago

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 commented 2 weeks ago

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.