Code Monkey home page Code Monkey logo

Comments (6)

daffl avatar daffl commented on July 18, 2024 1

I do but in my experience it is not a great idea to test against a different database than the one you are running in production or trying to be smarter about bootstrapping a test database. If you are not happy with cleaning up and seeding it on every test run you can still make sure that each test cleans up its own data:

describe('\'users\' service', () => {
  it('registered the service', () => {
    const service = app.service('users');
    expect(service).toBeTruthy();
  });

  it('creates a user', async () => {
    const user = await app.service('users').create({
      email: '[email protected]',
      password: 'VerySecretP@assw0rd',
    });

    expect(user.password).not.toEqual('VerySecretP@assw0rd'');

    await app.service('users').remove(user._id);
  });
});

from docs.

daffl avatar daffl commented on July 18, 2024

This depends on the database. With NeDB yes, the most reliable way would be removing the data folder by adding shx rm -rf data/ to your test script.

from docs.

soullivaneuh avatar soullivaneuh commented on July 18, 2024

@daffl Yes I'm already doing that:

"clean": "shx rm -rf test/data/",
"jest": "npm run clean && NODE_ENV=test npm run seed && NODE_ENV=test jest --coverage --detectOpenHandles --forceExit"

But how am I supposed to deal with it when I have a single test to run?

npx jest test/services/users.test.ts 

It would be great to have something that "reset" the created data of each test.

This would be the NodeJS equivalent of this great PHP tool : https://packagist.org/packages/dama/doctrine-test-bundle

from docs.

soullivaneuh avatar soullivaneuh commented on July 18, 2024

This also why I have the npm run seed command to generate data before testing.

I would like to get rid of it and see each test load data independently and cleanup.

Linked to memory database would be the best.

Do you see my point of view?

from docs.

soullivaneuh avatar soullivaneuh commented on July 18, 2024

Yes, I will maybe do it. But this assume additional repetitive code to produce, I would like to find a more lazy way. 👍

from docs.

daffl avatar daffl commented on July 18, 2024

Imo, tests are the one place where repetitive code is ok. I've seen many a time when someone tried to be lazy and the test code became unmaintainable.

from docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.