【问题标题】:jest.mock(..) not working in 'describe' (TypeError: moduleName.split is not a function)jest.mock(..) 在“describe”中不起作用(TypeError:moduleName.split 不是函数)
【发布时间】:2020-04-16 14:15:41
【问题描述】:

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

    });
});

而且它不起作用。

更新

我也试过了,还是不行:

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

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

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

        test(' snapshot', () => {
            someFunction()
        });
    });

【问题讨论】:

    标签: jestjs jest-fetch-mock


    【解决方案1】:

    Jest mock 如果用于模拟模块并且第一个参数是 moduleName,它必须是有效的模块名称(node_modules 或文件路径中)而不是直接功能/模块:

    jest.mock(moduleName, factory, options)

    在需要时使用自动模拟版本模拟模块。 factoryoptions 是可选的。

    您收到TypeError: moduleName.split is not a function 的错误是因为resolveModuleFromDirIfExists 尝试拆分模块名称/路径,您可以在jest-resolve/src/index.ts 中的207 行看到它。

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

    someFile.js 导出someFunction

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

    使用jest.mock() 模拟someFile.js 模块

    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 对其进行配置,其中可以包含您的模拟,然后像往常一样在测试文件的顶部导入模块。

    【讨论】:

    • 谢谢,我被模块的导出搞糊涂了。祝锁定愉快!
    • 在直接导入或作为嵌套依赖项之前模拟模块是诀窍。谢谢!!!
    • 对我来说,我的测试在升级到 jest v27 后停止工作,并且都给出了关于我的 jest.mock 挂钩参考(在我的 setupFileAfterEnv 文件中)不是函数的错误。添加__esModule: true 对我有用,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2019-10-02
    相关资源
    最近更新 更多