Git Product home page Git Product logo

Comments (2)

xduseko avatar xduseko commented on July 26, 2024 4

You can create custom container for each test case.

const IoC = require('electrolyte');

describe('myModule', function() {
    let container;

    beforeEach(function() {
        container = new IoC.Container();
        container.use(IoC.dir('app/myContainerFolder'));
        myModule = container.create('myModule');
    });
});

... and override modules with mocks like

container.use(function(id) {
    if (id === 'myDep') {
        myDepMock['@literal'] = true;
        return myDepMock;
    }
});

...or create something like this...

from electrolyte.

hoxxep avatar hoxxep commented on July 26, 2024 1

IoC.Container().use uses a hierarchy – the last .use(...) is the first one to be checked for an object. So while using a different container for each test is one option, adding extra .use(...)s during tests is another.

Assuming you have src/models which stores your UserModel, ExampleModel, etc., you can have test/mocks include mocks for ExampleModel; or if you need more refined control test/mocks/ExampleModel will be a directory containing only the ExampleModel mock.

This allows you to use the following in your main application:

const IoC = require('electrolyte');
const container = IoC.Container();
container.use(IoC.node_modules());
container.use(IoC.dir('src/models'));
container.use(IoC.dir('src/controllers'));
container.use(IoC.dir('src/routes'));
// ...

And then in your test set-up, you can either manually instantiate individual models with mocks (semi-ugly):

// IoC code above...
const ExampleController = require('src/controllers/ExampleModel');
const ExampleModelMock = require('test/mocks/ExampleModel');
let controller = new ExampleController(new ExampleModelMock(), IoC.create('UserModel'));

Or, add an extra .use(...) so that a mock called ExampleModel is resolved:

// IoC code above...
container.use(IoC.dir('test/mocks'));  // mocks must be named exactly the same as the original object
let controller = IoC.create('ExampleController');

Feature Request:

Although I would like to see some method that allows hot-swapping of different mocks. Perhaps something similar to

// in your tests bootstrap file:
container.use(IoC.dir('test/mocks'));  // mocks suffixed with 'Mock' such as 'ExampleModelMock'

// in a particular test:
let controller = IoC.create('ExampleController', {
  // override the `ExampleModel` with a mock
  'ExampleModel': IoC.create('ExampleModelMock')
});

from electrolyte.

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.