如何使用 Jest 单元测试覆盖 TypeORM @Column 装饰器?

2024-03-06

我希望尽可能多地对我的应用程序进行单元和端到端测试,我的目标是覆盖率达到 101%。我的设置现在的问题是,typeorm 的 @Column 装饰器使用箭头函数来设置默认值,例如数据库更新的当前时间戳。这个箭头函数没有被玩笑测试覆盖。消息是:statement not covered

我运行代码覆盖率:jest --coverage。 我的版本:

"jest": "^24.9.0",
"typeorm": "^0.2.20"

package.json 中的 Jest 配置:

{
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../build/coverage",
    "testEnvironment": "node",
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": -10
      }
    }
  },
}

我的实体看起来像这样:

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Role {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    tenantId: number;

    @Column({ type: 'timestamp', update: false, default: () => 'CURRENT_TIMESTAMP()' })
    createdAt: Date;

    @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP()', onUpdate: 'CURRENT_TIMESTAMP()' })
    updatedAt: Date;
}

该实体的覆盖范围:


I ran into a similar issue with GraphQL decorators. As all of these are functions, what you can do is create a file that holds all of the functions you'll be using, name them, and export them, so that you can actually test them as well and get the coverage with Jest. (The tests should literally be something like expect(namedFunction).toBe('CURRENT_TIMESTAMP()') or very similar. For an example you can see my function here https://github.com/jmcdo29/zeldaPlay/blob/master/apps/api/src/app/models/gqlReturns.ts and my tests here https://github.com/jmcdo29/zeldaPlay/blob/master/apps/api/src/app/models/graphql-returns.spec.ts.

编辑 11/03/2020

代码示例不再了。话虽这么说,你可以做类似的事情

export const returnString = () => String;

@Query(returnString)

对于 GraphQL。对此的测试如下所示

expect(returnString()).toBe(String);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Jest 单元测试覆盖 TypeORM @Column 装饰器? 的相关文章

随机推荐