ip-cidr icon indicating copy to clipboard operation
ip-cidr copied to clipboard

jest + 4.0.1 version issues. SyntaxError: Cannot use import statement outside a module

Open Ginxo opened this issue 7 months ago • 1 comments

After upgrading the library to version 4.0.1 there is an issue on our jest execution.

  • in case nothing is done or apply on our side
    SyntaxError: Cannot use import statement outside a module

    > 3 | import IPCIDR from 'ip-cidr';
  • in case library is added to transformIgnorePatterns, like transformIgnorePatterns: ['<rootDir>/node_modules/(?!(ip-cidr)/)',], the Address4 is not found due to import ipAddress from 'ip-address'; is a problem
    TypeError: Cannot read properties of undefined (reading 'Address4')

      833 |   const erroredSubnets: ErroredSubnet[] = [];
      834 |
    > 835 |   const startingIP = (cidr: string) => new IPCIDR(cidr).start().toString();
          |                                        ^
      836 |
      837 |   const compareCidrs = (shouldInclude: boolean) => {
      838 |     if (shouldInclude) {

      at Function.Address4 [as createAddress] (node_modules/ip-cidr/index.js:179:71)
      at new createAddress (node_modules/ip-cidr/index.js:11:38)
  • in case dist file is used from moduleNameMapper like '^ip-cidr$': '<rootDir>/node_modules/ip-cidr/dist/ip-cidr.js',
    TypeError: ip_cidr_1.default is not a constructor

      833 |   const erroredSubnets: ErroredSubnet[] = [];
      834 |
    > 835 |   const startingIP = (cidr: string) => new IPCIDR(cidr).start().toString();

to mock the library was the only solution I found for being able to upgrade, like

class IPCIDR {
  address: string;

  constructor(address: string) {
    this.address = address;
  }

  start = () => this.address.split('/')[0];
}

export default IPCIDR;

and then '^ip-cidr$': '<rootDir>/__mocks__/ip-cidr-mock.ts', added to moduleNameMapper but this is not the right solution and could be very problematic in the future.

I guess library should be adapted to properly import ip-address library, right?

Ginxo avatar Jul 11 '24 07:07 Ginxo