1.2.0 • Published 10 months ago

jfixtures v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

jfixtures

This module allows you to inject dependencies to methods, by argument name.

Simple example:

import * as jestFixtures from 'jfixtures'

class Fixtures {
    @jestFixtures.fixture
    dateMock(): Date {
        return new Date();
    }
}

jestFixtures.test('When called => return ok', async (dateMock: Date) => {
    console.log(dateMock, 'is available!');
});

Fixtures could use other fixtures!

import * as jestFixtures from 'jfixtures'

import * as mongodb from 'mongodb';

import * as exampleDAO from '../example';

class Fixtures {
   @jestFixtures.fixture
    mongoClient(): mongodb.MongoClient {
        return new mongodb.MongoClient('no-such-mongo-server');
    }

    @jestFixtures.fixture
    exampleDB(mongoClient: mongodb.MongoClient): mongodb.Db {
        return mongoClient.db();
    }

    @jestFixtures.fixture
    exampleCollection(mongoClient: mongodb.MongoClient): mongodb.Collection {
        const collection = mongoClient.db().collection('');

        collection.find = jest.fn();
        collection.findOne = jest.fn();
        collection.deleteOne = jest.fn();

        return collection;
    }

    @jestFixtures.fixture
    exampleDao(mongoClient: mongodb.MongoClient): exampleDAO.ExampleDAO {
        return new exampleDAO.ExampleDAO(mongoClient);
    }
}

jestFixtures.test('When called => return ok', async (exampleDao: exampleDAO.ExampleDAO) => {
    console.log(exampleDao, 'is available!');
});
1.2.0

10 months ago

1.1.0

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago