功能测试与单元测试结果不一致

来源:8-12 手写curry函数,实现函数柯里化

进击的技术宅

2022-04-11

双越老师,我扩展了单元测试的内容如下:

import { curryFunc } from "./curry-function"

describe('函数柯里化测试', () => {
  it('正常情况', () => {
    function add(num1: number, num2: number, num3: number): number {
      return num1 + num2 + num3
    }
    const curryAdd = curryFunc(add)
    const res = add(10, 20, 30)
    const res1 = curryAdd(10)(20)(30)
    const res2 = curryAdd(10, 20, 30)
    const res3 = curryAdd(10)(20, 30)
    expect(res1).toBe(res)
    expect(res2).toBe(res)
    expect(res3).toBe(res)
  })

  it('参数不足', () => {
    function add2(num1: number, num2: number, num3: number, num4: number): number {
      return num1 + num2 + num3 + num4
    }
    const curryAdd = curryFunc(add2)
    expect(curryAdd(10)(20) instanceof Function).toBe(true)
  })
})

发现执行到const res3 = curryAdd(10)(20, 30)时有如下报错:

    TypeError: curryAdd(...) is not a function

      15 |     const res1 = curryAdd(10)(20)(30)
      16 |     const res2 = curryAdd(10, 20, 30)
    > 17 |     const res3 = curryAdd(10)(20, 30)
         |                              ^
      18 |     expect(res1).toBe(res)
      19 |     expect(res2).toBe(res)
      20 |     expect(res3).toBe(res)

      at Object.<anonymous> (src/practice-24/curry-function.test.ts:17:30)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

但在功能测试中,curryAdd(10)(20, 30)是正常返回结果的

/**
 * @description 函数柯里化处理
 * @param func 待柯里化处理的函数
 */
export function curryFunc(func: Function): Function {
  // 获取待柯里化处理的函数入参数量
  const funArgLength = func.length
  let args: any[] = []

  function calc(this: any, ...params: any[]) {
    args = [
      ...args,
      ...params
    ]
    if (args.length < funArgLength) {
      return calc
    } else {
      // 需要对args进行截取,使之与func函数的入参数量对应
      return func.apply(this, args.slice(0, funArgLength))
    }
  }

  return calc
}

function add(num1: number, num2: number, num3: number): number {
  return num1 + num2 + num3
}

const curryAdd = curryFunc(add)

console.log(curryAdd(10)(20, 30)) // 控制台打印60,功能正常

我的依赖库版本为:

"devDependencies": {
    "@babel/core": "^7.13.14",
    "@babel/preset-env": "^7.13.12",
    "@types/jest": "^27.0.2",
    "autoprefixer": "^10.2.5",
    "babel-jest": "^27.3.0",
    "babel-loader": "^8.2.2",
    "clean-webpack-plugin": "^3.0.0",
    "cross-env": "^7.0.3",
    "css-loader": "^5.2.0",
    "html-webpack-plugin": "^5.3.1",
    "jest": "^27.3.0",
    "less": "^4.1.1",
    "less-loader": "^8.0.0",
    "postcss-loader": "^5.2.0",
    "style-loader": "^2.0.0",
    "ts-loader": "^8.1.0",
    "typescript": "^4.2.3",
    "url-loader": "^4.1.1",
    "webpack": "^5.30.0",
    "webpack-bundle-analyzer": "^4.4.0",
    "webpack-cli": "^4.6.0",
    "webpack-dev-server": "^3.11.2",
    "webpack-merge": "^5.7.3",
    "ts-jest": "^27.0.7"
  }

麻烦双越老师协助定位一下,谢谢

写回答

1回答

双越

2022-04-11

报错提示:curryAdd(...) is not a function

那你就在单元测试的代码中打印一下 curryAdd(10) 是什么?

得根据它的报错提示一步一步调试,谁都无法通过肉眼一下看出哪里的问题。

0
3
mkzlog
回复
进击的技术宅
点赞点赞
2022-11-03
共3条回复

2周刷完100道前端优质面试真题 双越最新力作

『前端面试真题100道』视频详解

1509 学习 · 642 问题

查看课程