What is “Argument of type is not assignable to parameter of type ‘never'” in Jest?

I took a bunch of examples online and ChatGPT code to do my testing. I ended up with some red squiggly lines under the parameters mockResolvedValue with the error:

Argument of type '{ email: string; }' is not assignable to parameter of type 'never'.ts(2345)
Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)

for these two lines:

        (getUserByEmail as jest.Mock).mockResolvedValue({ email: '[email protected]' });
        (bcrypt.hash as jest.MockedFunction<typeof bcrypt.hash>).mockResolvedValue('hashedPassword');

My test code:

import React from "react";
import test from "node:test";
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";

import { checkNumberOfUsers, createNewUser, updateProfile } from "../domain/userControl";
import { createUser, getNumberOfUsers, getUserByEmail } from "../data/local/userRepo";
import { newUserSchema } from "@/schema/custom";
import bcrypt from 'bcryptjs';


jest.mock("@/data/local/userRepo", () => ({
    getNumberOfUsers: jest.fn(),
    getUserByEmail: jest.fn(),
    createUser: jest.fn(),
}));
jest.mock('bcryptjs', () => ({
    hash: jest.fn(),
}));
// describe("checkNumberOfUsers", () => {
//     beforeEach(() => {})
// });  


jest.mock("@/data/local/userRepo", () => ({
    getNumberOfUsers: jest.fn(),
    getUserByEmail: jest.fn(),
    createUser: jest.fn(),
}));

jest.mock('bcryptjs', () => ({
    hash: jest.fn(),
    compare: jest.fn(),
}));

describe('createNewUser', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });

    it("should call getNumberOfUsers", async () => {
        await checkNumberOfUsers();
        expect(getNumberOfUsers).toHaveBeenCalled();
    });

    it('returns error when fields are invalid', async () => {
        const invalidValues = {
            email: 'invalid-email',
            password: 'short',
            confirmPassword: 'short',
        };

        const result = await createNewUser(invalidValues);

        expect(result).toEqual({ error: 'Invalid fields!' });
    });

    it('returns error when passwords do not match', async () => {
        const values = {
            email: '[email protected]',
            password: 'password123',
            confirmPassword: 'password124',
        };

        const result = await createNewUser(values);

        expect(result).toEqual({ error: 'Passwords do not match!' });
    });

    it('returns error when email is already in use', async () => {
        const values = {
            email: '[email protected]',
            password: 'password123',
            confirmPassword: 'password123',
        };

        (getUserByEmail as jest.Mock).mockResolvedValue({ email: '[email protected]' });

        const result = await createNewUser(values);

        expect(getUserByEmail).toHaveBeenCalledWith(values.email);
        expect(result).toEqual({ error: 'Email already in use!' });
    });

    it('creates a new user successfully', async () => {
        const values = {
            email: '[email protected]',
            password: 'password123',
            confirmPassword: 'password123',
        };

        (getUserByEmail as jest.MockedFunction<typeof getUserByEmail>).mockResolvedValue(null);
        (bcrypt.hash as jest.MockedFunction<typeof bcrypt.hash>).mockResolvedValue('hashedPassword');
        (createUser as jest.MockedFunction<typeof createUser>).mockResolvedValue({ id: '1', email: '[email protected]', password: 'hashedPassword' });

        const result = await createNewUser(values);

        expect(getUserByEmail).toHaveBeenCalledWith(values.email);
        expect(bcrypt.hash).toHaveBeenCalledWith(values.password, 10);
        expect(createUser).toHaveBeenCalledWith({
            email: values.email,
            password: 'hashedPassword',
        });
        expect(result).toEqual({ success: 'User created.' });
    });
});

The code I want to test:

"use server";

import {createUser, getNumberOfUsers, getUserByEmail, updateUser} from "@/data/local/userRepo";
import {newUserSchema, updateProfileSchema} from "@/schema/custom";
import {z} from "zod";
import bcrypt from "bcryptjs";

/**
 * Updates the user profile with the provided values.
 *
 * @param values - The values to update the profile with.
 * @returns An object with an error property if there was an error during the update, otherwise undefined.
 */
export async function updateProfile(values: z.infer<typeof updateProfileSchema>) {
    const validation = updateProfileSchema.safeParse(values)

    if (!validation.success) {
        return {error: "Invalid input"}
    }

    const {email, originalPassword, password} = validation.data

    const existingUser = await getUserByEmail(email)

    if (!existingUser) {
        return {error: "User not found"}
    }

    const passwordMatch = await bcrypt.compare(originalPassword, existingUser.password)

    if (!passwordMatch) {
        return {error: "Incorrect password"}
    }

    const hashedPassword = await bcrypt.hash(password, 10)

    await updateUser(existingUser.id, {email, password: hashedPassword})
}

export async function checkNumberOfUsers() {
    return await getNumberOfUsers()
}

export const createNewUser = async (values: z.infer<typeof newUserSchema>) => {
    const validatedFields = newUserSchema.safeParse(values);
    if (!validatedFields.success) {
        return {error: "Invalid fields!"};
    }

    const {email, password, confirmPassword} = validatedFields.data;

    if (password !== confirmPassword) {
        return {error: "Passwords do not match!"};
    }

    const hashedPassword = await bcrypt.hash(password, 10);

    const existingUser = await getUserByEmail(email);

    if (existingUser) {
        return {error: "Email already in use!"};
    }

    await createUser({
        email: email,
        password: hashedPassword,
    });

    // Add any other business logic here (e.g., checking credentials)
    return {success: "User created."};
};

I also tried changing the getUserByEmailand bcrypt.hash to this:

        getUserByEmail.mockResolvedValue({ email: '[email protected]' });
        bcrypt.hash.mockResolvedValue('hashedPassword');

following Argument of type ‘”openURL”‘ is not assignable to parameter of type ‘never’ using Jest, but I get:

Property 'mockResolvedValue' does not exist on type '(email: string) => Promise<{ id: string; email: string; password: string; } | null>'.ts(2339)

and:

Property 'mockResolvedValue' does not exist on type '{ (s: string, salt: string | number): Promise<string>; (s: string, salt: string | number, callback?: ((err: Error | null, hash: string) => void) | undefined, progressCallback?: ((percent: number) => void) | undefined): void; }'.ts(2339)

I found out you can use mockreturnvalueonce for “non async functions”. However I have no idea why this fixed it and my test case passed because I don’t know if bcrypt is an async function or not.

(getUserByEmail as jest.Mock).mockReturnValueOnce({ email: '[email protected]' });
(bcrypt.hash as jest.MockedFunction<typeof bcrypt.hash>).mockReturnValueOnce();

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật