rochejul / sequelize-mocking

Sequelize extension to deal with data-mocking for testing
MIT License
63 stars 26 forks source link

Using this with Tape #5

Closed alexdrans closed 7 years ago

alexdrans commented 7 years ago

Hi,

I write my tests using tape. Would it be straight forward enough to get this module working with it?

rochejul commented 7 years ago

Hi

It could be interesting.

I have used other stuff from substack but never its own testing framework.

I can try to look how to integrate it when I found some times.

Many thank for your feedback

Regards

alexdrans commented 7 years ago

Awesome, good news.

Anything I can help with?

rochejul commented 7 years ago

If you want to give me a pull request, this is a good idea :)

alexdrans commented 7 years ago

tape doesn't have explicit before, beforeEach, after or afterEach functions and so I got thinking about how to implement sequelize-mocking for tape.

I figured that I could expose the functions of the module to be used manually, like this.

alexdrans commented 7 years ago

For tape specifically, it would be implemented like this

rochejul commented 7 years ago

I will take a look of your feedbacks

Thanks !

alexdrans commented 7 years ago

Had a chance to take a look?

rochejul commented 7 years ago

Hi @alexdrans

I'm sorry, I have a lot of work at my job and few times at home

I have finished to look on some tasks on another project. I hope to look soon this point

Sorry

Regards

rochejul commented 7 years ago

Hi

I start to work on it.

I think it should be:

/**
 * Using SequelizeMocking with tape easily
 *
 * @module lib/sequelize-mocking-tape
 * @exports sequelizeMochingTape
 * @version 0.1.0
 * @since 0.1.0
 * @author Julien Roche
 */

'use strict';

// Imports
const test = require('tape');

const Sequelize = require('sequelize');
const _ = Sequelize.Utils._;
const SequelizeMocking = require('./sequelize-mocking');

/**
 * @method
 * @private
 * @param {Test} test
 * @param {Function} handler
 * @returns {Function}
 */
function beforeEach(test, handler) {
    return function (name, listener) {
        test(name, function (assert) {
            let _end = assert.end;

            assert.end = function () {
                assert.end = _end;
                listener(assert);
            };

            handler(assert);
        });
    }
}

/**
 * @method
 * @private
 * @param {Test} test
 * @param {Function} handler
 * @returns {Function}
 */
function afterEach(test, handler) {
    return function (name, listener) {
        test(name, function (assert) {
            let _end = assert.end;

            assert.end = function () {
                assert.end = _end;
                handler(assert);
            };

            listener(assert);
        });
    }
}

/**
 * @name sequelizeMochingMocha
 * @param {Sequelize} originalSequelize
 * @param {string} [fixtureFilePath]
 * @param {SequelizeMockingOptions} [options]
 */
module.exports = function (originalSequelize, fixtureFilePath, options) {
    let mockedSequelize = null;
    let newTest = null;

    newTest = beforeEach(test, function (assert) {
        let createFunc = _.partialRight(fixtureFilePath ? SequelizeMocking.createAndLoadFixtureFile : SequelizeMocking.create, options);

        createFunc(originalSequelize, fixtureFilePath)
            .then(function (sequelizeInstance) {
                mockedSequelize = sequelizeInstance;
                assert.end();
            })
            .catch(assert.end);
    });

    newTest = afterEach(newTest, function (assert) {
        SequelizeMocking
            .restore(mockedSequelize, options)
            .then(function () {
                assert.end();
            })
            .catch(assert.end);
    });

    return newTest;
};

I try to publish a feature branch with examples (maybe into the weekend)

Cheers

alexdrans commented 7 years ago

Bump @rochejul