jest.mock(..) 无法在“描述”中工作(类型错误: moduleName.split 不是函数)

2024-01-10

笑话.mock(..)似乎不适用于 'describe' 我的测试水平。

如果我有以下内容:

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', () => {

    });
});

但它不起作用。

UPDATE

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

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 mock https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options用于模拟模块,第一个参数是moduleName它必须是有效的模块名称(inside node_modules或文件路径)而不是直接函数/模块:

jest.mock(moduleName, factory, options) https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options

在需要时使用自动模拟版本来模拟模块。factory and options是可选的。

你收到的错误TypeError: moduleName.split is not a function是因为resolveModuleFromDirIfExists https://github.com/facebook/jest/blob/master/packages/jest-resolve/src/index.ts#L137尝试分割模块名称/路径,你可以在里面看到它jest-resolve/src/index.ts https://github.com/facebook/jest/blob/master/packages/jest-resolve/src/index.ts#L207在线207.

当您想要测试 ES 模块时,您可以传递模块位置moduleName然后你创建一个factory using __esModule: true然后使用被模拟的导出函数创建属性jest.fn():

someFile.js出口的someFunction:

module.exports.someFunction = () => 'Some function result!';

Mocking someFile.js模块使用jest.mock() https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options

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

  // Mock the module and its functions
  jest.mock('./someFile', () => ({
    __esModule: true,
    someFunction: jest.fn(() => 'Mocked someFunction!')
  }));

  // Import the function from the mocked module
  const { someFunction } = require('./someFile');

  test('snapshot', () => {
    // Execute the mocked function
    const someResult = someFunction();

    // Expect to return the mocked value
    expect(someResult).toBe('Mocked someFunction!');
  });

});

您必须在之后导入模拟模块jest.mock模块模拟。您可以创建一个jest.setup.js并使用配置它setupFilesAfterEnv https://jestjs.io/docs/en/configuration#setupfilesafterenv-array可以在其中包含您的模拟,然后像平常一样在测试文件顶部导入模块。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jest.mock(..) 无法在“描述”中工作(类型错误: moduleName.split 不是函数) 的相关文章

随机推荐