Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"no exported member 'mockLogger'" error when mocking the library in TypeScript #139

Open
simonespa opened this issue Oct 25, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@simonespa
Copy link

Context

I'm using this library to send metrics via the metricScope and I need to test the functionality. To do so I'm following the example described here https://github.com/awslabs/aws-embedded-metrics-node/blob/master/examples/testing/tests/module.jest.test.js.

The project is written in TypeScript and I use jest for unit testing.

Issue

This example https://github.com/awslabs/aws-embedded-metrics-node/blob/master/examples/testing/tests/module.jest.test.js fails in a TypeScript context with the following error:

Module '"aws-embedded-metrics"' has no exported member 'mockLogger'.ts(2305)

Question

  • How do I expose the mockLogger to the test function without making TypeScript complain?
  • Is there a different approach to mock the logger and expose it to the test?
@markkuhn markkuhn added the bug Something isn't working label Oct 25, 2022
@simonespa
Copy link
Author

My workaround to make TypeScript happy was to extend the definition of the library by declaring the following:

declare module 'aws-embedded-metrics' {
  const mockLogger: {
    setNamespace: (value: string) => MetricsLogger;
    setDimensions: (dimensionSet: Record<string, string>) => MetricsLogger;
    setProperty: (key: string, value: unknown) => MetricsLogger;
    putMetric: (key: string, value: number, unit?: Unit | string) => MetricsLogger;
  };
}

By doing so I can use the mockLogger object returned by the stubbed library.

@simonespa
Copy link
Author

simonespa commented Oct 27, 2022

I also tried other approaches to pass the "mockLogger" without being part of the aws-embedded-metrics to avoid any TypeScript complaint. Unfortunately, the fact that Jest hoists the jest.mock definition doesn't help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants