如何使用 jest 模拟 Knex

Arj*_*jun 6 mocking node.js knex.js jestjs

我正在尝试使用 jest 来模拟 knex 进行以下实现

 const knex = Knex({ client: "mysql"})

    const query = knex("table_name")
      .where({
        title: "xxx-yyy",
        lang: "eng"
      })
      .select()
      .orderBy('date', 'desc')
      .toSQL()
      .toNative()
      
Run Code Online (Sandbox Code Playgroud)

下面我尝试了,但它不起作用并收到错误“TypeError:knex不是函数”

jest.mock("knex", () => {
  return () => {
    return {
      knex: () => {
        where: () => {
          select: () => {
            orderBy: () => {
              toSQL: () => {
                toNative: jest.fn()
              }
            }
          }
        }
      }
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

我真的很感谢对此的任何帮助。

sli*_*wp2 11

jest.mock()将使用自动模拟版本模拟模块,factory并且options是可选的。

\n

您可以使用mockFn.mockReturnThis()来模拟方法链接调用。

\n

此外,如果您Knex在模块范围内进行初始化,则需要在require模拟设置后对模块进行初始化。

\n

例如

\n

index.js

\n
import Knex from \'knex\';\n\nconst knex = Knex({ client: \'mysql\' });\n\nexport function main() {\n  const query = knex(\'table_name\')\n    .where({\n      title: \'xxx-yyy\',\n      lang: \'eng\',\n    })\n    .select()\n    .orderBy(\'date\', \'desc\')\n    .toSQL()\n    .toNative();\n}\n
Run Code Online (Sandbox Code Playgroud)\n

index.test.js

\n
import Knex from \'knex\';\n\njest.mock(\'knex\');\n\ndescribe(\'68717941\', () => {\n  test(\'should pass\', () => {\n    const querybuilder = {\n      where: jest.fn().mockReturnThis(),\n      select: jest.fn().mockReturnThis(),\n      orderBy: jest.fn().mockReturnThis(),\n      toSQL: jest.fn().mockReturnThis(),\n      toNative: jest.fn(),\n    };\n    const mKnex = jest.fn().mockReturnValue(querybuilder);\n    Knex.mockReturnValue(mKnex);\n    const { main } = require(\'./\');\n    main();\n    expect(Knex).toBeCalledWith({ client: \'mysql\' });\n    expect(mKnex).toBeCalledWith(\'table_name\');\n    expect(querybuilder.where).toBeCalledWith({ title: \'xxx-yyy\', lang: \'eng\' });\n    expect(querybuilder.select).toBeCalledTimes(1);\n    expect(querybuilder.orderBy).toBeCalledWith(\'date\', \'desc\');\n    expect(querybuilder.toSQL).toBeCalledTimes(1);\n    expect(querybuilder.toNative).toBeCalledTimes(1);\n  });\n});\n
Run Code Online (Sandbox Code Playgroud)\n

测试结果:

\n
 PASS  examples/68717941/index.test.js (8.844 s)\n  68717941\n    \xe2\x9c\x93 should pass (7464 ms)\n\n----------|---------|----------|---------|---------|-------------------\nFile      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\nAll files |     100 |      100 |     100 |     100 |                   \n index.js |     100 |      100 |     100 |     100 |                   \n----------|---------|----------|---------|---------|-------------------\nTest Suites: 1 passed, 1 total\nTests:       1 passed, 1 total\nSnapshots:   0 total\nTime:        9.439 s\n
Run Code Online (Sandbox Code Playgroud)\n

  • 出现错误:类型“typeof knex”上不存在属性“mockReturnValue” (6认同)