Skip to content

Commit c637d3b

Browse files
authored
Switch from Mocha to Ava for faster tests (#289)
* Switch from Mocha to Ava * Concurrency: 5
1 parent 65d136d commit c637d3b

12 files changed

+121
-241
lines changed

vision/samples/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"license": "Apache Version 2.0",
66
"author": "Google Inc.",
77
"scripts": {
8-
"test": "cd ..; npm run t -- vision/test/*.test.js",
9-
"system-test": "cd ..; npm run st -- vision/system-test/*.test.js"
8+
"test": "cd ..; npm run st -- vision/system-test/*.test.js"
109
},
1110
"dependencies": {
1211
"@google-cloud/vision": "0.7.0",

vision/samples/quickstart.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ const visionClient = Vision({
3131
const fileName = './resources/wakeupcat.jpg';
3232

3333
// Performs label detection on the image file
34-
visionClient.detectLabels(fileName, (err, labels) => {
35-
if (err) {
36-
console.error(err);
37-
return;
38-
}
34+
visionClient.detectLabels(fileName)
35+
.then((results) => {
36+
const labels = results[0];
3937

40-
console.log('Labels:');
41-
labels.forEach((label) => console.log(label));
42-
});
38+
console.log('Labels:');
39+
labels.forEach((label) => console.log(label));
40+
});
4341
// [END vision_quickstart]

vision/samples/system-test/faceDetection.test.js

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,54 @@
1515

1616
'use strict';
1717

18-
var fs = require('fs');
19-
var path = require('path');
18+
require(`../../system-test/_setup`);
2019

21-
function MockCanvas () {
22-
this.getContext = function () {
20+
const fs = require(`fs`);
21+
const path = require(`path`);
22+
23+
class MockCanvas {
24+
getContext () {
2325
return {
24-
drawImage: function () {},
25-
beginPath: function () {},
26-
lineTo: function () {},
27-
stroke: function () {}
26+
drawImage: () => {},
27+
beginPath: () => {},
28+
lineTo: () => {},
29+
stroke: () => {}
2830
};
29-
};
30-
this.pngStream = function () {
31+
}
32+
33+
pngStream () {
3134
return {
32-
on: function (event, cb) {
35+
on: (event, cb) => {
3336
if (event === 'end') {
34-
setTimeout(function () {
35-
cb();
36-
}, 1000);
37-
} else if (event === 'data') {
38-
cb('test');
39-
cb('foo');
40-
cb('bar');
37+
setTimeout(cb, 1000);
38+
} else if (event === `data`) {
39+
cb(`test`);
40+
cb(`foo`);
41+
cb(`bar`);
4142
}
4243
}
4344
};
44-
};
45+
}
4546
}
4647

47-
MockCanvas.Image = function () {};
48+
MockCanvas.Image = class Image {};
4849

49-
var faceDetectionExample = require('../faceDetection');
50-
var inputFile = path.join(__dirname, '../resources', 'face.png');
51-
var outputFile = path.join(__dirname, '../../vision', 'out.png');
50+
const faceDetectionExample = require(`../faceDetection`);
51+
const inputFile = path.join(__dirname, `../resources`, `face.png`);
52+
const outputFile = path.join(__dirname, `../../vision`, `out.png`);
5253

53-
describe('vision:faceDetection', function () {
54-
it('should detect faces', function (done) {
55-
faceDetectionExample.main(
56-
inputFile,
57-
outputFile,
58-
MockCanvas,
59-
function (err, faces) {
60-
assert.ifError(err);
61-
assert(faces.length === 1);
62-
var image = fs.readFileSync(outputFile);
63-
assert(image.toString('utf8') === 'testfoobar');
64-
assert(console.log.calledWith('Found 1 face'));
65-
assert(console.log.calledWith('Highlighting...'));
66-
assert(console.log.calledWith('Finished!'));
67-
done();
68-
}
69-
);
54+
test.before(stubConsole);
55+
test.after(restoreConsole);
56+
57+
test.cb(`should detect faces`, (t) => {
58+
faceDetectionExample.main(inputFile, outputFile, MockCanvas, (err, faces) => {
59+
t.ifError(err);
60+
t.is(faces.length, 1);
61+
const image = fs.readFileSync(outputFile);
62+
t.is(image.toString(`utf8`), `testfoobar`);
63+
t.true(console.log.calledWith(`Found 1 face`));
64+
t.true(console.log.calledWith(`Highlighting...`));
65+
t.true(console.log.calledWith(`Finished!`));
66+
t.end();
7067
});
7168
});

vision/samples/system-test/labelDetection.test.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515

1616
'use strict';
1717

18-
var path = require('path');
19-
var labelDetectionSample = require('../labelDetection');
20-
var inputFile = path.join(__dirname, '../resources', 'cat.jpg');
18+
require(`../../system-test/_setup`);
2119

22-
describe('vision:labelDetection', function () {
23-
it('should detect labels', function (done) {
24-
labelDetectionSample.main(
25-
inputFile,
26-
function (err, labels) {
27-
assert.ifError(err);
28-
assert(labels.length > 0);
29-
assert(console.log.calledWith('Found label: cat for ' + inputFile));
30-
done();
31-
}
32-
);
20+
const path = require(`path`);
21+
22+
const labelDetectionSample = require(`../labelDetection`);
23+
const inputFile = path.join(__dirname, `../resources`, `cat.jpg`);
24+
25+
test.before(stubConsole);
26+
test.after(restoreConsole);
27+
28+
test.cb(`should detect labels`, (t) => {
29+
labelDetectionSample.main(inputFile, (err, labels) => {
30+
t.ifError(err);
31+
t.true(labels.length > 0);
32+
t.true(console.log.calledWith(`Found label: cat for ${inputFile}`));
33+
t.end();
3334
});
3435
});

vision/samples/system-test/landmarkDetection.test.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515

1616
'use strict';
1717

18-
var landmarkDetectionSample = require('../landmarkDetection');
19-
var inputFile = 'https://cloud-samples-tests.storage.googleapis.com/vision/water.jpg';
18+
require(`../../system-test/_setup`);
2019

21-
describe('vision:landmarkDetection', function () {
22-
it('should detect landmarks', function (done) {
23-
landmarkDetectionSample.main(inputFile, function (err, landmarks) {
24-
assert.ifError(err);
25-
assert(landmarks.length > 0);
26-
assert(console.log.calledWith('Found landmark: Taitung, Famous Places "up the water flow" marker for ' + inputFile));
27-
done();
28-
});
20+
const landmarkDetectionSample = require(`../landmarkDetection`);
21+
const inputFile = `https://cloud-samples-tests.storage.googleapis.com/vision/water.jpg`;
22+
23+
test.before(stubConsole);
24+
test.after(restoreConsole);
25+
26+
test.cb(`should detect landmarks`, (t) => {
27+
landmarkDetectionSample.main(inputFile, (err, landmarks) => {
28+
t.ifError(err);
29+
t.true(landmarks.length > 0);
30+
t.true(console.log.calledWith(`Found landmark: Taitung, Famous Places "up the water flow" marker for ${inputFile}`));
31+
t.end();
2932
});
3033
});

vision/samples/system-test/quickstart.test.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,45 @@
1515

1616
'use strict';
1717

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

22-
describe(`vision:quickstart`, () => {
23-
let visionMock, VisionMock;
24-
25-
it(`should detect labels`, (done) => {
26-
const filePath = path.join(__dirname, `../resources/wakeupcat.jpg`);
27-
const expectedFileName = `./resources/wakeupcat.jpg`;
28-
29-
visionMock = {
30-
detectLabels: (_fileName, _callback) => {
31-
assert.equal(_fileName, expectedFileName);
32-
assert.equal(typeof _callback, 'function');
33-
34-
vision.detectLabels(filePath, (err, labels, apiResponse) => {
35-
_callback(err, labels, apiResponse);
36-
assert.ifError(err);
37-
assert.equal(Array.isArray(labels), true);
38-
assert.notEqual(apiResponse, undefined);
39-
assert.equal(console.log.called, true);
40-
assert.deepEqual(console.log.firstCall.args, [`Labels:`]);
41-
labels.forEach((label, i) => {
42-
assert.deepEqual(console.log.getCall(i + 1).args, [label]);
43-
});
44-
done();
24+
test.before(stubConsole);
25+
test.after(restoreConsole);
26+
27+
test.cb(`should detect labels`, (t) => {
28+
const filePath = path.join(__dirname, `../resources/wakeupcat.jpg`);
29+
const expectedFileName = `./resources/wakeupcat.jpg`;
30+
const visionMock = {
31+
detectLabels: (_fileName) => {
32+
t.is(_fileName, expectedFileName);
33+
34+
return vision.detectLabels(filePath)
35+
.then(([labels]) => {
36+
t.true(Array.isArray(labels));
37+
38+
setTimeout(() => {
39+
try {
40+
t.is(console.log.callCount, 6);
41+
t.deepEqual(console.log.getCall(0).args, [`Labels:`]);
42+
labels.forEach((label, i) => {
43+
t.deepEqual(console.log.getCall(i + 1).args, [label]);
44+
});
45+
t.end();
46+
} catch (err) {
47+
t.end(err);
48+
}
49+
}, 200);
50+
51+
return [labels];
4552
});
46-
}
47-
};
48-
VisionMock = sinon.stub().returns(visionMock);
53+
}
54+
};
4955

50-
proxyquire(`../quickstart`, {
51-
'@google-cloud/vision': VisionMock
52-
});
56+
proxyquire(`../quickstart`, {
57+
'@google-cloud/vision': sinon.stub().returns(visionMock)
5358
});
5459
});

vision/samples/system-test/textDetection.test.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@
1515

1616
'use strict';
1717

18-
var path = require('path');
19-
var inputDir = path.join(__dirname, '../resources');
20-
var textDetectionSample = require('../textDetection');
18+
require(`../../system-test/_setup`);
2119

22-
describe('vision:textDetection', function () {
23-
it('should detect texts', function (done) {
24-
textDetectionSample.main(inputDir, function (err, textResponse) {
25-
assert.ifError(err);
26-
assert(Object.keys(textResponse).length > 0);
27-
textDetectionSample.lookup(['the', 'sunbeams', 'in'], function (err, hits) {
28-
assert.ifError(err);
29-
assert(hits.length > 0);
30-
assert(hits[0].length > 0);
31-
done();
32-
});
20+
const path = require(`path`);
21+
22+
const inputDir = path.join(__dirname, `../resources`);
23+
const textDetectionSample = require(`../textDetection`);
24+
25+
test.cb(`should detect texts`, (t) => {
26+
textDetectionSample.main(inputDir, (err, textResponse) => {
27+
t.ifError(err);
28+
t.true(Object.keys(textResponse).length > 0);
29+
textDetectionSample.lookup(['the', 'sunbeams', 'in'], (err, hits) => {
30+
t.ifError(err);
31+
t.true(hits.length > 0);
32+
t.true(hits[0].length > 0);
33+
t.end();
3334
});
3435
});
3536
});

vision/samples/test/faceDetection.test.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

vision/samples/test/labelDetection.test.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

vision/samples/test/landmarkDetection.test.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)