最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Express上下文中箭头功能的代码覆盖范围

运维笔记admin12浏览0评论

Express上下文中箭头功能的代码覆盖范围

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/mrdulin/apollo-graphql-tutorial/tree/master/stackoverflow/58226940

发布评论

评论列表(0)

  1. 暂无评论