alonronin/mockingoose

Need some clarification

calvintychan opened this issue · 5 comments

Hi @alonronin,

Mockingoose looks great! Thanks for creating this project.

I am having trouble using Mockingoose + Jest in my project, but I am sure its due to my lack of knowledge and I am hoping you can provide some clarification.

I am trying to test a wrapper function but I keep getting a timeout in jest.

// userCreate.js
import { User } from '../models'

const createUser = user => {
  const doc = new User(user)
  return doc.save();
}

export default createUser;
// userCreate.test.js
import mockingoose from 'mockingoose';
import createUser from './createUser';

describe('test create user api', () => {
  it('should create user', async () => {
    mockingoose.User.toReturn({ _id: '507f191e810c19729de860ea' }, 'save');
    return createUser({ first_name: 'Calvin', last_name: 'Chan' })
      .then(result => {
        console.log(result); 
        expect(true).toBe(true);
      });
  });
});
// console
$> jest
 FAIL  src/users/createUser.test.js (7.675s)
  ● test create user api › should create user

    Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

I am expecting the test to pass but receiving a timeout instead. It looks like the promise is never resolved in createUser.js. Any guidance will be greatly appreciated.

Thanks in advance for your help!

Calvin

Try

const createUser = async (user) => { const doc = new User(user); return await doc.save(); }

i have the same problem too and i find that using mockingoose@2.4.0 works fine but newer releases result in the timeout

I've been having a similar issue when using the remove instance method. For example:

handler: async (request, h) => {
		const {
			params
		} = request

		const user = await UserRepository.findOne({ _id: params.id })

		if (user) {
			await user.remove()
			const response = h.response()
			response.statusCode = '204'

			return response
		}

		return UserNotFoundError
	}

seems like the remove method is not overwritten from within this package like save is. I've put a PR here to address this #24

Interestingly, if I copy mongoose source code and import it directly, everything is working as expected. Importing the lib from node_module results in timeout.

@calvintychan can you share with me a sample project so I can test it?