Skip to content

Commit

Permalink
refactor: modernize the sample tests (#185)
Browse files Browse the repository at this point in the history
* refactor: modernize the sample tests

* lintalicious
  • Loading branch information
JustinBeckwith authored and sofisl committed Oct 13, 2022
1 parent e37ee9d commit 258d093
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/google-cloud-language/samples/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
rules:
no-console: off
node/no-missing-require: off
5 changes: 3 additions & 2 deletions packages/google-cloud-language/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"resources"
],
"scripts": {
"test": "mocha"
"test": "mocha --timeout 60000"
},
"dependencies": {
"@google-cloud/automl": "^0.1.1",
Expand All @@ -22,7 +22,8 @@
"yargs": "^12.0.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^5.2.0",
"uuid": "^3.2.1"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/google-cloud-language/samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

// [START language_quickstart]
async function main() {
async function quickstart() {
// Imports the Google Cloud client library
const language = require('@google-cloud/language');

Expand All @@ -39,6 +39,6 @@ async function main() {
console.log(`Sentiment score: ${sentiment.score}`);
console.log(`Sentiment magnitude: ${sentiment.magnitude}`);
}

main().catch(console.error);
// [END language_quickstart]

quickstart().catch(console.error);
23 changes: 9 additions & 14 deletions packages/google-cloud-language/samples/test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@

'use strict';

const path = require(`path`);
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const {assert} = require('chai');
const execa = require('execa');

const cmd = `node quickstart.js`;
const cwd = path.join(__dirname, `..`);

beforeEach(async () => tools.stubConsole);
afterEach(async () => tools.restoreConsole);

it(`should analyze sentiment in text`, async () => {
const output = await tools.runAsync(cmd, cwd);
assert(RegExp('Text: Hello, world!').test(output));
assert(RegExp('Sentiment score: ').test(output));
assert(RegExp('Sentiment magnitude: ').test(output));
describe('quickstart', () => {
it('should analyze sentiment in text', async () => {
const {stdout} = await execa.shell('node quickstart.js');
assert(stdout, /Text: Hello, world!/);
assert(stdout, /Sentiment score: /);
assert(stdout, /Sentiment magnitude: /);
});
});

0 comments on commit 258d093

Please sign in to comment.