Skip to content

Commit

Permalink
refactor(samples): convert sample tests from ava to mocha (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
nareshqlogic authored and Ace Nassri committed Nov 17, 2022
1 parent c52492c commit 1402063
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 61 deletions.
3 changes: 1 addition & 2 deletions translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"node": ">=8"
},
"scripts": {
"ava": "ava -T 20s --verbose test/*.test.js ./system-test/*.test.js",
"cover": "nyc --reporter=lcov --cache ava -T 20s --verbose test/*.test.js ./system-test/*.test.js && nyc report",
"cover": "nyc --reporter=lcov --cache mocha ./system-test/*.test.js --timeout=60000 && nyc report",
"test": "npm run cover"
},
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions translate/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
env:
mocha: true
rules:
node/no-unpublished-require: off
node/no-unsupported-features: off
Expand Down
55 changes: 25 additions & 30 deletions translate/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,45 @@

'use strict';

const proxyquire = require(`proxyquire`).noPreserveCache();
const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const {Translate} = proxyquire(`@google-cloud/translate`, {});
const proxyquire = require('proxyquire').noPreserveCache();
const sinon = require('sinon');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');
const {Translate} = proxyquire('@google-cloud/translate', {});
const translate = new Translate();

test.before(tools.checkCredentials);
test.before(tools.stubConsole);
test.after.always(tools.restoreConsole);
before(tools.checkCredentials);
before(tools.stubConsole);
after(tools.restoreConsole);

test.cb(`should translate a string`, t => {
const string = `Hello, world!`;
const expectedTranslation = `Привет, мир!`;
const targetLanguage = `ru`;
it('should translate a string', () => {
const string = 'Hello, world!';
const expectedTranslation = 'Привет, мир!';
const targetLanguage = 'ru';
const translateMock = {
translate: (_string, _targetLanguage) => {
t.is(_string, string);
t.is(_targetLanguage, targetLanguage);
assert.strictEqual(_string, string);
assert.strictEqual(_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);
.then(async ([translation]) => {
assert.strictEqual(translation, expectedTranslation);
await new Promise(r => setTimeout(r, 200));
assert.strictEqual(console.log.callCount, 2);
assert.deepStrictEqual(console.log.getCall(0).args, [
`Text: ${string}`,
]);
assert.deepStrictEqual(console.log.getCall(1).args, [
`Translation: ${expectedTranslation}`,
]);

return [translation];
});
},
};

proxyquire(`../quickstart`, {
proxyquire('../quickstart', {
'@google-cloud/translate': {
Translate: sinon.stub().returns(translateMock),
},
Expand Down
58 changes: 29 additions & 29 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@

'use strict';

const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const {Translate} = require(`@google-cloud/translate`);
const path = require('path');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');
const {Translate} = require('@google-cloud/translate');
const translate = new Translate();

const cwd = path.join(__dirname, `..`);
const cmd = `node translate.js`;
const text = `Hello world!`;
const text2 = `Goodbye!`;
const model = `nmt`;
const toLang = `ru`;
const cwd = path.join(__dirname, '..');
const cmd = 'node translate.js';
const text = 'Hello world!';
const text2 = 'Goodbye!';
const model = 'nmt';
const toLang = 'ru';

test.before(tools.checkCredentials);
before(tools.checkCredentials);

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

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

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

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

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

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

test(`should translate a single string with a model`, async t => {
it('should translate a single string with a model', async () => {
const output = await tools.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);
assert.strictEqual(output, expected);
});

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

0 comments on commit 1402063

Please sign in to comment.