Giter VIP home page Giter VIP logo

Comments (4)

vschoener avatar vschoener commented on May 30, 2024 1

I forgot to say but thank you for the v2, really good job ;)

from nestjs-console.

Rmannn avatar Rmannn commented on May 30, 2024

Hi, this is a really good question.

The best way to test commands with promise is to use the exitOverride method of commander.

Calling command.exitOverride() on a command will disable the normal process.exit(). Commander will throw a CommanderError instead.
Since process.exit is mocked, the one called from your command is ignored, and the default one called by commander will throw a CommanderError.

import { CommanderError } from 'commander';
// ...
it('should succeed and create the index', async () => {
    try {
        // override the normal exit of commander (cli is the root command)
        const consoleService = app.get<ConsoleService>(ConsoleService);
        consoleService.getCli().exitOverride();

        boot([process.argv0, 'node', '01-actor-id-actor-type-index', 'update']);
    } catch (e) {
        // commander will throw a CommanderError on first exit
        expect(e).toBeInstanceOf(CommanderError);
        // expect(e.message).toBe('(outputHelp)');
        // expect(e.code).toBe('commander.help');
        // expect(e.exitCode).toBe(0);

        expect(await snapsCollection.listIndexes().toArray()).toEqual([
            {
                v: 2,
                key: {
                    _id: 1
                },
                name: '_id_',
                ns: 'snapping.snaps'
            },
            {
                v: 2,
                key: {
                    actor_id: 1,
                    actor_type: 1
                },
                name: 'actor_id_1_actor_type_1',
                ns: 'snapping.snaps',
                background: true
            }
        ]);
    }
});

I did not test it in the v1 branch, but after reading your question i think it's really important to implement a better approach.
Maybe making the method boot returning a Promise would be a better solution. I will try it in the v2 branch.

from nestjs-console.

vschoener avatar vschoener commented on May 30, 2024

That would awesome, I did look over your v2 branch to see if it was already done but it wasn't :D
If you can architecture the code the way to run a specific command and get back to the starting point, that would be awesome, even to close all the connection that could be present and initiated from the container (forRoot connection for example, or maybe you app.close after the boot call? I didn't look over the source

from nestjs-console.

Rmannn avatar Rmannn commented on May 30, 2024

v2 is now published, promise and errors are tested.

from nestjs-console.

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.