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

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

Open
Ginxo opened this issue Jul 11, 2024 · 1 comment

Comments

@Ginxo
Copy link

Ginxo commented Jul 11, 2024

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
Copy link
Author

Ginxo commented Jul 29, 2024

any update on this? @ortexx @zyoshoka

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant