首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jest ReferenceError:无法在初始化前访问“”

jest ReferenceError:无法在初始化前访问“”
EN

Stack Overflow用户
提问于 2021-01-03 21:03:07
回答 7查看 42.4K关注 0票数 44

我得到了错误:

ReferenceError: Cannot access 'myMock' before initialization

尽管我尊重关于提升的开玩笑文档:A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'.

我要这么做:

代码语言:javascript
运行
复制
import MyClass from './my_class';
import * as anotherClass from './another_class';

const mockMethod1 = jest.fn();
const mockMethod2 = jest.fn();
jest.mock('./my_class', () => {
  return {
    default: {
      staticMethod: jest.fn().mockReturnValue(
        {
          method1: mockMethod1,
          method2: mockMethod2,
        })
    }
  }
});

正如您所看到的,我的两个变量都尊重“标准”,但没有正确地悬挂。

我漏掉了什么吗?

显然,当我只通过jest.fn()而不是变量时,它就能工作,但是我不知道以后如何在我的测试中使用这些。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2021-04-15 19:07:15

当您需要监视const声明时,接受的答案不会处理,因为它是在模块工厂范围内定义的。

对我来说,模块工厂需要是,而不是--任何最终导入您想要模拟的东西的导入语句。下面是一个使用雀巢普里斯马库的代码片段。

代码语言:javascript
运行
复制
// app.e2e.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import mockPrismaClient from './utils/mockPrismaClient'; // you can assert, spy, etc. on this object in your test suites.

// must define this above the `AppModule` import, otherwise the ReferenceError is raised.
jest.mock('@prisma/client', () => {
  return {
    PrismaClient: jest.fn().mockImplementation(() => mockPrismaClient),
  };
});

import { AppModule } from './../src/app.module'; // somwhere here, the prisma is imported

describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });
)};
票数 19
EN

Stack Overflow用户

发布于 2021-06-21 19:31:04

上面的答案都解决不了我的问题,下面是我的解决方案:

代码语言:javascript
运行
复制
var mockMyMethod: jest.Mock;

jest.mock('some-package', () => ({
  myMethod: mockMyMethod
}));

在进口产品之前使用const让我觉得很奇怪。事情是:jest.mock被吊起来了。为了能够在变量之前使用它,您需要使用var,因为它也是悬挂的。它不适用于letconst,因为它们不起作用。

票数 35
EN

Stack Overflow用户

发布于 2021-05-19 00:49:37

要澄清贾森·利曼托罗的意思,请将const移到导入模块的位置上:

代码语言:javascript
运行
复制
const mockMethod1 = jest.fn(); // Defined here before import.
const mockMethod2 = jest.fn();

import MyClass from './my_class'; // Imported here.
import * as anotherClass from './another_class';

jest.mock('./my_class', () => {
  return {
    default: {
      staticMethod: jest.fn().mockReturnValue(
        {
          method1: mockMethod1,
          method2: mockMethod2,
        })
    }
  }
});
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65554910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档