Comments (17)
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.
we have the same issue with esm + jest
from connect-redis.
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.
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.
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.
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.
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.
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.
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.
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.
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.
just to be clear, this is an issue of
jest
and notconnect-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.
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.
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.
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.
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.
This issue was closed because it has been stalled for 5 days with no activity.
from connect-redis.
Related Issues (20)
- RedisStore lacks construct signature with typescript HOT 6
- Unable to upgrade [email protected] to [email protected] [typescript] HOT 12
- Use with fastify HOT 6
- TypeError: require(...) is not a function
- 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type. HOT 1
- TypeError: Invalid argument type HOT 2
- connectRedis is not a function HOT 1
- What does the secret parameter actually do? HOT 1
- Issue with express-session HOT 1
- Support passing in `Store` class HOT 4
- Add more functions to normalized client? HOT 2
- Express Application Freeze HOT 5
- Export default build issue HOT 2
- Create a Security Policy HOT 2
- Client uses `ioredis` syntax when using `createCluster` from node-redis HOT 1
- `RedisStore` not working on TypeScript HOT 1
- Pub/Sub silently breaks the store HOT 1
- Connect-redis 7.0.0 and Redis 4.6.11 throwing "store.on is not a function" in Node v.16 HOT 10
- Type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is not assignable to type 'Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference<any>'. HOT 6
- TypeScript CommonJS/ESM HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from connect-redis.