简体   繁体   English

Jest 覆盖未检测到 node.js 中的某些文件

[英]Jest coverage not detecting some files in node.js

I'm doing a small project with node, typescript and typeorm.我正在用 node、typescript 和 typeorm 做一个小项目。 I created some files basically to create appointments and users and created test files.我创建了一些文件,基本上是为了创建约会和用户,并创建了测试文件。 The problem is, i can't make these files to show in jest coverage report.问题是,我无法让这些文件显示在开玩笑的覆盖率报告中。 I'll write down the files:我会写下这些文件:

jest.config.js开玩笑的配置文件

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  collectCoverageFrom: [
    "**/*.{ts,tsx}",
    "!**/node_modules/**",
    "!**/vendor/**"
  ]

}

src/routes/users.routes.ts src/routes/users.routes.ts

import { Router } from 'express';
import CreateUserService from '../services/CreateUserService';

const usersRouter = Router();

usersRouter.post('/', async (request, response) => {
  try {
    const { name, email, password } = request.body;

    const createUser = new CreateUserService();

    if (!name) {
      return response.status(400).json({ error: 'Name not informed' });
    }
    if (!email) {
      return response.status(400).json({ error: 'Email not informed' });
    }
    if (!password) {
      return response.status(400).json({ error: 'Password not informed' });
    }

    const user = await createUser.execute({
      name,
      email,
      password,
    });

    const { password: userPassword, ...userResponse } = user;

    return response.send(userResponse);
  } catch (err) {
    return response.status(400).json({ error: err.message });
  }
});

export default usersRouter;

src/routes/index.ts src/routes/index.ts

import { Router } from 'express';
import appointmentsRouter from './appointments.routes';
import usersRouter from './users.routes';

const routes = Router();

routes.use('/users', usersRouter);
routes.use('/appointments', appointmentsRouter);

export default routes;

src/routes/users.routes.test.ts src/routes/users.routes.test.ts

import request from 'supertest';
import { createConnection, getConnection } from 'typeorm';

describe('appointments routes test', () => {
  beforeAll(async () => {
    await createConnection('development');
    await getConnection('development')
      .createQueryBuilder()
      .delete()
      .from('users')
      .execute();
  });
  afterAll(async () => {
    await getConnection('development')
      .createQueryBuilder()
      .delete()
      .from('users')
      .execute();
    const connection = getConnection('development');
    connection.close();
  });

  it('post new user without name', async () => {
    const response = await request('localhost:3333').post('/users').send({});
    expect(response.status).toBe(400);
    expect(response.body).toEqual({ error: 'Name not informed' });
  });
  it('post new user without email', async () => {
    const response = await request('localhost:3333')
      .post('/users')
      .send({ name: 'Gustavo' });
    expect(response.status).toBe(400);
    expect(response.body).toEqual({ error: 'Email not informed' });
  });
  it('post new user without password', async () => {
    const response = await request('localhost:3333')
      .post('/users')
      .send({ name: 'Gustavo', email: 'ga@gmail.com' });
    expect(response.status).toBe(400);
    expect(response.body).toEqual({ error: 'Password not informed' });
  });
  it('post new valid user', async () => {
    const response = await request('localhost:3333').post('/users').send({
      name: 'Gustavo',
      email: 'ga@gmail.com',
      password: '12345',
    });
    expect(response.status).toBe(200);
    expect(response.body).toMatchObject({
      name: 'Gustavo',
      email: 'ga@gmail.com',
    });
  });
});

The command I'm using for jest: yarn jest --coverage --watchAll --no-cache --runInBand我用来开玩笑的命令: yarn jest --coverage --watchAll --no-cache --runInBand

And the result for the coverage:以及覆盖范围的结果:

$ jest --coverage --watchAll --no-cache --runInBand
 PASS  src/routes/appointments.routes.test.ts (6.082 s)
 PASS  src/routes/users.routes.test.ts
---------------------------------|---------|----------|---------|---------|-------------------
File                             | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------------------------|---------|----------|---------|---------|-------------------
All files                        |      20 |        0 |    6.67 |   19.12 |                   
 src                             |       0 |      100 |       0 |       0 |                   
  server.ts                      |       0 |      100 |       0 |       0 | 1-16              
 src/database                    |       0 |        0 |       0 |       0 |                   
  index.ts                       |       0 |        0 |       0 |       0 | 1-10              
 src/database/migrations         |   33.33 |      100 |       0 |   33.33 |                   
  ...86628-CreateAppointments.ts |      40 |      100 |       0 |      40 | 6-42              
  1604083144371-CreateUsers.ts   |      40 |      100 |       0 |      40 | 5-46              
  ...roviderFieldToProviderID.ts |      25 |      100 |       0 |      25 | 11-38             
 src/models                      |     100 |      100 |     100 |     100 |                   
  Appointment.ts                 |     100 |      100 |     100 |     100 |                   
  User.ts                        |     100 |      100 |     100 |     100 |                   
 src/repositories                |       0 |        0 |       0 |       0 |                   
  AppointmentsRepository.ts      |       0 |        0 |       0 |       0 | 1-15              
 src/routes                      |       0 |        0 |       0 |       0 |                   
  appointments.routes.ts         |       0 |        0 |       0 |       0 | 1-55              
  index.ts                       |       0 |      100 |     100 |       0 | 1-10              
  users.routes.ts                |       0 |        0 |       0 |       0 | 1-36              
 src/services                    |       0 |        0 |       0 |       0 |                   
  CreateAppointmentService.ts    |       0 |        0 |       0 |       0 | 1-40              
  CreateUserService.ts           |       0 |        0 |       0 |       0 | 1-38              
---------------------------------|---------|----------|---------|---------|-------------------

Test Suites: 2 passed, 2 total
Tests:       12 passed, 12 total
Snapshots:   0 total
Time:        8.352 s
Ran all test suites.

As you can see, it ignores completes the users.routes.ts and index.ts.如您所见,它忽略了 users.routes.ts 和 index.ts 的完成。 I tried several things but nothing seems to work.我尝试了几件事,但似乎没有任何效果。 Can somebody help me?有人可以帮助我吗?

I recently ran into a similar issue.我最近遇到了类似的问题。 Updating to the most recent version of Jest seemed to fix the problem.更新到最新版本的 Jest 似乎解决了这个问题。 You can find the docs for using yarn to upgrade a package here yarn v2.x or here yarn v1.x .您可以在此处找到使用 yarn 升级软件包的文档yarn v2.x或此处yarn v1.x

只需清除您的笑话缓存: yarn jest --clearCachenpx jest --clearCache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM