Skip to content

Commit

Permalink
fix: modernize the sample tests (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Dec 21, 2018
1 parent 832181b commit b1e1050
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 551 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
"scripts": {
"presystem-test": "npm run compile",
"system-test": "mocha build/system-test --timeout 600000",
"cover": "nyc --reporter=lcov mocha build/test && nyc report",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"test-no-cover": "mocha build/test",
"test": "npm run cover",
"test": "nyc mocha build/test",
"lint": "eslint '**/*.js' && gts check",
"docs": "jsdoc -c .jsdoc.js",
"fix": "eslint --fix '**/*.js' && gts fix",
Expand Down
2 changes: 0 additions & 2 deletions samples/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
env:
mocha: true
rules:
no-console: off
node/no-missing-require: off
19 changes: 7 additions & 12 deletions samples/package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
{
"name": "nodejs-docs-samples-pubsub",
"version": "0.0.1",
"files": [
"*.js"
],
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/googleapis/nodejs-pubsub.git"
},
"repository": "googleapis/nodejs-pubsub",
"engines": {
"node": ">=8"
},
"scripts": {
"mocha": "mocha system-test/*.js --timeout 600000",
"cover": "nyc --reporter=lcov --cache mocha system-test/*.test.js --timeout 600000 && nyc report",
"test": "npm run cover"
"test": "mocha system-test --timeout 600000"
},
"dependencies": {
"@google-cloud/pubsub": "^0.22.2",
"yargs": "^12.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^5.2.0",
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"nyc": "^13.0.0",
"proxyquire": "^2.0.0",
"sinon": "^7.0.0",
"uuid": "^3.1.0"
}
}
21 changes: 9 additions & 12 deletions samples/quickstart.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@
// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

async function quickstart() {
// Your Google Cloud Platform project ID
const projectId = process.env.GCLOUD_PROJECT || 'YOUR_PROJECT_ID';

async function quickstart(
projectId = 'your-project-id', // Your Google Cloud Platform project ID
topicName = 'my-topic' // Name for the new topic to create
) {
// Instantiates a client
const pubsubClient = new PubSub({
projectId: projectId,
});

// The name for the new topic
const topicName = 'my-topic';
const pubsub = new PubSub({projectId});

// Creates the new topic
const [topic] = await pubsubClient.createTopic(topicName);
const [topic] = await pubsub.createTopic(topicName);
console.log(`Topic ${topic.name} created.`);
}
quickstart().catch(console.error);
// [END pubsub_quickstart_create_topic]

const args = process.argv.slice(2);
quickstart(...args).catch(console.error);
Empty file modified samples/subscriptions.js
100755 → 100644
Empty file.
5 changes: 0 additions & 5 deletions samples/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
---
env:
mocha: true
rules:
node/no-unpublished-require: off
node/no-unsupported-features: off
no-empty: off
no-unused-vars: error
57 changes: 15 additions & 42 deletions samples/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,24 @@

'use strict';

const proxyquire = require('proxyquire').noPreserveCache();
const {PubSub} = proxyquire('@google-cloud/pubsub', {});
const sinon = require('sinon');
const assert = require('assert');
const tools = require('@google-cloud/nodejs-repo-tools');
const {PubSub} = require('@google-cloud/pubsub');
const {assert} = require('chai');
const execa = require('execa');
const uuid = require('uuid');

const projectId = process.env.GCLOUD_PROJECT;
const pubsub = new PubSub({projectId});
describe('quickstart', () => {
const projectId = process.env.GCLOUD_PROJECT;
const pubsub = new PubSub({projectId});
const topicName = `nodejs-docs-samples-test-${uuid.v4()}`;

const topicName = `nodejs-docs-samples-test-${uuid.v4()}`;
const fullTopicName = `projects/${projectId}/topics/${topicName}`;

before(tools.stubConsole);
after(async () => {
tools.restoreConsole();
return await pubsub
.topic(topicName)
.delete()
.catch(() => {});
});

it(`should create a topic`, async () => {
const expectedTopicName = `my-topic`;
const pubsubMock = {
createTopic: _topicName => {
assert.strictEqual(_topicName, expectedTopicName);
return pubsub.createTopic(topicName).then(([topic]) => {
assert.strictEqual(topic.name, fullTopicName);
setTimeout(() => {
try {
assert.strictEqual(console.log.callCount, 1);
assert.deepStrictEqual(console.log.getCall(0).args, [
`Topic ${topic.name} created.`,
]);
} catch (err) {}
}, 200);
return [topic];
});
},
};
after(async () => {
await pubsub.topic(topicName).delete();
});

proxyquire(`../quickstart`, {
'@google-cloud/pubsub': {
PubSub: sinon.stub().returns(pubsubMock),
},
it('should run the quickstart', async () => {
const {stdout} = await execa.shell(
`node quickstart ${projectId} ${topicName}`
);
assert.match(stdout, /^Topic .* created.$/);
});
});
Loading

0 comments on commit b1e1050

Please sign in to comment.