Я сталкиваюсь с типом ошибки: user.findone.mockresolvedValue не является функцией, пытаясь издеваться над методом FindOne Mongoose в моих шутках. Проблема возникает, хотя я правильно издевался над моделью пользователя, используя jest.mock () и настроил макетные реализации для ее методов.
Вот моя настройка: < /p>
I am using ES modules with import and export in my code.
I am mocking a Mongoose model, specifically the findOne and create methods in the User.js model, to simulate the behavior of the database during tests.
I am running tests with Jest, and using the supertest package to send HTTP requests to my application.
Внутренние модели/Mocks/user.js:
const findOne = jest.fn();
const create = jest.fn();
export default {findOne, create};
< /code>
Inside jest.config.js: < /p>
export default {
testEnvironment: 'node',
transform: {},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
};
< /code>
Inside auth.test.js: < /p>
import {jest} from '@jest/globals';
import request from 'supertest';
import app from '../app.js';
jest.mock('../models/User.js');
import User from '../models/User.js';
import bcrypt from "bcrypt";
let dummyUser;
beforeAll(async () => {
dummyUser = {
_id: 'fakeid123',
username: 'mockuser',
email: '[email protected]',
password: await bcrypt.hash('password123!', 10)
};
})
describe('Auth tanpa DB (mock)', ()=>{
it('register user successfully', async ()=>{
console.log(User.findOne);
User.findOne.mockResolvedValue(null);
User.create.mockResolvedValue(dummyUser);
const res = await request(app)
.post('/api/auth/register')
.send({
username: dummyUser.username,
email: dummyUser.email,
password: 'password123!'
});
expect(res.status).toBe(201);
expect(res.body).toHaveProperty('message');
})
it('login user successfully', async ()=>{
User.findOne.mockResolvedValue(dummyUser);
const res = await request(app)
.post('/api/auth/login')
.send({
email: dummyUser.email,
password: 'password123',
});
expect(res.status).toBe(200);
expect(res.body).toHaveProperty('token');
})
})
< /code>
И это ошибка: < /p>
FAIL tests/auth.test.js
● Console
console.log
[Function: findOne]
at Object. (tests/auth.test.js:26:11)
● Auth tanpa DB (mock) › register user successfully
TypeError: User.findOne.mockResolvedValue is not a function
25 | it('register user successfully', async ()=>{
26 | console.log(User.findOne);
> 27 | User.findOne.mockResolvedValue(null);
| ^
28 | User.create.mockResolvedValue(dummyUser);
29 |
30 | const res = await request(app)
at Object. (tests/auth.test.js:27:16)
● Auth tanpa DB (mock) › login user successfully
TypeError: User.findOne.mockResolvedValue is not a function
40 |
41 | it('login user successfully', async ()=>{
> 42 | User.findOne.mockResolvedValue(dummyUser);
| ^
43 |
44 | const res = await request(app)
45 | .post('/api/auth/login')
at Object. (tests/auth.test.js:42:16)
Подробнее здесь: https://stackoverflow.com/questions/795 ... -jest-mock
TypeError: user.findone.mockresolvedValue не является функцией при использовании jest.mock () с моделью монгуза в шутках ⇐ Javascript
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Next.js и Jest: TypeError `Аргумент« оригинал »должен быть функцией типа
Anonymous » » в форуме Javascript - 0 Ответы
- 3 Просмотры
-
Последнее сообщение Anonymous
-