Skip to content

Commit 5df3e5f

Browse files
jmdobryAce Nassri
authored and
Ace Nassri
committed
Switch from Mocha to Ava for faster tests (#289)
* Switch from Mocha to Ava * Concurrency: 5
1 parent 5770a6d commit 5df3e5f

File tree

2 files changed

+84
-93
lines changed

2 files changed

+84
-93
lines changed

translate/system-test/quickstart.test.js

+36-30
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,44 @@
1515

1616
'use strict';
1717

18+
require(`../../system-test/_setup`);
19+
1820
const proxyquire = require(`proxyquire`).noPreserveCache();
1921
const translate = proxyquire(`@google-cloud/translate`, {})();
2022

21-
describe(`translate:quickstart`, () => {
22-
it(`should translate a string`, (done) => {
23-
const string = `Hello, world!`;
24-
const expectedTranslation = `Привет мир!`;
25-
const targetLanguage = `ru`;
26-
const translateMock = {
27-
translate: (_string, _targetLanguage) => {
28-
assert.equal(_string, string);
29-
assert.equal(_targetLanguage, targetLanguage);
30-
31-
return translate.translate(_string, _targetLanguage)
32-
.then((results) => {
33-
const translation = results[0];
34-
assert.equal(translation, expectedTranslation);
35-
36-
setTimeout(() => {
37-
assert.equal(console.log.callCount, 2);
38-
assert.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
39-
assert.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]);
40-
done();
41-
}, 200);
42-
43-
return results;
44-
});
45-
}
46-
};
47-
48-
proxyquire(`../quickstart`, {
49-
'@google-cloud/translate': sinon.stub().returns(translateMock)
50-
});
23+
test.before(stubConsole);
24+
test.after(restoreConsole);
25+
26+
test.cb(`should translate a string`, (t) => {
27+
const string = `Hello, world!`;
28+
const expectedTranslation = `Привет мир!`;
29+
const targetLanguage = `ru`;
30+
const translateMock = {
31+
translate: (_string, _targetLanguage) => {
32+
t.is(_string, string);
33+
t.is(_targetLanguage, targetLanguage);
34+
35+
return translate.translate(_string, _targetLanguage)
36+
.then(([translation]) => {
37+
t.is(translation, expectedTranslation);
38+
39+
setTimeout(() => {
40+
try {
41+
t.is(console.log.callCount, 2);
42+
t.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
43+
t.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]);
44+
t.end();
45+
} catch (err) {
46+
t.end(err);
47+
}
48+
}, 200);
49+
50+
return [translation];
51+
});
52+
}
53+
};
54+
55+
proxyquire(`../quickstart`, {
56+
'@google-cloud/translate': sinon.stub().returns(translateMock)
5157
});
5258
});

translate/system-test/translate.test.js

+48-63
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
'use strict';
1717

18-
const Translate = require(`@google-cloud/translate`);
18+
require(`../../system-test/_setup`);
19+
20+
const translate = require(`@google-cloud/translate`)();
1921
const path = require(`path`);
20-
const run = require(`../../utils`).run;
2122

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

29-
describe(`translate:translate`, () => {
30-
const translate = Translate();
31-
32-
it(`should detect language of a single string`, () => {
33-
const output = run(`${cmd} detect "${text}"`, cwd);
34-
return translate.detect(text)
35-
.then((results) => {
36-
const expected = `Detections:\n${text} => ${results[0].language}`;
37-
assert.equal(output, expected);
38-
});
39-
});
30+
test(`should detect language of a single string`, async (t) => {
31+
const output = await runAsync(`${cmd} detect "${text}"`, cwd);
32+
const [detection] = await translate.detect(text);
33+
const expected = `Detections:\n${text} => ${detection.language}`;
34+
t.is(output, expected);
35+
});
4036

41-
it(`should detect language of multiple strings`, () => {
42-
const output = run(`${cmd} detect "${text}" "${text2}"`, cwd);
43-
return translate.detect([text, text2])
44-
.then((results) => {
45-
const expected = `Detections:\n${text} => ${results[0][0].language}\n${text2} => ${results[0][1].language}`;
46-
assert.equal(output, expected);
47-
});
48-
});
37+
test(`should detect language of multiple strings`, async (t) => {
38+
const output = await runAsync(`${cmd} detect "${text}" "${text2}"`, cwd);
39+
const [detections] = await translate.detect([text, text2]);
40+
const expected = `Detections:\n${text} => ${detections[0].language}\n${text2} => ${detections[1].language}`;
41+
t.is(output, expected);
42+
});
4943

50-
it(`should list languages`, () => {
51-
const output = run(`${cmd} list`, cwd);
52-
assert.equal(output.includes(`Languages:`), true);
53-
assert.equal(output.includes(`{ code: 'af', name: 'Afrikaans' }`), true);
54-
});
44+
test(`should list languages`, async (t) => {
45+
const output = await runAsync(`${cmd} list`, cwd);
46+
t.true(output.includes(`Languages:`));
47+
t.true(output.includes(`{ code: 'af', name: 'Afrikaans' }`));
48+
});
5549

56-
it(`should list languages with a target`, () => {
57-
const output = run(`${cmd} list es`, cwd);
58-
assert.equal(output.includes(`Languages:`), true);
59-
assert.equal(output.includes(`{ code: 'af', name: 'afrikáans' }`), true);
60-
});
50+
test(`should list languages with a target`, async (t) => {
51+
const output = await runAsync(`${cmd} list es`, cwd);
52+
t.true(output.includes(`Languages:`));
53+
t.true(output.includes(`{ code: 'af', name: 'afrikáans' }`));
54+
});
6155

62-
it(`should translate a single string`, () => {
63-
const output = run(`${cmd} translate ${toLang} "${text}"`, cwd);
64-
return translate.translate(text, toLang)
65-
.then((results) => {
66-
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
67-
assert.equal(output, expected);
68-
});
69-
});
56+
test(`should translate a single string`, async (t) => {
57+
const output = await runAsync(`${cmd} translate ${toLang} "${text}"`, cwd);
58+
const [translation] = await translate.translate(text, toLang);
59+
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
60+
t.is(output, expected);
61+
});
7062

71-
it(`should translate multiple strings`, () => {
72-
const output = run(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
73-
return translate.translate([text, text2], toLang)
74-
.then((results) => {
75-
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
76-
assert.equal(output, expected);
77-
});
78-
});
63+
test(`should translate multiple strings`, async (t) => {
64+
const output = await runAsync(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd);
65+
const [translations] = await translate.translate([text, text2], toLang);
66+
const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`;
67+
t.is(output, expected);
68+
});
7969

80-
it(`should translate a single string with a model`, () => {
81-
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
82-
return translate.translate(text, toLang)
83-
.then((results) => {
84-
const expected = `Translations:\n${text} => (${toLang}) ${results[0]}`;
85-
assert.equal(output, expected);
86-
});
87-
});
70+
test(`should translate a single string with a model`, async (t) => {
71+
const output = await runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd);
72+
const [translation] = await translate.translate(text, toLang);
73+
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
74+
t.is(output, expected);
75+
});
8876

89-
it(`should translate multiple strings with a model`, () => {
90-
const output = run(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
91-
return translate.translate([text, text2], toLang)
92-
.then((results) => {
93-
const expected = `Translations:\n${text} => (${toLang}) ${results[0][0]}\n${text2} => (${toLang}) ${results[0][1]}`;
94-
assert.equal(output, expected);
95-
});
96-
});
77+
test(`should translate multiple strings with a model`, async (t) => {
78+
const output = await runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd);
79+
const [translations] = await translate.translate([text, text2], toLang);
80+
const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`;
81+
t.is(output, expected);
9782
});

0 commit comments

Comments
 (0)