This package provides a function called muddle that accepts a
string and produces a string resembling the original but with
random substitutions of alphanumeric characters. This produces
a result that differs from the input but in many cases preserves
relevant aspects of the string format.
For details, see readme.test.ts.
This can be useful to improve test coverage. Consider for example a test for an authentication system that goes something like this:
-
Invoke a service under test to create an resource that should be accessible only to the holder of a password which the service generates and returns from the creation action.
-
Make a request for the resource using the wrong password, and assert that the service rejected the request.
Does it matter what value the test uses as its incorrect password? Perhaps. Suppose the service only generates hexadecimal UUID passwords and its authentication routine goes as follows:
-
Parse the given password as a hexadecimal UUID; if it is not in this format, reject the request.
-
Compare the given password to the actual password, and reject the request if the values do not match.
If the test chooses as its incorrect password an arbitrary value
like xyz or a random string of numbers, the request will always be
rejected at step 1, and step 2 will have no test coverage. If the
service exhibits erroneous behavior in the second step which would
prevent it from rejecting a password with a correct format but
incorrect value, this problem would not be caught by the test.
Instead of choosing a value arbitrarily, we can start with the
correct password and muddle it to produce an incorrect one. The
muddled string will differ from the original, but it will often
still be a "realistic"-looking value.