inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.21k stars 715 forks source link

getDecorators is not a function #1509

Open linonetwo opened 1 year ago

linonetwo commented 1 year ago

Expected Behavior

Work in esm env

Current Behavior

console.log
    { default: [Function: getDecorators] }

This means

import { Container } from 'inversify';
import getDecorators from 'inversify-inject-decorators';
import 'reflect-metadata';

export const container = new Container();
export const { lazyInject } = getDecorators(container);

have to change to

export const { lazyInject } = getDecorators.default(container);

Possible Solution

maybe its jsnext:main is not pickup by jest.

Change package.json of this package to "main": "es/index.js", can solve this

and change them to mjs

截屏2023-05-17 16 24 59

Steps to Reproduce (for bugs)

I set up ts jest using

    "test": "NODE_OPTIONS=--experimental-vm-modules jest -c jest.config.ts"

jest.config.ts

import type { JestConfigWithTsJest } from 'ts-jest';

const jestConfig: JestConfigWithTsJest = {
  preset: 'ts-jest/presets/js-with-ts-esm',
  testEnvironment: 'node',
  extensionsToTreatAsEsm: ['.ts'],
  globals: {
    'ts-jest': {
      useESM: true,
      isolatedModules: true,
    },
  },
  roots: ['<rootDir>/src/', '<rootDir>/tests/'],
};

export default jestConfig;

Context

Maybe it is time to provide a esm version of that package, without feature change.

Your Environment

    "inversify": "^6.0.1",
    "inversify-inject-decorators": "^3.1.0",
    "reflect-metadata": "^0.1.13"
 node -v                       
v18.12.1

Stack trace

  ● Test suite failed to run

    TypeError: getDecorators is not a function

      4 |
      5 | export const container = new Container();
    > 6 | export const { lazyInject } = getDecorators(container);
        |                               ^
      7 |

      at src/iocContainer/container.ts:6:31