Skip to content

Commit f0e4ec5

Browse files
nareshqlogicAce Nassri
authored and
Ace Nassri
committed
refactor(samples): convert sample tests from ava to mocha (#171)
1 parent 42b34c4 commit f0e4ec5

File tree

4 files changed

+57
-61
lines changed

4 files changed

+57
-61
lines changed

translate/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"node": ">=8"
1010
},
1111
"scripts": {
12-
"ava": "ava -T 20s --verbose test/*.test.js ./system-test/*.test.js",
13-
"cover": "nyc --reporter=lcov --cache ava -T 20s --verbose test/*.test.js ./system-test/*.test.js && nyc report",
12+
"cover": "nyc --reporter=lcov --cache mocha ./system-test/*.test.js --timeout=60000 && nyc report",
1413
"test": "npm run cover"
1514
},
1615
"dependencies": {

translate/system-test/.eslintrc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
env:
3+
mocha: true
24
rules:
35
node/no-unpublished-require: off
46
node/no-unsupported-features: off

translate/system-test/quickstart.test.js

+25-30
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,45 @@
1515

1616
'use strict';
1717

18-
const proxyquire = require(`proxyquire`).noPreserveCache();
19-
const sinon = require(`sinon`);
20-
const test = require(`ava`);
21-
const tools = require(`@google-cloud/nodejs-repo-tools`);
22-
const {Translate} = proxyquire(`@google-cloud/translate`, {});
18+
const proxyquire = require('proxyquire').noPreserveCache();
19+
const sinon = require('sinon');
20+
const assert = require('assert');
21+
const tools = require('@google-cloud/nodejs-repo-tools');
22+
const {Translate} = proxyquire('@google-cloud/translate', {});
2323
const translate = new Translate();
2424

25-
test.before(tools.checkCredentials);
26-
test.before(tools.stubConsole);
27-
test.after.always(tools.restoreConsole);
25+
before(tools.checkCredentials);
26+
before(tools.stubConsole);
27+
after(tools.restoreConsole);
2828

29-
test.cb(`should translate a string`, t => {
30-
const string = `Hello, world!`;
31-
const expectedTranslation = `Привет, мир!`;
32-
const targetLanguage = `ru`;
29+
it('should translate a string', () => {
30+
const string = 'Hello, world!';
31+
const expectedTranslation = 'Привет, мир!';
32+
const targetLanguage = 'ru';
3333
const translateMock = {
3434
translate: (_string, _targetLanguage) => {
35-
t.is(_string, string);
36-
t.is(_targetLanguage, targetLanguage);
35+
assert.strictEqual(_string, string);
36+
assert.strictEqual(_targetLanguage, targetLanguage);
3737

3838
return translate
3939
.translate(_string, _targetLanguage)
40-
.then(([translation]) => {
41-
t.is(translation, expectedTranslation);
42-
43-
setTimeout(() => {
44-
try {
45-
t.is(console.log.callCount, 2);
46-
t.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]);
47-
t.deepEqual(console.log.getCall(1).args, [
48-
`Translation: ${expectedTranslation}`,
49-
]);
50-
t.end();
51-
} catch (err) {
52-
t.end(err);
53-
}
54-
}, 200);
40+
.then(async ([translation]) => {
41+
assert.strictEqual(translation, expectedTranslation);
42+
await new Promise(r => setTimeout(r, 200));
43+
assert.strictEqual(console.log.callCount, 2);
44+
assert.deepStrictEqual(console.log.getCall(0).args, [
45+
`Text: ${string}`,
46+
]);
47+
assert.deepStrictEqual(console.log.getCall(1).args, [
48+
`Translation: ${expectedTranslation}`,
49+
]);
5550

5651
return [translation];
5752
});
5853
},
5954
};
6055

61-
proxyquire(`../quickstart`, {
56+
proxyquire('../quickstart', {
6257
'@google-cloud/translate': {
6358
Translate: sinon.stub().returns(translateMock),
6459
},

translate/system-test/translate.test.js

+29-29
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@
1515

1616
'use strict';
1717

18-
const path = require(`path`);
19-
const test = require(`ava`);
20-
const tools = require(`@google-cloud/nodejs-repo-tools`);
21-
const {Translate} = require(`@google-cloud/translate`);
18+
const path = require('path');
19+
const assert = require('assert');
20+
const tools = require('@google-cloud/nodejs-repo-tools');
21+
const {Translate} = require('@google-cloud/translate');
2222
const translate = new Translate();
2323

24-
const cwd = path.join(__dirname, `..`);
25-
const cmd = `node translate.js`;
26-
const text = `Hello world!`;
27-
const text2 = `Goodbye!`;
28-
const model = `nmt`;
29-
const toLang = `ru`;
24+
const cwd = path.join(__dirname, '..');
25+
const cmd = 'node translate.js';
26+
const text = 'Hello world!';
27+
const text2 = 'Goodbye!';
28+
const model = 'nmt';
29+
const toLang = 'ru';
3030

31-
test.before(tools.checkCredentials);
31+
before(tools.checkCredentials);
3232

33-
test(`should detect language of a single string`, async t => {
33+
it('should detect language of a single string', async () => {
3434
const output = await tools.runAsync(`${cmd} detect "${text}"`, cwd);
3535
const [detection] = await translate.detect(text);
3636
const expected = `Detections:\n${text} => ${detection.language}`;
37-
t.is(output, expected);
37+
assert.strictEqual(output, expected);
3838
});
3939

40-
test(`should detect language of multiple strings`, async t => {
40+
it('should detect language of multiple strings', async () => {
4141
const output = await tools.runAsync(
4242
`${cmd} detect "${text}" "${text2}"`,
4343
cwd
@@ -46,32 +46,32 @@ test(`should detect language of multiple strings`, async t => {
4646
const expected = `Detections:\n${text} => ${
4747
detections[0].language
4848
}\n${text2} => ${detections[1].language}`;
49-
t.is(output, expected);
49+
assert.strictEqual(output, expected);
5050
});
5151

52-
test(`should list languages`, async t => {
52+
it('should list languages', async () => {
5353
const output = await tools.runAsync(`${cmd} list`, cwd);
54-
t.true(output.includes(`Languages:`));
55-
t.true(output.includes(`{ code: 'af', name: 'Afrikaans' }`));
54+
assert.ok(output.includes('Languages:'));
55+
assert.ok(output.includes(`{ code: 'af', name: 'Afrikaans' }`));
5656
});
5757

58-
test(`should list languages with a target`, async t => {
58+
it('should list languages with a target', async () => {
5959
const output = await tools.runAsync(`${cmd} list es`, cwd);
60-
t.true(output.includes(`Languages:`));
61-
t.true(output.includes(`{ code: 'af', name: 'afrikáans' }`));
60+
assert.ok(output.includes('Languages:'));
61+
assert.ok(output.includes(`{ code: 'af', name: 'afrikáans' }`));
6262
});
6363

64-
test(`should translate a single string`, async t => {
64+
it('should translate a single string', async () => {
6565
const output = await tools.runAsync(
6666
`${cmd} translate ${toLang} "${text}"`,
6767
cwd
6868
);
6969
const [translation] = await translate.translate(text, toLang);
7070
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
71-
t.is(output, expected);
71+
assert.strictEqual(output, expected);
7272
});
7373

74-
test(`should translate multiple strings`, async t => {
74+
it('should translate multiple strings', async () => {
7575
const output = await tools.runAsync(
7676
`${cmd} translate ${toLang} "${text}" "${text2}"`,
7777
cwd
@@ -80,20 +80,20 @@ test(`should translate multiple strings`, async t => {
8080
const expected = `Translations:\n${text} => (${toLang}) ${
8181
translations[0]
8282
}\n${text2} => (${toLang}) ${translations[1]}`;
83-
t.is(output, expected);
83+
assert.strictEqual(output, expected);
8484
});
8585

86-
test(`should translate a single string with a model`, async t => {
86+
it('should translate a single string with a model', async () => {
8787
const output = await tools.runAsync(
8888
`${cmd} translate-with-model ${toLang} ${model} "${text}"`,
8989
cwd
9090
);
9191
const [translation] = await translate.translate(text, toLang);
9292
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
93-
t.is(output, expected);
93+
assert.strictEqual(output, expected);
9494
});
9595

96-
test(`should translate multiple strings with a model`, async t => {
96+
it('should translate multiple strings with a model', async () => {
9797
const output = await tools.runAsync(
9898
`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`,
9999
cwd
@@ -102,5 +102,5 @@ test(`should translate multiple strings with a model`, async t => {
102102
const expected = `Translations:\n${text} => (${toLang}) ${
103103
translations[0]
104104
}\n${text2} => (${toLang}) ${translations[1]}`;
105-
t.is(output, expected);
105+
assert.strictEqual(output, expected);
106106
});

0 commit comments

Comments
 (0)