Jest模拟不返回预期值

时间:2018-03-09 10:08:29

标签: jestjs babel-jest

我发现我正在使用的模拟器会返回一个字符串,似乎正在返回jest.fn()而不是"实现"模拟是jest.fn().mockImplementation(...)

我这样称呼它:

const mockDefaultQuery = 'query { mock }'
jest.mock('../functions', () => (
{
    getArticle: jest.fn().mockName('getArticle').mockImplementation(() => {
        return {}
    }),
    defaultQuery: jest.fn().mockImplementation(() => {
        return mockDefaultQuery
    })
})
)

但是从导入的'函数'调用defaultQuery。库在测试范围内返回[Function mockConstructor]而不是" query {mock}"由const定义它应该返回。

我也试过使用jest.fn().mockReturnValue(mockDefaultQuery),但无济于事。

1 个答案:

答案 0 :(得分:0)

问题是在编译期间#target photoshop // // Conditional Layer Removal PSD JPG.jsx // Conditional Removal of ID Layer based upon number of layer in a PSD // // Generated Sat Jul 29 2017 00:59:28 GMT+0500 // cTID = function(s) { return app.charIDToTypeID(s); }; sTID = function(s) { return app.stringIDToTypeID(s); }; var layerCount = app.activeDocument.layers.length; if (layerCount > 2) { // //==================== RemoveIDLayer ============== // function RemoveIDLayer() { // Select function step1(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Frnt')); desc1.putReference(cTID('null'), ref1); desc1.putBoolean(cTID('MkVs'), false); var list1 = new ActionList(); list1.putInteger(9); desc1.putList(cTID('LyrI'), list1); executeAction(cTID('slct'), desc1, dialogMode); }; // Delete function step2(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt')); desc1.putReference(cTID('null'), ref1); var list1 = new ActionList(); list1.putInteger(9); desc1.putList(cTID('LyrI'), list1); executeAction(cTID('Dlt '), desc1, dialogMode); }; // Select function step3(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Frnt')); desc1.putReference(cTID('null'), ref1); desc1.putBoolean(cTID('MkVs'), false); var list1 = new ActionList(); list1.putInteger(9); desc1.putList(cTID('LyrI'), list1); executeAction(cTID('slct'), desc1, dialogMode); }; // Trim function step4(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); desc1.putEnumerated(sTID("trimBasedOn"), sTID("trimBasedOn"), sTID("topLeftPixelColor")); desc1.putBoolean(cTID('Top '), true); desc1.putBoolean(cTID('Btom'), true); desc1.putBoolean(cTID('Left'), true); desc1.putBoolean(cTID('Rght'), true); executeAction(sTID('trim'), desc1, dialogMode); }; // Trim function step5(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); desc1.putEnumerated(sTID("trimBasedOn"), sTID("trimBasedOn"), sTID("bottomRightPixelColor")); desc1.putBoolean(cTID('Top '), true); desc1.putBoolean(cTID('Btom'), true); desc1.putBoolean(cTID('Left'), true); desc1.putBoolean(cTID('Rght'), true); executeAction(sTID('trim'), desc1, dialogMode); }; // Scripts function step6(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var estr = '//@include "~/OneDrive/Business/Website Making/Commands and Scripts/Adobe Scripts/Conditional resizeImage 1100 All.jsx";\r\n'; eval(estr); }; // Canvas Size function step7(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); desc1.putBoolean(cTID('Rltv'), true); desc1.putUnitDouble(cTID('Wdth'), cTID('#Pxl'), 300); desc1.putUnitDouble(cTID('Hght'), cTID('#Pxl'), 300); desc1.putEnumerated(cTID('Hrzn'), cTID('HrzL'), cTID('Cntr')); desc1.putEnumerated(cTID('Vrtc'), cTID('VrtL'), cTID('Cntr')); desc1.putEnumerated(sTID("canvasExtensionColorType"), sTID("canvasExtensionColorType"), cTID('BckC')); executeAction(sTID('canvasSize'), desc1, dialogMode); }; // Scripts function step8(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var estr = '//@include "~/OneDrive/Business/Website Making/Commands and Scripts/Adobe Scripts/PSDtoJPG.jsx";\r\n'; eval(estr); }; // Close function step9(enabled, withDialog) { if (enabled != undefined && !enabled) return; var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO); var desc1 = new ActionDescriptor(); desc1.putEnumerated(cTID('Svng'), cTID('YsN '), cTID('N ')); desc1.putInteger(cTID('DocI'), 816); desc1.putBoolean(sTID("forceNotify"), true); executeAction(cTID('Cls '), desc1, dialogMode); }; step1(); // Select step2(); // Delete step3(); // Select step4(); // Trim step5(); // Trim step6(); // Scripts step7(); // Canvas Size step8(); // Scripts step9(); // Close }; //========================================= // RemoveIDLayer.main //========================================= // RemoveIDLayer.main = function () { RemoveIDLayer(); }; RemoveIDLayer.main(); // EOF "Conditional Layer Removal PSD JPG.jsx" // EOF } else app.doAction ("< 2 Layer removal PSD", "My Actions")会在测试文件的顶部悬挂jest.mock have a look here。因此,您永远不能在模拟定义中的模块范围内使用某些东西。所以你的代码就像这样进行评估

jest.mock('../functions', () => (
{
    getArticle: jest.fn().mockName('getArticle').mockImplementation(() => {
        return {}
    }),
    defaultQuery: jest.fn().mockImplementation(() => {
        return mockDefaultQuery
    })
})
)
const mockDefaultQuery = 'query { mock }'

在这种情况下我通常做的是创建一个空模拟并在之后填充:

jest.mock('../functions', () => jest.fn())
import myFunction from '../functions'
const mockDefaultQuery = 'query { mock }'
myFunction.mockImplementation( () => (
{
    getArticle: jest.fn().mockName('getArticle').mockImplementation(() => {
        return {}
    }),
    defaultQuery: jest.fn().mockImplementation(() => {
        return mockDefaultQuery
    })
}))