Express上下文中箭头功能的代码覆盖范围
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Express上下文中箭头功能的代码覆盖范围相关的知识,希望对你有一定的参考价值。
如何实现包含上下文的行的代码覆盖率?
import { ApolloServer } from "apollo-server-express"
const server = new ApolloServer({
context: ({ req, res }) => ({ req, res }),
})
如果此行不存在,我可以编写一个失败的测试,但是,它本身无法说服Jest进行代码覆盖来投诉。具体来说,它说的是那条线:
- 声明未涵盖
- 功能未涵盖
答案
这里是解决方法:
server.ts
:
import { ApolloServer, gql } from 'apollo-server-express';
const typeDefs = gql`
type Query {
_: Boolean
}
`;
function contextFunction({ req, res }) {
return { req, res };
}
const server = new ApolloServer({
typeDefs,
context: contextFunction,
});
export { server, contextFunction };
server.spec.ts
:
import { ApolloServer } from 'apollo-server-express';
import { server, contextFunction } from './server';
describe('server', () => {
it('should initialize apollo server', () => {
expect(server).toBeInstanceOf(ApolloServer);
});
it('should create context', () => {
const mockedReq = {};
const mockedRes = {};
const actualValue = contextFunction({ req: mockedReq, res: mockedRes });
expect(actualValue).toEqual({ req: mockedReq, res: mockedRes });
});
});
具有100%覆盖率报告的单元测试结果:
PASS stackoverflow/58226940/server.spec.ts
server
✓ should initialize apollo server (4ms)
✓ should create context (1ms)
-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
server.ts | 100 | 100 | 100 | 100 | |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 4.484s, estimated 6s
源代码:https://github.com/mrdulin/apollo-graphql-tutorial/tree/master/stackoverflow/58226940
以上是关于Express上下文中箭头功能的代码覆盖范围的主要内容,如果未能解决你的问题,请参考以下文章