-
Notifications
You must be signed in to change notification settings - Fork 126
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
Cannot read property '_transformer' of undefined #568
Comments
Hey! This is because Something like: const ioredis = require('ioredis-mock')
module.exports = ioredis
import { Redis } from "../Redis";
describe('RedisAdapter', () => {
it('can get', async () => {
const redis = new Redis();
expect(await redis.get('KEY')).toBe(null)
})
})
import * as ioredis from 'ioredis'
export default redis
import ioredis from './RedisClient'
export class Redis {
private instance: ioredis.Redis | null
constructor() {
this.instance = null
}
private async getInstance() {
if (!this.instance) {
this.instance = new ioredis({
host: '127.0.0.1',
port: 6379,
retryStrategy: (times) => false
})
return this.instance
} else {
return this.instance
}
}
public async get(k: string) {
return this.getInstance().then(instance => instance.get(k))
}
} Hope this helps 😄 |
@stipsan Thank you so much, it worked! Updating the example repo just in case anybody stumbles across this issue. |
Great to hear! 👍 |
For what it worth, if you don't want to change to introduce this client file, here is what you can do: jest.mock('ioredis', () => {
const Redis = require('ioredis-mock')
if (typeof Redis === 'object') {
// the first mock is an ioredis shim because ioredis-mock depends on it
// /~https://github.com/stipsan/ioredis-mock/blob/master/src/index.js#L101-L111
return {
Command: { _transformer: { argument: {}, reply: {} } }
}
}
// second mock for our code
return function(...args) {
return new Redis(args)
}
}) If you need access to the ioredis-mock instance: const mocks = { redis: null }
jest.mock('ioredis', () => {
const Redis = require('ioredis-mock')
if (typeof Redis === 'object') {
// the first mock is an ioredis shim because ioredis-mock depends on it
// /~https://github.com/stipsan/ioredis-mock/blob/2ba837f07c0723cde993fb8f791a5fcfdabce719/src/index.js#L100-L109
return {
Command: { _transformer: { argument: {}, reply: {} } }
}
}
// second mock for our code
return function(...args) {
const instance = new Redis(args)
mocks.redis = instance
return instance
}
}) |
Reopening this as it would be really helpful to have this information in the readme 🤔 |
I'm currently struggling with this, and I just wanted to add my thoughts. jest.setMock('ioredis', require('ioredis-mock'));
const ioredis = require('ioredis'); This will then mock out any references to |
This seemed to work: in const ioredisMock = require('ioredis-mock')
jest.mock('ioredis', () => {
const Redis = require('ioredis-mock')
if (typeof Redis === 'object') {
// the first mock is an ioredis shim because ioredis-mock depends on it
// /~https://github.com/stipsan/ioredis-mock/blob/master/src/index.js#L101-L111
return {
Command: { _transformer: { argument: {}, reply: {} } },
}
}
// second mock for our code
return Redis
})
module.exports = ioredisMock |
None of the above worked for us, the following did: // On the jest setup file: __test__/setup/index.ts
const ioredisMock = require('ioredis-mock')
jest.setMock('ioredis', ioredisMock) |
@santiagofm solution worked for me. |
@sjclemmy Your method of mocking Redis allows you to write directly to Redis using the mock client within the test itself. For example if you're using Jest you create a test harness to preload data before each test using all the standard ioredis methods and the data would be written in-memory. I prefer this over passing a data object to the RedisMock constructor. In my service/application I have specialized DAOs/DTOs to read/write data to Redis. Not sure what your application does but if you follow this pattern and create your own set of DAOs and DTO's then you can very easily test by using ioredis-mock to mock all those calls out to Redis. |
what is the best way to clean up redis data among tests? |
Starting in jest.mock('ioredis', () => require('ioredis-mock/jest')); |
@stipsan even with that I still get
|
Description
I cannot use
ioredis-mock
with Jest module mocking, it keeps erroring back the below error. I've tried to pinpoint it down but couldn't find anything. Any ideas?Source files
/__mocks__/ioredis.ts
/src/__tests__/Redis.ts
/src/Redis.ts
Sample Repo
/~https://github.com/liangchunn/ioredis-transform-error
The text was updated successfully, but these errors were encountered: