簡體   English   中英

Jest 覆蓋未檢測到 node.js 中的某些文件

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

我正在用 node、typescript 和 typeorm 做一個小項目。 我創建了一些文件,基本上是為了創建約會和用戶,並創建了測試文件。 問題是,我無法讓這些文件顯示在開玩笑的覆蓋率報告中。 我會寫下這些文件:

開玩笑的配置文件

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

}

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

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

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',
    });
  });
});

我用來開玩笑的命令: yarn jest --coverage --watchAll --no-cache --runInBand

以及覆蓋范圍的結果:

$ 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.

如您所見,它忽略了 users.routes.ts 和 index.ts 的完成。 我嘗試了幾件事,但似乎沒有任何效果。 有人可以幫助我嗎?

我最近遇到了類似的問題。 更新到最新版本的 Jest 似乎解決了這個問題。 您可以在此處找到使用 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