FluidFramework icon indicating copy to clipboard operation
FluidFramework copied to clipboard

Unit test failing with Jest 28 for packages depending on `@fluid-experimental/property-dds`

Open weiwei opened this issue 2 years ago • 2 comments

Describe the bug

We have packages that rely on @fluid-experimental/property-dds. After upgrading jest to version 28 tests that refers to the package would fail with the following:

    Jest encountered an unexpected token
    [...]
    Details:

    C:\Users\weiwei\code\dp\node_modules\msgpackr\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './pack.js'                                                                                      ^^^^^^
    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (node_modules/@fluid-experimental/property-dds/src/propertyTree.ts:11:1)

Changing jest testEnvironment to node passes the test, but unfortunately we do need jsdom for the app.

To Reproduce

I have created a minimal repo to demonstrate the failure.

Steps to reproduce the behavior:

  1. Go to https://github.com/weiwei/fluidtest. Clone the demo repo.
  2. Run npm install and npm test in both the main branch(jest 28) and jest27 branch.
  3. See the test result.

Expected behavior

Test should pass on jest 28.

Logs

weiwei avatar Aug 19 '22 04:08 weiwei

Do you need to use Jest 28? Or is 27 sufficient for your needs?

tylerbutler avatar Sep 19 '22 15:09 tylerbutler

@tylerbutler The issue seems to be with two of the dependencies of property-dds: msgpackr & uuid, which seems to configure exports incorrectly.
Following a similar solution to the one posted in this article fixes the issue:

export default {
  testEnvironment: "jsdom",
  preset: "ts-jest",
  moduleNameMapper: {
    '^msgpackr':  require.resolve('msgpackr'),
    '^uuid$': require.resolve('uuid')
  },
};

nedalhy avatar Sep 19 '22 15:09 nedalhy

This PR has been automatically marked as stale because it has had no activity for 60 days. It will be closed if no further activity occurs within 8 days of this comment. Thank you for your contributions to Fluid Framework!