fixture-lite
TypeScript icon, indicating that this package has built-in type declarations

0.0.17 • Public • Published

fixture-lite

fixture-lite A lightweight, flexible and powerful tool that allows you to conveniently create the data needed for unit, integration and e2e tests.
The library can work without using any ORMs by creating objects of specified prototypes. If necessary, you can use the ready-made integration with TypeORM.

Documentation

Installation

Install fixture-lite:

npm i fixture-lite

Simple using

  1. Setting up the factory assembly in the project.

At the very beginning, we need to configure the factory function manager, with which the library will be able to assemble all the factories in your project for future use.

// setup-files/setup-factories.ts
FixtureManager.factories.load({
    srcPath: path.join(__dirname, '..', '..', 'src'),
    filePrefix: '.factory.ts'
})

This code must be executed before running all tests, for example in the files specified in the setupFiles (if you use Jest).
You can skip this step and register factories right before running the tests (for example in beforeEach)

Note that using load, the library builds factories for the entire project, rather than a specific module, so you don't have to call it several times before running tests.

  1. Create a factory

Now we need to create a file that will store a factory with which fixture-lite will create instances of the class you need in the tests in the future.

// ../../src/module/tests/factories/user.factory.ts
FixtureManager.factories.add(UserEntity, async (faker, generator) => {
    return {
        id: uuid(),
        name: faker.person.fullName(),
        email: faker.internet.email(),
        profile: await generator.entity(ProfileEntity).single(),
    };
});

Inside the factory function, we also have access to Faker for convenient generation of fake entity data and generator with which we can create nested entities.

  1. Test preparation

Package Sidebar

Install

npm i fixture-lite

Weekly Downloads

2

Version

0.0.17

License

MIT

Unpacked Size

30.5 kB

Total Files

38

Last publish

Collaborators

  • plac