miniflare icon indicating copy to clipboard operation
miniflare copied to clipboard

Unexpected token 'export' when using `@aws-sdk` with Jest

Open swelham opened this issue 3 years ago • 9 comments

I have created a new worker using npx wrangler init with typescript. I am using the @aws-sdk/client-eventbridge package in my worker. However, when I try and mock it in tests using aws-sdk-client-mock I get the following error.

.../node_modules/uuid/dist/esm-browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';

This appears to be an ES module issue and only occurs when I have new EventBridgeClient({}) in my test code.

My jest config looks like this.

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'miniflare',
};

If I change testEnvironment to node, the error no longer occurs and the test will pass. However, I loose the miniflare benefits in jest.

Is there something I'm missing here to get this working with the miniflare test environment?

swelham avatar May 27 '22 13:05 swelham

Hey! 👋 Which version of jest and jest-environment-miniflare are you using? It's possible upgrading to jest@28 and [email protected] might fix this issue.

mrbbot avatar Jun 09 '22 10:06 mrbbot

Hey. I'm on [email protected] and [email protected] and still seeing the above issue.

Interestingly I have been able to work around it by mocking the uuid library in tests like this.

jest.mock('uuid', () => 'eb7b7961-395d-4b4c-afc6-9ebcadaf0150');

Not ideal, but helps me out in my case. Though this would become an issue I guess if I wanted to directly use uuid in my worker without having to supply a mock.

swelham avatar Jun 09 '22 12:06 swelham

same issue

thienna avatar Jun 15 '22 04:06 thienna

same issue

tmizuma avatar Jul 15 '22 12:07 tmizuma

same issue, has anyone found a solution?

esther44 avatar Jul 19 '22 09:07 esther44

Same issue

cyberdude avatar Aug 08 '22 16:08 cyberdude

Same

andreMycroft avatar Aug 09 '22 14:08 andreMycroft

https://github.com/microsoft/accessibility-insights-web/pull/5421#issuecomment-1109168149/

This solve my problem create a resolver. the uuid has a incompatibility with jest.

andreMycroft avatar Aug 09 '22 20:08 andreMycroft

What is the solution for this issue ? i am on jest 29.0 and have the same issue.

maxy4u avatar Sep 21 '22 12:09 maxy4u

@maxy4u as @andreMycroft mentioned, the resolver. He could be a little bit more specific I guess 😝 : https://github.com/microsoft/accessibility-insights-web/pull/5421/commits/9ad4e618019298d82732d49d00aafb846fb6bac7

mkbctrl avatar Sep 22 '22 21:09 mkbctrl

@maxy4u as @andreMycroft mentioned, the resolver. He could be a little bit more specific I guess 😝 : microsoft/accessibility-insights-web@9ad4e61

Thank you @mkbctrl I will try this. :)

maxy4u avatar Sep 23 '22 11:09 maxy4u