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

v3.0.0 + TypeScript Cannot find module 'redux-actions' from ... #390

Open
kaelig opened this issue Mar 2, 2023 · 12 comments
Open

v3.0.0 + TypeScript Cannot find module 'redux-actions' from ... #390

kaelig opened this issue Mar 2, 2023 · 12 comments

Comments

@kaelig
Copy link

kaelig commented Mar 2, 2023

The new ESM version fails to be imported by TypeScript.

Here's a reproducible comparison:

In the shell, run:

tsc index.ts
  • v2.6.5: compiles successfully
  • v3.0.0:
~/ReduxActionsV3BugReportTS$ tsc index.ts
index.ts:1:30 - error TS2307: Cannot find module 'redux-actions' or its corresponding type declarations.

1 import { createAction } from 'redux-actions';
                               ~~~~~~~~~~~~~~~

Found 1 error in index.ts:1

Screenshot 2023-03-02 at 10 07 05 AM

@alexander-heimbuch
Copy link
Contributor

The applied typescript configuration still transpiles to node's require:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var redux_actions_1 = require("redux-actions");
var myAction = (0, redux_actions_1.createAction)('MY_ACTION');

This doesn't work with the ESM only build we currently have. @timche this could be a case why a commonJS export still might have relevance.

@alexander-heimbuch
Copy link
Contributor

@kaelig With this configuration it should work: https://replit.com/@alexander-heimb/ReduxActionsV3BugReportTS

v3.0.0 provides the same API as v2.6.5, so for projects that might not be able to switch to ESM this should work as well. I will close this bug but if you that doesn't solve your issue feel free to reopen it.

@kaelig
Copy link
Author

kaelig commented Mar 8, 2023

Thank you! Simply adding the types did fix the build.

That said, for some reason our Jest + @swc/jest setup still throws the same error so I need to investigate further...

Any tips appreciated!

@alexander-heimbuch
Copy link
Contributor

Hey @kaelig,
the jest error might be due to the configured jest runtime. Could you share the configuration? Btw. it just sprung to my mind that we worked for the same company (Salesforce) a while ago :D

@kaelig
Copy link
Author

kaelig commented Mar 10, 2023

Oh, what a fun coincidence!

Here's our jest.config.js configuration:

import hq from 'alias-hq';

const ESM_NODE_MODULES = {
  '@devcycle/devcycle-js-sdk/node_modules/uuid/*': ['uuid'],
  '@storybook/addon-docs/*': ['@mdx-js/react'],
  'markdown-table': undefined,
  'node-fetch': ['data-uri-to-buffer', 'fetch-blob', 'formdata-polyfill'],
  'parse-domain': ['ip-regex', 'is-ip'],
  'react-markdown': [
    'bail',
    'decode-named-character-reference',
    'hast-util-whitespace',
    'is-plain-obj',
    'mdast-util-from-markdown',
    'mdast-util-to-hast',
    'micromark',
    'remark-rehype',
    'trim-lines',
    'trough',
    'unified',
    'unist-util-generated',
  ],
  'strip-ansi': ['ansi-regex'],
};

const ignoreESMNodeModules = (delimiter = '|') => {
  return `<rootDir>/node_modules/(?!${Object.entries(ESM_NODE_MODULES)
    .map(([key, values = []]) => [key, ...values].join(delimiter))
    .join(delimiter)})`;
};

/** @type {import('jest').Config} */
// eslint-disable-next-line import/no-anonymous-default-export
export default {
  setupFiles: ['<rootDir>/test/setup.ts', 'jest-canvas-mock'],
  setupFilesAfterEnv: ['<rootDir>/test/setupAfterEnv.ts'],
  transform: {
    '^.+\\.(t|j)sx?$': [
      '@swc/jest',
      {
        jsc: {
          parser: {
            syntax: 'typescript',
            decorators: true,
          },
          transform: {
            react: {
              runtime: 'automatic',
            },
          },
          experimental: {
            plugins: [['jest_workaround', {}]],
          },
        },
      },
    ],
  },
  transformIgnorePatterns: [ignoreESMNodeModules()],
  automock: false,
  testMatch: [
    '<rootDir>/(src|functions)/**/*.spec.[jt]s?(x)',
    '<rootDir>/*.spec.[jt]s?(x)',
  ],
  modulePaths: ['<rootDir>/src', '<rootDir>/test'],
  moduleFileExtensions: ['js', 'cjs', 'json', 'jsx', 'ts', 'tsx'],
  moduleNameMapper: {
    '\\.(css|jpg|png|webp)$': '<rootDir>/test/empty-module.cjs',
    '\\.svg$': '<rootDir>/test/svg-file-mock.ts',
    // Load path aliases from tsconfig.json
    ...hq.get('jest', { format: 'array' }),
  },
  collectCoverageFrom: [
    'src/**/*.[jt]s?(x)',
    'functions/**/*.[jt]s?(x)',
    '!**/src/**/*.stories.[jt]s?(x)',
    '!**/node_modules/**',
    '!**/.netlify/**',
    '!**/src/config/**',
    '!**/src/ampli/**',
    '!**/__generated__/**',
    '!**/*.d.ts',
  ],
  reporters: [
    'default',
    [
      'jest-junit',
      { outputDirectory: 'reports/jest', outputName: 'report.xml' },
    ],
  ],
  testEnvironment: 'jest-environment-jsdom',
  testEnvironmentOptions: {
    url: 'http://localhost/',
    resources: 'usable',
  },
};

@alexander-heimbuch
Copy link
Contributor

@kaelig did you tried to add redux-actions to the ESM_NODE_MODULES? Might be worth a try.

@kaelig
Copy link
Author

kaelig commented Mar 14, 2023

That's the first thing I tried, but sadly it didn't solve it :/

@markthiessen
Copy link

@kaelig did you solve this? Running into the same issue

@kaelig
Copy link
Author

kaelig commented Apr 4, 2023

I haven't had time to look into this dependency upgrade. Since 3.0.0 doesn't bring any new features, I've deprioritized it.

If anybody finds a workaround, please post here!

@alexander-heimbuch
Copy link
Contributor

Hey @kaelig,

That is a valid approach. v3 is dedicated to bring ESM compatibility. In case you want to give it a try I managed to provide a minimal example with v3 and Jest: https://replit.com/@alexander-heimb/redux-actions-jest#jest.config.js

Also a different way would be to use vitest as an alternative test runner.

@prathamesh-mutkure
Copy link

Hey @alexander-heimbuch, here's my jest config

"jest": {
    "globalSetup": "./test/jest.global-setup.js",
    "setupFilesAfterEnv": [
      "./test/jest-per-test-setup.js"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts",
      "!src/setup*.js",
      "!src/utils/DraggableManager/demo/*.tsx",
      "!src/utils/test/**/*.js",
      "!src/demo/**/*.js",
      "!src/types/*"
    ],
    "reporters": [
      "default",
      "jest-junit"
    ],
    "transform": {
      "\\.(css|png)$": "./test/generic-file-transform.js",
      "\\.([jt]sx?|svg)$": "./test/babel-transform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\\\\\]node_modules[/\\\\\\\\].+\\\\.(js|jsx|mjs|cjs|ts|tsx)$"
    ],
    "testEnvironment": "jsdom",
    "snapshotFormat": {
      "escapeString": true,
      "printBasicPrototype": true
    }
  }

The app is running fine but it's giving errors in test, it is giving Module Not Found error in all places where it's imported

Screenshot 2023-08-11 at 4 44 20 AM

@kaelig @markthiessen did you guys find any solution?

@kaelig
Copy link
Author

kaelig commented Aug 23, 2023

I haven't found any solutions, sorry!

yurishkuro pushed a commit to jaegertracing/jaeger-ui that referenced this issue Sep 23, 2023
## Which problem is this PR solving?
- This PR upgrades the `redux-actions` package from v2.2.1 to v2.6.5.
- The next version released by the redux-actions team is v3, after
2.6.5, but it doesn't support CJS exports so we can't upgrade to that
version.
- relates to:
redux-utilities/redux-actions#390
- Also, v3 doesn't include any new feature, so can be ignored:
https://github.com/redux-utilities/redux-actions/releases/tag/v3.0.0

## Notable addition:
Since version 2.5.0, package size has been reduced by up to 50%

## How was this change tested?
- locally

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

---------

Signed-off-by: Ansh Goyal <anshgoyal1704@gmail.com>
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

4 participants