Skip to content

Cannot convert undefined or null to object #8

@rtrembecky

Description

@rtrembecky

Hi!
I'm very surprised this barrel files problem is being fixed only now, but rather later than never... However, I encounter multiple issues. Seeing you're active on the issues here and fix the bugs fairly quickly, I have high hopes it could help us on a project.

Let me start with the first issue I encountered: TypeError: /Users/richard/repos/xxx/node_modules/@react-native-google-signin/google-signin/jest/build/jest/setup.js: Cannot convert undefined or null to object

I "solved" this by skipping node_modules in babelTransform.js:

      plugins: sourcePath.includes('node_modules')
        ? []
        : [
            [
              'transform-barrels',
              {
                executorName: 'jest',
                alias: alias,
                extensions: extensions,
                logging: { type: 'screen' },
              },
            ],
          ],

Note: babel-jest-boost as your counterpart has ignoreNodeModules option: https://github.com/gtsop/babel-jest-boost?tab=readme-ov-file#ignorenodemodules-boolean
It would be cool to see something similar here eventually.

However, it just allowed babel-jest to process more files, only to quickly crash again: TypeError: /Users/richard/repos/xxx/test-setup.mobile.tsx: Cannot convert undefined or null to object

Short stacktrace:

      at Function.createASTJestMockCallFunction (../../node_modules/babel-plugin-transform-barrels/src/ast.js:62:30)
      at PluginPass.call (../../node_modules/babel-plugin-transform-barrels/src/main.js:53:36)
      at call (../../node_modules/@babel/traverse/src/visitors.ts:303:14)

Logs:

2024-11-05 19:34:11 - Processed Javascript file: /Users/richard/repos/xxx/test-setup.mobile.tsx
2024-11-05 19:34:11 - Source import line: import mockSafeAreaContext from 'react-native-safe-area-context/jest/mock';

Isolated minimal crashing file:

//@ts-expect-error not typed
import mockSafeAreaContext from 'react-native-safe-area-context/jest/mock';

jest.mock('react-native-safe-area-context', () => mockSafeAreaContext);

This is the suggested way to mock this package: https://github.com/th3rdwave/react-native-safe-area-context?tab=readme-ov-file#testing
I've also tried commenting these lines out, only to have it fail on other similar code. This is also mocked as of package guide:

jest.mock(
  'react-native-fbsdk-next',
  // eslint-disable-next-line @typescript-eslint/no-var-requires
  () => require('react-native-fbsdk-next/jest/mocks').default
);

Looking here, as of the stacktrace, the specifiersByModule is undefined: https://github.com/FogelAI/babel-plugin-transform-barrels/blob/main/src/ast.js#L62
I provided empty object {} as the default in your code. Moving on...

Whether I make the above adjustment or I comment out the problematic lines, I get to: TypeError: /Users/richard/repos/xxx/apps/mobile-expo/src/components/AppLoad.spec.tsx: Cannot read properties of undefined (reading 'absEsmFile'

Stacktrace:

      at PluginPass.call (../../node_modules/babel-plugin-transform-barrels/src/main.js:19:79)
      at call (../../node_modules/@babel/traverse/src/visitors.ts:303:14)
      at NodePath.call [as _call] (../../node_modules/@babel/traverse/src/path/context.ts:36:20)

We are using Nx monorepo and in jest.config.ts we have resolver: '@nx/jest/plugins/resolver' defined. I just quickly debugged your resolver, the arguments for const absolutePath = PathFunctions.getAbsolutePath(originalPath, fromDir, this.modulesDirs) are @xxx/mobile/core, /Users/richard/repos/xxx/apps/mobile-expo/src/components, [ 'node_modules' ], the absolute path returned is null. Not sure if this has something to do with path aliases and whether there should be a way to combine the resolvers, or I just need to make manually sure the aliases are provided...

That's it for now. Any help? Can you make the code type-safer, so e.g. absEsmFile is never accessed on null object, also regarding the undefined specifiersByModule before? And advice on the resolver issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions