Ren*_*don 5 unit-testing typescript jestjs typeorm nestjs
I want to Unit and e2e test my applications as much as possible and my goal is a coverage of 101%. Problem right now with my setup is, that the @Column decorator from typeorm uses an arrow function to set a default value like the current timestamp on a database update. This arrow function is not covered with jest test coverage. Message is: statement not covered
I run the code coverage with: jest --coverage.
My versions:
"jest": "^24.9.0",
"typeorm": "^0.2.20"
Run Code Online (Sandbox Code Playgroud)
Jest configuration within package.json:
{
"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
}
}
},
}
Run Code Online (Sandbox Code Playgroud)
My entity looks like this:
"jest": "^24.9.0",
"typeorm": "^0.2.20"
Run Code Online (Sandbox Code Playgroud)
Coverage for this entity:
(to post an image i need 10 reputation -.-): https://i.ibb.co/FYQgstP/Screenshot-2019-11-07-11-46-45.png
我遇到了与 GraphQL 装饰器类似的问题。由于所有这些都是函数,您可以做的是创建一个文件,其中包含您将使用的所有函数、命名并导出它们,以便您也可以实际测试它们并使用 Jest 获得覆盖范围。(测试字面上应该类似expect(namedFunction).toBe('CURRENT_TIMESTAMP()')或非常相似。例如,您可以在此处查看我的函数和在此处查看我的测试。
代码示例不再赘述。话虽如此,你可以做这样的事情
export const returnString = () => String;
@Query(returnString)
Run Code Online (Sandbox Code Playgroud)
对于 GraphQL。对此的测试看起来像
expect(returnString()).toBe(String);
Run Code Online (Sandbox Code Playgroud)