简体   繁体   中英

Mocking chained methods

(I use Jest for testing) For example, I have this function:

const find = () => {
    return {
        where: () => {
            in: () => {}
        }
    };
};

and I call that in different place:

find('me').where('id').in(['123']);

How to mock and test calls in find(), where() and in()?

Here's a dirt simple mock interface:

 const find = (findData) => { const data = { find: findData }; const self = { where: (whereData) => { data.where = whereData; return self; }, in: (inData) => { data.in = inData; return self; }, data }; return self; }; const res = find('me').where('id').in(['123']); console.log(res.data); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM