Git Product home page Git Product logo

Comments (5)

LoicPoullain avatar LoicPoullain commented on May 10, 2024 2

Hi @PatrickDorneles ,

The best way to achieve this is to use a test database and add some fake data before the test. Here's an example with a SQLite db:

describe('UserService', () => {

  let service: UserService;
  let connection: Connection;

  // Create a connection to the database before running all the tests.
  before(async () => {
    connection = await createConnection({
      // Choose a test database. You don't want to run your tests on your production data.
      database: './test_db.sqlite3',
      // Drop the schema when the connection is established.
      dropSchema: true,
      // Register the models that are used.
      entities: [ User ],
      // Auto create the database schema.
      synchronize: true,
      // Specify the type of database.
      type: 'sqlite',
    });
    const user = new User();
    user.name = 'Mary';
    await connection.manager.save(user);
  });

  // Close the database connection after running all the tests whether they succeed or failed.
  after(() => connection.close());

  it('should have a getUsers method which returns the users of the database', async () => {
    service = createService(UserService);
    const users = await service.getUsers();
    
    strictEqual(users.length, 1);
    strictEqual(users[0].name, 'Mary');
  });

});

Note in this example that if another service (or a controller) uses the UserController, you can test it by mocking the UserService:

// my-other.service.ts
class MyOtherService {
  @dependency
  userService: UserService
}

// my-other.service.spec.ts
class MockUserService {
   async getUsers() {
    return [];
  }
}

...
myOtherService = createService(MyOtherService, { userService: MockUserService }); 
...

Does it help?

from foal.

LoicPoullain avatar LoicPoullain commented on May 10, 2024

My bad, your example was to register a user, not getting them. It's the same logic though

from foal.

LoicPoullain avatar LoicPoullain commented on May 10, 2024

The dropSchema option lets you empty your database when establishing the connection (see https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md).

from foal.

LoicPoullain avatar LoicPoullain commented on May 10, 2024

connection.getRepository(User).clear() may also be useful if you have several tests.

From the comments of TypeORM:

    /**
     * Clears all the data from the given table/collection (truncates/drops it).
     *
     * Note: this method uses TRUNCATE and may not work as you expect in transactions on some platforms.
     * @see https://stackoverflow.com/a/5972738/925151
     */
    clear(): Promise<void>;

from foal.

LoicPoullain avatar LoicPoullain commented on May 10, 2024

Hi @PatrickDorneles,

Did it solve your problem? Can I close this?

Thanks!

from foal.

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.