反对模拟始终返回referenceerror:未定义require

qhhrdooz  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(164)

我有一个简单的(es6) Express 应用程序结构为 module . 这个应用程序运行得很好,但是现在我尝试引入一些测试,我在开玩笑时遇到了一些奇怪的错误。
所讨论的测试如下所示:

// ./src/service/customer.service.test.js
import CustomerService from "./customer.service.js";

import { describe, expect, jest, test } from "@jest/globals";

describe("Customer Service", () => {
  const service = new CustomerService();

  jest.mock("../model/customer.model.js", () => {
    const expected = [
      {
        id: "baf5dade-eabd-4d54-98f7-e90f6953108f",
        first_name: "First",
        last_name: "Last",
        email: "first.last@acme.org",
      },
    ];
    return {
      query: expected,
    };
  });

  describe("findAll", () => {
    test("should return all customers", () => {
      expect(service.findAll()).toEqual([]); // ...change [] for the real deal!
    });
  });
});

…以及使用相应的 Customer 模型看起来像:

// ./src/service/customer.service.js
import { logger } from "../config/winston.js";
import { Customer } from "../model/customer.model.js";

export default class CustomerService {
  findAll() {
    logger.info("Searching all customers...");
    return Customer.query();
  }
}
// ./src/model/customer.model.js
import { Model } from "objection"; // "objection": "2.2.15" from package.json

export class Customer extends Model {
  static get idColumn() {
    return "id";
  }

  static get tableName() {
    return "customers";
  }
}

我什么时候跑步 npm run test (定义为 "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest" 在里面 package.json )我得到了这个错误:

FAIL  src/service/customer.service.test.js
  ● Test suite failed to run

    ReferenceError: require is not defined

      2 |
      3 | import { describe, expect, jest, test } from "@jest/globals";
    > 4 |
        | ^
      5 | describe("Customer Service", () => {
      6 |   const service = new CustomerService();
      7 |

      at _getJestObj (src/service/customer.service.test.js:4:7)
      at src/service/customer.service.test.js:8:3
      at src/service/customer.service.test.js:5:1

这里有什么线索吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题