Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jestjs 开玩笑的模仿(……)不在';描述';(TypeError:moduleName.split不是函数)_Jestjs_Jest Fetch Mock - Fatal编程技术网

Jestjs 开玩笑的模仿(……)不在';描述';(TypeError:moduleName.split不是函数)

Jestjs 开玩笑的模仿(……)不在';描述';(TypeError:moduleName.split不是函数),jestjs,jest-fetch-mock,Jestjs,Jest Fetch Mock,jest.mock(…)在我的测试中,在“描述”级别似乎不起作用 如果我有以下资料: import React from 'react'; import {someFunction} from "./something/someFile"; describe('Overview Test', () => { jest.mock(someFunction); test(' snapshot', () => { }); }); 然后运行“测试”(即在测试

jest.mock(…)在我的测试中,在“描述”级别似乎不起作用

如果我有以下资料:

import React from 'react';
import {someFunction} from "./something/someFile";

describe('Overview Test', () => {

    jest.mock(someFunction);

    test(' snapshot', () => {

    });
});
然后运行“测试”(即在测试级别)就可以了

但如果我运行“描述”(即描述级别或套件级别),则会出现以下错误:

TypeError: moduleName.split is not a function

    at Resolver.resolveModuleFromDirIfExists (A:\frontend\node_modules\jest-resolve\build\index.js:224:30)
    at Resolver.resolveModule (A:\frontend\node_modules\jest-resolve\build\index.js:252:12)
如果我有这个:

describe('Overview Test', () => {
    test(' snapshot', () => {
        jest.mock(someFunction);
    });
});
那么这两种方法都不起作用

我也试过:

import React from 'react';
import {someFunction} from "./something/someFile";


describe('Overview Test', () => {

    beforeEach(() => {
        jest.mock(someFunction);
    });

    test(' snapshot', () => {

    });
});
而且它不起作用

更新

我也尝试过这个,但它不起作用:

import React from 'react';
import {someFunction} from "./something/someFile";

    describe('Overview Test', () => {

        jest.mock('./something/someFile', () => {
            return { someFunction: jest.fn(() => "futhissit")};
        });

        test(' snapshot', () => {
            someFunction()
        });
    });
Jest if for mock modules,第一个参数是
moduleName
,它必须是有效的模块名(在
节点\u modules
或文件路径内),而不是直接的函数/模块:

在需要时使用自动模拟版本模拟模块<代码>工厂和
选项
是可选的

您得到的错误
TypeError:moduleName.split不是一个函数
是因为尝试拆分模块名称/路径,您可以在第
207行看到它

当您想要测试ES模块时,您需要传递
moduleName
的模块位置,并使用
\uu esModule:true
创建一个
工厂
,然后使用
jest.fn()创建导出函数的属性:

someFile.js
导出
someFunction
module.exports.someFunction=()=>“某些函数结果!”;
使用模拟
someFile.js
模块
description('Overview Test',()=>{
//模拟模块及其功能
jest.mock('./someFile',()=>({
__艾斯莫杜勒:没错,
someFunction:jest.fn(()=>“mock someFunction!”)
}));
//从模拟模块导入函数
const{someFunction}=require('./someFile');
测试('快照',()=>{
//执行模拟函数
const someResult=someFunction();
//期望返回模拟值
expect(someResult).toBe('Mocked someFunction!');
});
});

您必须在
jest.mock
模块模拟之后导入模拟模块。您可以创建一个
jest.setup.js
并使用它进行配置,这样可以将您的模拟放在其中,然后像正常一样在测试文件顶部导入模块。

谢谢,我在导出模块时弄糊涂了。。封锁愉快!