Skip to content

Commit

Permalink
Switch from Mocha to Ava for faster tests (#289)
Browse files Browse the repository at this point in the history
* Switch from Mocha to Ava

* Concurrency: 5
  • Loading branch information
jmdobry authored and Ace Nassri committed Nov 17, 2022
1 parent d1d95c0 commit 631e2ce
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 93 deletions.
66 changes: 36 additions & 30 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,44 @@

'use strict';

require(`../../system-test/_setup`);

const proxyquire = require(`proxyquire`).noPreserveCache();
const translate = proxyquire(`@google-cloud/translate`, {})();

describe(`translate:quickstart`, () => {
it(`should translate a string`, (done) => {
const string = `Hello, world!`;
const expectedTranslation = `Привет мир!`;
const targetLanguage = `ru`;
const translateMock = {
translate: (_string, _targetLanguage) => {
assert.equal(_string, string);
assert.equal(_targetLanguage, targetLanguage);

return translate.translate(_string, _targetLanguage)
.then((results) => {
const translation = results[0];
assert.equal(translation, expectedTranslation);

setTimeout(() => {
assert.equal(console.log.callCount, 2);
assert.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
assert.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]);
done();
}, 200);

return results;
});
}
};

proxyquire(`../quickstart`, {
'@google-cloud/translate': sinon.stub().returns(translateMock)
});
test.before(stubConsole);
test.after(restoreConsole);

test.cb(`should translate a string`, (t) => {
const string = `Hello, world!`;
const expectedTranslation = `Привет мир!`;
const targetLanguage = `ru`;
const translateMock = {
translate: (_string, _targetLanguage) => {
t.is(_string, string);
t.is(_targetLanguage, targetLanguage);

return translate.translate(_string, _targetLanguage)
.then(([translation]) => {
t.is(translation, expectedTranslation);

setTimeout(() => {
try {
t.is(console.log.callCount, 2);
t.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
t.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]);
t.end();
} catch (err) {
t.end(err);
}
}, 200);

return [translation];
});
}
};

proxyquire(`../quickstart`, {
'@google-cloud/translate': sinon.stub().returns(translateMock)
});
});
111 changes: 48 additions & 63 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

'use strict';

const Translate = require(`@google-cloud/translate`);
require(`../../system-test/_setup`);

const translate = require(`@google-cloud/translate`)();
const path = require(`path`);
const run = require(`../../utils`).run;

const cwd = path.join(__dirname, `..`);
const cmd = `node translate.js`;
Expand All @@ -26,72 +27,56 @@ const text2 = `Goodbye!`;
const model = `nmt`;
const toLang = `ru`;

describe(`translate:translate`, () => {
const translate = Translate();

it(`should detect language of a single string`, () => {
const output = run(`${cmd} detect "${text}"`, cwd);
return translate.detect(text)
.then((results) => {
const expected = `Detections:\n${text} => ${results[0].language}`;
assert.equal(output, expected);
});
});
test(`should detect language of a single string`, async (t) => {
const output = await runAsync(`${cmd} detect "${text}"`, cwd);
const [detection] = await translate.detect(text);
const expected = `Detections:\n${text} => ${detection.language}`;
t.is(output, expected);
});

it(`should detect language of multiple strings`, () => {
const output = run(`${cmd} detect "${text}" "${text2}"`, cwd);
return translate.detect([text, text2])
.then((results) => {
const expected = `Detections:\n${text} => ${results[0][0].language}\n${text2} => ${results[0][1].language}`;
assert.equal(output, expected);
});
});
test(`should detect language of multiple strings`, async (t) => {
const output = await runAsync(`${cmd} detect "${text}" "${text2}"`, cwd);
const [detections] = await translate.detect([text, text2]);
const expected = `Detections:\n${text} => ${detections[0].language}\n${text2} => ${detections[1].language}`;
t.is(output, expected);
});

it(`should list languages`, () => {
const output = run(`${cmd} list`, cwd);
assert.equal(output.includes(`Languages:`), true);
assert.equal(output.includes(`{ code: 'af', name: 'Afrikaans' }`), true);
});
test(`should list languages`, async (t) => {
const output = await runAsync(`${cmd} list`, cwd);
t.true(output.includes(`Languages:`));
t.true(output.includes(`{ code: 'af', name: 'Afrikaans' }`));
});

it(`should list languages with a target`, () => {
const output = run(`${cmd} list es`, cwd);
assert.equal(output.includes(`Languages:`), true);
assert.equal(output.includes(`{ code: 'af', name: 'afrikáans' }`), true);
});
test(`should list languages with a target`, async (t) => {
const output = await runAsync(`${cmd} list es`, cwd);
t.true(output.includes(`Languages:`));
t.true(output.includes(`{ code: 'af', name: 'afrikáans' }`));
});

it(`should translate a single string`, () => {
const output = run(`${cmd} translate ${toLang} "${text}"`, cwd);
return translate.translate(text, toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
assert.equal(output, expected);
});
});
test(`should translate a single string`, async (t) => {
const output = await runAsync(`${cmd} translate ${toLang} "${text}"`, cwd);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
t.is(output, expected);
});

it(`should translate multiple strings`, () => {
const output = run(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
return translate.translate([text, text2], toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
assert.equal(output, expected);
});
});
test(`should translate multiple strings`, async (t) => {
const output = await runAsync(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
const [translations] = await translate.translate([text, text2], toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`;
t.is(output, expected);
});

it(`should translate a single string with a model`, () => {
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
return translate.translate(text, toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
assert.equal(output, expected);
});
});
test(`should translate a single string with a model`, async (t) => {
const output = await runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
t.is(output, expected);
});

it(`should translate multiple strings with a model`, () => {
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
return translate.translate([text, text2], toLang)
.then((results) => {
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
assert.equal(output, expected);
});
});
test(`should translate multiple strings with a model`, async (t) => {
const output = await runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
const [translations] = await translate.translate([text, text2], toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`;
t.is(output, expected);
});

0 comments on commit 631e2ce

Please sign in to comment.