bahmutov / bdd-stdin

Utility for easily feeding mock responses to unit tests that require command line input from the user
39 stars 4 forks source link

Race condition when using Inquirer.js #2

Open m-a-r-c-e-l-i-n-o opened 8 years ago

m-a-r-c-e-l-i-n-o commented 8 years ago

Running the code below sometimes works and sometimes doesn't.

BDDStdin('hello\n', 'hello\n')
const questions = [{
        type: 'input',
        name: 'projectName',
        message: 'What\'s the name of your project?',
        default: function () {
            return 'hey'
        }
    },
    {
        type: 'input',
        name: 'projectName2',
        message: 'What\'s the name of your project again?',
        default: function () {
            return 'hey2'
        }
    }
]
const prompt = Inquirer.prompt(questions)
prompt.then(answers => {
    expect(answers.projectName).toBe('hey')
    expect(answers.projectName2).toBe('hey2')
    done()
})

It seems like bdd-stdin doesn't quite know when the question shows up. Not sure if it's an interference of some sort with Inquire.js or a bug, but changing https://github.com/bahmutov/bdd-stdin/blob/master/index.js#L31 to something like 500 makes everything okay (at least as far as I have witnessed). Any suggestions on how this could be sustainably resolved?

bahmutov commented 8 years ago

seems like this is a race condition that I did not prepare for. If you could come up with a self-contained unit tests, that would be great to merge in and fix.

m-a-r-c-e-l-i-n-o commented 8 years ago

@bahmutov Thanks for the response, are you suggesting a self-contained unit test involving Inquirer?

bahmutov commented 8 years ago

yeah, if possible, otherwise it is hard to debug this

m-a-r-c-e-l-i-n-o commented 8 years ago

The pr is ready to fire, but I just realized that the issue is already present in your checkbox-spec.js spec. Just upgrade your inquirer dependency to v1 and it'll surface. Let me know if you want me to pr either way.