Skip to content

Commit

Permalink
Merge pull request #184 from ericblade/dev
Browse files Browse the repository at this point in the history
chore: add tests for import/require in node (fix #119)
  • Loading branch information
ericblade authored May 3, 2020
2 parents 51e88f7 + 360ba03 commit ba24349
Show file tree
Hide file tree
Showing 9 changed files with 583 additions and 316 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test GreenKeeper/Pull Requests To Master
name: Test Pull Requests To Master

on:
push:
Expand Down Expand Up @@ -30,3 +30,10 @@ jobs:
npm test
env:
CI: true
- name: test package import
run: |
npm install ts-node @std/esm
npm run test:import
- name: test package require
run: |
npm run test:require
692 changes: 447 additions & 245 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@
"types": "type-definitions/quagga.d.ts",
"browser": "dist/quagga.min.js",
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
"@babel/plugin-proposal-optional-chaining": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@babel/plugin-transform-runtime": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/preset-typescript": "^7.8.3",
"@babel/runtime": "^7.8.4",
"@babel/runtime": "^7.9.6",
"@types/chai": "^4.2.9",
"@types/gl-vec2": "^1.3.0",
"@types/lodash": "^4.14.149",
"@types/lodash": "^4.14.150",
"@types/mocha": "5.2.7",
"@types/react": "^16.9.19",
"@types/sinon": "^7.5.1",
"@types/sinon-chai": "^3.2.3",
"async": "1.5.2",
"babel-loader": "^8.0.6",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-istanbul": "^6.0.0",
"chai": "^4.2.0",
"core-js": "^3.6.4",
"core-js": "^3.6.5",
"cross-env": "^7.0.0",
"eslint": "^6.8.0",
"grunt": "^1.0.4",
Expand All @@ -40,19 +39,19 @@
"karma-chrome-launcher": "2.2.0",
"karma-coverage": "1.1.2",
"karma-firefox-launcher": "0.1.7",
"karma-mocha": "0.2.2",
"karma-mocha": "1.3.0",
"karma-phantomjs-launcher": "0.2.3",
"karma-sinon": "^1.0.5",
"karma-sinon-chai": "^2.0.2",
"karma-source-map-support": "^1.4.0",
"karma-webpack": "^4.0.2",
"mocha": "2.5.3",
"mocha": "5.2.0",
"phantomjs": "^2.1.7",
"sinon": "5.1.1",
"sinon-chai": "^3.5.0",
"source-map-loader": "^0.2.4",
"typescript": "^3.8.2",
"webpack": "^4.41.6",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-sources": "^1.4.3"
},
Expand All @@ -62,6 +61,8 @@
"scripts": {
"check-types": "tsc",
"test": "npx grunt test",
"test:require": "npx mocha test/test-require",
"test:import": "npx mocha -r @std/esm -r ts-node/register test/test-import.mjs",
"integrationtest": "grunt integrationtest",
"build:dev": "npx cross-env BUILD_ENV=development webpack --config configs/webpack.config.js",
"build:prod": "npx cross-env BUILD_ENV=production webpack --config configs/webpack.config.min.js",
Expand Down Expand Up @@ -123,7 +124,7 @@
],
"license": "MIT",
"engines": {
"node": ">= 8.0"
"node": ">= 10.0"
},
"dependencies": {
"@babel/polyfill": "^7.8.7",
Expand All @@ -134,7 +135,7 @@
"lodash": "^4.17.15",
"ndarray": "^1.0.19",
"ndarray-linear-interpolate": "^1.0.0",
"snyk": "~1.315.0"
"snyk": "^1.315.1"
},
"snyk": true,
"greenkeeper": {
Expand Down
4 changes: 1 addition & 3 deletions src/common/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ export default (function() {
});
},
unsubscribe: function(eventName: EventName, callback?: Function | Subscription) {
var event;

if (eventName) {
event = getEvent(eventName);
const event = getEvent(eventName);
if (event && callback) {
event.subscribers = event.subscribers.filter(function(subscriber){
return subscriber.callback !== callback;
Expand Down
25 changes: 23 additions & 2 deletions src/quagga.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const instance = new Quagga();
const _context = instance.context;

const QuaggaJSStaticInterface = {
// TODO #185: Quagga.init should return a promise if there's no callback
init: function (config, cb, imageWrapper, quaggaInstance = instance) {
quaggaInstance.context.config = merge({}, Config, config);
// TODO: pending restructure in Issue #105, we are temp disabling workers
// TODO #179: pending restructure in Issue #179, we are temp disabling workers
if (quaggaInstance.context.config.numOfWorkers > 0) {
quaggaInstance.context.config.numOfWorkers = 0;
}
Expand All @@ -41,21 +42,41 @@ const QuaggaJSStaticInterface = {
_context.stopped = true;
},
onDetected: function (callback) {
if (!callback || (typeof callback !== 'function' && (typeof callback !== 'object' || !callback.callback))) {
console.trace('* warning: Quagga.onDetected called with invalid callback, ignoring');
return;
}
Events.subscribe('detected', callback);
},
offDetected: function (callback) {
Events.unsubscribe('detected', callback);
},
onProcessed: function (callback) {
if (!callback || (typeof callback !== 'function' && (typeof callback !== 'object' || !callback.callback))) {
console.trace('* warning: Quagga.onProcessed called with invalid callback, ignoring');
return;
}
Events.subscribe('processed', callback);
},
offProcessed: function (callback) {
Events.unsubscribe('processed', callback);
},
setReaders: function (readers) {
if (!readers) {
console.trace('* warning: Quagga.setReaders called with no readers, ignoring');
return;
}
instance.setReaders(readers);
},
registerReader: function (name, reader) {
if (!name) {
console.trace('* warning: Quagga.registerReader called with no name, ignoring');
return;
}
if (!reader) {
console.trace('* warning: Quagga.registerReader called with no reader, ignoring');
return;
}
instance.registerReader(name, reader);
},
registerResultCollector: function (resultCollector) {
Expand All @@ -80,7 +101,7 @@ const QuaggaJSStaticInterface = {
halfSample: false,
},
}, config);
// TODO: restructure worker support so that it will work with typescript using worker-loader
// TODO #175: restructure worker support so that it will work with typescript using worker-loader
// https://webpack.js.org/loaders/worker-loader/
if (config.numOfWorkers > 0) {
config.numOfWorkers = 0;
Expand Down
72 changes: 40 additions & 32 deletions test/integration/integration.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Quagga = require('../../src/quagga').default;
const async = require('async');
// const async = require('async');
const Code128Reader = require('../../src/reader/code_128_reader');

Quagga.registerReader('external_code_128_reader', Code128Reader.default);
Expand Down Expand Up @@ -47,36 +47,44 @@ describe('decodeSingle', function () {

folder = baseFolder + format.split('_').slice(0, -1).concat(suffix ? [suffix] : []).join('_') + '/';

it('should decode ' + folder + ' correctly', function(done) {
async.eachSeries(testSet, function (sample, callback) {
config.src = folder + sample.name;
config.readers = readers;
Quagga.decodeSingle(config, function(result) {
function testFactory(sample) {
return () => {
return new Promise((resolve) => {
const thisConfig = {
...config,
src: folder + sample.name,
readers,
};
console.log('Decoding', sample.name);
console.warn(`* Expect result ${JSON.stringify(result)} to be an object`);
expect(result).to.be.an('Object');
console.warn('* Expect codeResult to be an object');
expect(result.codeResult).to.be.an('Object');
console.warn(`* Expect ${result.codeResult.code} to equal ${sample.result}`);
expect(result.codeResult.code).to.equal(sample.result);
console.warn(`* Expect ${result.codeResult.format} to equal ${sample.format}`);
expect(result.codeResult.format).to.equal(sample.format);
console.warn(`* Expect Quagga.canvas to be an object ${Quagga.canvas}`);
expect(Quagga.canvas).to.be.an('Object');
console.warn(`* Expect Quagga.canvas.dom to be an object ${Quagga.canvas.dom}`);
expect(Quagga.canvas.dom).to.be.an('Object');
console.warn(`* Expect Quagga.canvas.ctx to be an object ${Quagga.canvas.ctx}`);
expect(Quagga.canvas.ctx).to.be.an('Object');
// In prior versions, calling decodeSingle was enough to setup the canvas
// variables, that is no longer the case now that decodeSingle() works on
// multiple instances.
// console.warn(`* Expect Quagga.canvas.ctx.overlay to be a CanvasRenderingContext2D ${Quagga.canvas.ctx.overlay}`);
// expect(Quagga.canvas.ctx.overlay).to.be.an('CanvasRenderingContext2D');
callback();
Quagga.decodeSingle(thisConfig, (result) => {
// console.warn(`* Expect result ${JSON.stringify(result)} to be an object`);
expect(result).to.be.an('Object');
// console.warn('* Expect codeResult to be an object');
expect(result.codeResult).to.be.an('Object');
// console.warn(`* Expect ${result.codeResult.code} to equal ${sample.result}`);
expect(result.codeResult.code).to.equal(sample.result);
// console.warn(`* Expect ${result.codeResult.format} to equal ${sample.format}`);
expect(result.codeResult.format).to.equal(sample.format);
// console.warn(`* Expect Quagga.canvas to be an object ${Quagga.canvas}`);
expect(Quagga.canvas).to.be.an('Object');
// console.warn(`* Expect Quagga.canvas.dom to be an object ${Quagga.canvas.dom}`);
expect(Quagga.canvas.dom).to.be.an('Object');
// console.warn(`* Expect Quagga.canvas.ctx to be an object ${Quagga.canvas.ctx}`);
expect(Quagga.canvas.ctx).to.be.an('Object');
// In prior versions, calling decodeSingle was enough to setup the canvas
// variables, that is no longer the case now that decodeSingle() works on
// multiple instances.
// console.warn(`* Expect Quagga.canvas.ctx.overlay to be a CanvasRenderingContext2D ${Quagga.canvas.ctx.overlay}`);
// expect(Quagga.canvas.ctx.overlay).to.be.an('CanvasRenderingContext2D');
resolve(result);
});
});
}, function() {
done();
});
};
}

it('should decode ' + folder + ' correctly', function() {
const promises = testSet.map(sample => testFactory(sample));
return promises.reduce((promise, func) => promise.then(result => func().then(Array.prototype.concat.bind(result))), Promise.resolve([]));
});
}

Expand Down Expand Up @@ -108,11 +116,11 @@ describe('decodeSingle', function () {
const results = await Promise.all(promises).catch((err) => { console.warn('*** Error in test', err); throw(err); });
const testResults = testSet.map(x => x.result);
results.forEach((r, index) => {
console.warn(`* expect r to be an object ${r}`);
// console.warn(`* expect r to be an object ${r}`);
expect(r).to.be.an('object');
console.warn(`* expect r.codeResult to be an object ${r.codeResult}`);
// console.warn(`* expect r.codeResult to be an object ${r.codeResult}`);
expect(r.codeResult).to.be.an('object');
console.warn(`* expect r.codeResult.code to equal ${r.codeResult.code} ${testResults[index]}`);
// console.warn(`* expect r.codeResult.code to equal ${r.codeResult.code} ${testResults[index]}`);
expect(r.codeResult.code).to.equal(testResults[index]);
});
});
Expand Down
34 changes: 14 additions & 20 deletions test/spec/camera_access.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ describe('camera_access', () => {
});

describe('not available', function(){
var originalGetUserMedia;

it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
.catch((err) => {
Expand All @@ -141,30 +139,26 @@ describe('camera_access', () => {
});

describe('pickConstraints', () => {
it('should return the given constraints if no facingMode is defined', (done) => {
it('should return the given constraints if no facingMode is defined', async () => {
const givenConstraints = {width: 180};
return pickConstraints(givenConstraints).then((actualConstraints) => {
try {
const actualConstraints = await pickConstraints(givenConstraints);
expect(actualConstraints.video).to.deep.equal(givenConstraints);
done();
})
.catch((err) => {
expect(err).to.equal(null);
console.log(err);
done();
});
} catch (err_1) {
expect(err_1).to.equal(null);
console.log(err_1);
}
});

it('should return the given constraints if deviceId is defined', (done) => {
it('should return the given constraints if deviceId is defined', async () => {
const givenConstraints = {width: 180, deviceId: '4343'};
return pickConstraints(givenConstraints).then((actualConstraints) => {
try {
const actualConstraints = await pickConstraints(givenConstraints);
expect(actualConstraints.video).to.deep.equal(givenConstraints);
done();
})
.catch((err) => {
expect(err).to.equal(null);
console.log(err);
done();
});
} catch (err_1) {
expect(err_1).to.equal(null);
console.log(err_1);
}
});
});
});
Expand Down
16 changes: 16 additions & 0 deletions test/test-import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Mocha from 'mocha';
const { describe, it } = Mocha.default;
import * as Chai from 'chai';
const { expect } = Chai.default;
import Q from '../lib/quagga.js';

describe('testing node import', () => {
it('import works', () => {
expect(Q).to.be.an('object');
expect(Q.init).to.be.a('function');
expect(Q.start).to.be.a('function');
expect(Q.stop).to.be.a('function');
});
});

// export default 1;
20 changes: 20 additions & 0 deletions test/test-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { describe, it } = require('mocha');
const { expect } = require('chai');

const Q = require('..');
const Q2 = require('..').default;

describe('testing node require', () => {
it('require works', () => {
expect(Q).to.be.an('object');
expect(Q.init).to.be.a('function');
expect(Q.start).to.be.a('function');
expect(Q.stop).to.be.a('function');
});
it('require default works', () => {
expect(Q2).to.be.an('object');
expect(Q.init).to.be.a('function');
expect(Q.start).to.be.a('function');
expect(Q.stop).to.be.a('function');
});
});

0 comments on commit ba24349

Please sign in to comment.