Покрытие кода для функции стрелки в контексте Express - PullRequest
0 голосов
/ 04 октября 2019

Как добиться покрытия кода для строки, содержащей контекст?

import { ApolloServer } from "apollo-server-express"
const server = new ApolloServer({
    context: ({ req, res }) => ({ req, res }),
})

Я могу написать тест, который не проходит, если эта строка отсутствует, однако сама по себе она не убеждает покрытие кода от Jest, чтобы жаловаться,В частности, он говорит об этой строке:

  • оператор не распространяется
  • функция не распространяется

1 Ответ

1 голос
/ 04 октября 2019

Вот решение:

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

...