commitizen / cz-conventional-changelog

A commitizen adapter for the angular preset of https://github.com/conventional-changelog/conventional-changelog
MIT License
766 stars 441 forks source link

Better Tests #125

Open ThisIsMissEm opened 4 years ago

ThisIsMissEm commented 4 years ago

I've been working on a new feature for cz-conventional-changelog (related to #118 ), and I've noticed that all of the tests essentially fake out inquirer, which means that logic like when in inquirer prompts wasn't respected. An alternative testing strategy would be to assume that the inquirer module works as expected and then work with the stdin/stdout streams to do more integration-style testing.

If we assume that git-cz correctly injects an inquirer instance into this module when calling engine(options).prompter(inquirer, commit), then we can actually write tests with:

testPrompt = inquirer.createPromptModule({
  input: readableStream,
  output: writeableStream
});

engine(options).prompter(testPrompt, (message) => {
  // .. do some assertions on message
})

// do some assertions on the prompter by sending data to readableStream and reading from writeableStream

Where readableStream and writeableStream are as defined in the node.js Streams documentation.

The article linked above actually uses the enquirer module, not inquirer, which has a prompt.keypress method to send data in during tests.

For now, I'm considering this as being far too an extensive piece of work to do in my pull request, but would allow us to better test cz-conventional-changelog by having proper regression tests. I have already done a bunch of work to separate the loading of options from the loading of the module, such that the option handling can be tested without mocking out external modules too much (just @commitlint/load)

ThisIsMissEm commented 4 years ago

You can see what I mean by the separation of logic here: https://github.com/ThisIsMissEm/cz-conventional-changelog/pull/1/files#diff-168726dbe96b3ce427e7fedce31bb0bcR94