Code Monkey home page Code Monkey logo

Comments (17)

abhijit945 avatar abhijit945 commented on August 23, 2024 3

Fixes for me by adding the following in jest.config.js

"moduleNameMapper": {
      "connect-redis": "<rootDir>/node_modules/connect-redis/dist/cjs"
 }

Use import like usual

import RedisStore from 'connect-redis';

from connect-redis.

lukaselmer avatar lukaselmer commented on August 23, 2024 1

we have the same issue with esm + jest

from connect-redis.

anbusurendhars avatar anbusurendhars commented on August 23, 2024 1

I've similar issue when trying to load this library with testdouble.js.

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/path/node_modules/connect-redis/dist/esm/index.js from /app/path/redis-connection.ts not supported. Instead change the require of index.js in /app/path/redis-connection.ts to a dynamic import() which is available in all CommonJS modules.

from connect-redis.

anbusurendhars avatar anbusurendhars commented on August 23, 2024 1

Fixed the issue with the below code in function scope where exactly the RedisStore class is being used.

function someFunction(){

const RedisStore = (await import('connect-redis')).default;
expressSession({store: new RedisStore()});

}

Test framework: Testdouble
Test runner: Mocha
Test assert lib: Chai

Reference: https://stackoverflow.com/questions/29923879/pass-options-to-es6-module-imports

from connect-redis.

lukaselmer avatar lukaselmer commented on August 23, 2024 1

Out of straight curiosity, could you name a few libraries that export both cjs and esm that you use and jest is ok with them? Because this is literally a pita for my team. Thank you

I hear you, it's a real pain with cjs and esm. I'll check.

from connect-redis.

SimenB avatar SimenB commented on August 23, 2024 1

What version of Jest are people using? exports support needs Jest 28 or newer. Jest 29 has, as far as I know, 100% support.


connect-redis is doing nothing wrong. If people are having issues using Jest 29 (latest, as of the time of writing), and issue should be opened there. This one should be closed.

from connect-redis.

wavded avatar wavded commented on August 23, 2024

Yeah, I can concur with your findings. You are requiring in a CJS fashion but Jest (or something) is pulling in the ESM module and not the CJS one for some reason. I'm not familiar with Jest to know what it may be doing under the hood. I'm assuming this isn't an issue when you are just running the project?

from connect-redis.

cynicaloptimist avatar cynicaloptimist commented on August 23, 2024

I'm getting this at runtime with my commonjs node project.

Uncaught SyntaxError C:\Users\cynic\Documents\src\improved-initiative\node_modules\connect-redis\dist\esm\index.js:1
import { Store } from "express-session";
^^^^^^

SyntaxError: Cannot use import statement outside a module

As a workaround, I got it to target the right js file with this:

const RedisStore = require("connect-redis/dist/cjs").default;

from connect-redis.

newsiberian avatar newsiberian commented on August 23, 2024

I'm using ESM and I also have an issue w/ CJS when running jest:

 FAIL  src/graphql/users/resolvers/__tests__/oauthLogin.int.test.mtst.mts:207:12)
  ● Test suite failed to run

    src/redis.mts:82:31 - error TS2351: This expression is not constructable.
      Type 'typeof import("../node_modules/connect-redis/dist/cjs/index")' has no construct signatures.

    82 export const redisStore = new RedisStore({

I have a suggestion: maybe you also might add named export near default? I can make a PR if you need

from connect-redis.

github-actions avatar github-actions commented on August 23, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

from connect-redis.

konkasidiaris avatar konkasidiaris commented on August 23, 2024

Hello friends, have you tried the moduleNameMapper option on jest config?

all you have to do is to add the following

  moduleNameMapper: {
     'connect-redis': '<rootDir>/node_modules/connect-redis/dist/cjs'
  }

in order for jest to resolve where the cjs code lies
just to be clear, this is an issue of jest and not connect-redis library

from connect-redis.

lukaselmer avatar lukaselmer commented on August 23, 2024

just to be clear, this is an issue of jest and not connect-redis library

uhm, I'm pretty sure this is a problem with the connect-redis library. we use literally hundreds of modules, and only connect-redis has this issue 🤔

from connect-redis.

konkasidiaris avatar konkasidiaris commented on August 23, 2024

Out of straight curiosity, could you name a few libraries that export both cjs and esm that you use and jest is ok with them?
Because this is literally a pita for my team. Thank you

from connect-redis.

oliverlockwood avatar oliverlockwood commented on August 23, 2024

Fixed the issue with the below code in function scope where exactly the RedisStore class is being used.

function someFunction(){

const RedisStore = (await import('connect-redis')).default; expressSession({store: new RedisStore()});

}

Test framework: Testdouble Test runner: Mocha Test assert lib: Chai

Reference: https://stackoverflow.com/questions/29923879/pass-options-to-es6-module-imports

@anbusurendhars (and anyone else who's interested!) for a testdouble workaround, It would be better instead to simply change your production code from:
import RedisStore from 'connect-redis';
into:
import RedisStore from 'connect-redis/dist/cjs';

This fix allows testdouble to mock out a module which imports connect-redis, with significantly less pollution to the production code.

Of course, it would be nicer if this issue could be properly fixed 😉

from connect-redis.

github-actions avatar github-actions commented on August 23, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

from connect-redis.

github-actions avatar github-actions commented on August 23, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

from connect-redis.

github-actions avatar github-actions commented on August 23, 2024

This issue was closed because it has been stalled for 5 days with no activity.

from connect-redis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.