Reputation: 11
I am working on a command line application that uses yargs to process options passed to that application and operates in a variety of languages.
Testing fails when using non English locales because the expected results for the test are hard coded in that language.
For example the following code returns a passed test in English, however will fail if the language is set to French as Yargs is returning the error in French.
it('call config with no arguments', () => {
return app('config')
.then(out => expect(out).to.include('Not enough non-option arguments: got 0, need at least 1'))
})
This brings me to my question. What is best practice to handle a situation like this? I have been digging into Yargs and even the y18n package that it uses for translation and see no necessarily straight forward way to handle this problem.
I imagine a possible solution would be to have the string that the test expects be translated perhaps via the y18n library, or perhaps having a test for every supported language, however that could be quite overkill.
What have you done in such a situation? I would love to know!
Thanks!
Upvotes: 1
Views: 1159
Reputation: 34873
Yargs has a locale()
method
which you could use to set the locale during testing.
One way to do this with a local wrapper module would be:
app.js
:
const yargs = require('./my-yargs');
...
args = yargs.option(...)...
...
my-yargs.js
:
let yargs = require('yargs');
if (process.env.NODE_ENV === 'test') {
yargs = yargs.locale('en')
}
module.exports = yargs;
Which would result in the following output:
$ LC_ALL=fr_FR.UTF-8 node test.js --help
Options:
--help Affiche de l'aide [booléen]
--version Affiche le numéro de version [booléen]
$ NODE_ENV=test LC_ALL=fr_FR.UTF-8 node test.js --help
Options:
--help Show help [boolean]
--version Show version number [boolean]
Upvotes: 1