Jest integration test - Nodejs API + Mongo

Asked

Viewed 608 times

0

I would like a help in my tests, because I am doing something wrong because there is time that my test passes and soon after it does not pass more. Below are some of the two tests I have of my study application.

const request = require('supertest');

const { Category } = require('../../src/models/category');

describe('/categories', () => {
  let server;

  beforeEach(async () => {
    server = require('../../index');
    const categories = [{ name: 'Electronics' }, { name: 'Books' }];

    await Category.deleteMany({});
    await Category.insertMany(categories);
  });

  afterEach(async () => {
    await Category.deleteMany({});
  });

  describe('GET /', () => {
    it('should return all categories', async () => {
      const response = await request(server).get('/categories');
      expect(response.status).toBe(200);
      expect(response.body.length).toBe(2);
    });
  });

  describe('POST /', () => {
    it('should register a new category', async () => {
      const category = { name: 'Video Game' };

      const response = await request(server)
        .post('/categories')
        .send(category);

      expect(response.status).toBe(201);
      expect(response.body).toHaveProperty('name', category.name);
    });
  });
});

Category integration test - Gist

const request = require('supertest');

const { Product } = require('../../src/models/product');
const { Category } = require('../../src/models/category');

describe('/products', () => {
  let server;
  const defaultCategoryId = '56cb91bdc3464f14678934ca';

  const cleanDatabase = async () => {
    await Category.deleteMany({});
    await Product.deleteMany({});
  };

  beforeEach(async () => {
    server = require('../../index');
    await cleanDatabase();

    let category = new Category({ name: 'Movie', _id: defaultCategoryId });
    category = await category.save();

    const product = {
      name: 'Star Wars',
      description: 'Star Wars: The Last Jedi',
      category: {
        _id: category._id,
        name: category.name,
      },
      price: 14.99,
    };
    await Product.insertMany([product]);
  });

  afterEach(async () => await cleanDatabase());

  describe('GET /', () => {
    it('should return all products', async () => {
      const response = await request(server).get('/products');

      expect(response.status).toBe(200);
      expect(response.body.length).toBe(1);
    });
  });

  describe('POST /', () => {
    it('should register a new product', async () => {
      const product = {
        name: 'Solo',
        description: 'A Star Wars story',
        categoryId: defaultCategoryId,
        price: 17.75,
      };

      const response = await request(server)
        .post('/products')
        .send(product);

      expect(response.status).toBe(201);
      expect(response.body).toHaveProperty('name', product.name);
    });
  });
});

Product integration testing - Gist

Teste passando

Passing test

Teste falhando

Test failing (Without changing anything in the implementation just running again)

If anyone can help me, I’d really appreciate it.

Github repository

1 answer

0


It may be conflicting with the tests, because they don’t run in sequence by default, so when you create the instances in the bank, there may be duplicity. For this, in your package.json, pass a parameter to the test script: "test": "NODE_ENV=test jest --verbose --forceExit --runInBand"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.