Skip to content

Commit 570a952

Browse files
committed
WIP: add test harness
1 parent 776ae3d commit 570a952

File tree

10 files changed

+3149
-127
lines changed

10 files changed

+3149
-127
lines changed

gulp/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
'tests/**/*.test.ts',
3131
'!tests/**/browser/**/*.test.ts',
3232
'!tests/**/binary/**/*.test.ts',
33+
'!src/firebase-*.ts',
3334
],
3435
binary: [
3536
'tests/**/binary/**/*.test.ts',

gulp/tasks/dev.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ const gulp = require('gulp');
1717
const config = require('../config');
1818

1919
// Ensure that the test tasks get set up
20-
require('./test');
20+
const testFxns = require('./test');
2121

2222
function watchDevFiles() {
2323
const stream = gulp.watch([
2424
`${config.root}/src/**/*.ts`,
2525
config.paths.test.unit
26-
], gulp.parallel('test:unit'));
26+
], testFxns.runBrowserUnitTests(true));
2727

28-
stream.on('error', () => {});
28+
stream.on('error', err => {});
2929
return stream;
3030
}
3131

3232
gulp.task('dev', gulp.parallel([
33-
'test:unit',
3433
watchDevFiles
3534
]));

gulp/tasks/test.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,29 @@ function runNodeBinaryTests() {
5454
/**
5555
* Runs all of the browser unit tests in karma
5656
*/
57-
function runBrowserUnitTests(done) {
58-
const karmaConfig = Object.assign({}, config.karma, {
59-
// list of files / patterns to load in the browser
60-
files: [
61-
'./+(src|tests)/**/*.ts'
62-
],
63-
64-
// list of files to exclude from the included globs above
65-
exclude: [
66-
// we don't want this file as it references files that only exist once compiled
67-
`./src/firebase.ts`,
57+
function runBrowserUnitTests(dev) {
58+
return (done) => {
59+
const karmaConfig = Object.assign({}, config.karma, {
60+
// list of files / patterns to load in the browser
61+
files: [
62+
'./+(src|tests)/**/*.ts'
63+
],
64+
65+
// list of files to exclude from the included globs above
66+
exclude: [
67+
// we don't want this file as it references files that only exist once compiled
68+
`./src/firebase-*.ts`,
6869

69-
// Don't include node test files
70-
'./tests/**/node/**/*.test.ts',
70+
// Don't include node test files
71+
'./tests/**/node/**/*.test.ts',
7172

72-
// Don't include binary test files
73-
'./tests/**/binary/**/*.test.ts',
74-
],
75-
});
76-
new karma.Server(karmaConfig, done).start();
73+
// Don't include binary test files
74+
'./tests/**/binary/**/*.test.ts',
75+
],
76+
browsers: !!dev ? ['Chrome'] : config.karma.browsers
77+
});
78+
new karma.Server(karmaConfig, done).start();
79+
};
7780
}
7881

7982
/**
@@ -121,9 +124,9 @@ function runAllKarmaTests(done) {
121124
}
122125

123126
gulp.task('test:unit:node', runNodeUnitTests);
124-
gulp.task('test:unit:browser', runBrowserUnitTests);
127+
gulp.task('test:unit:browser', runBrowserUnitTests());
125128

126-
const unitTestSuite = gulp.parallel(runNodeUnitTests, runBrowserUnitTests);
129+
const unitTestSuite = gulp.parallel(runNodeUnitTests, runBrowserUnitTests());
127130
gulp.task('test:unit', unitTestSuite);
128131

129132
gulp.task('test:binary:browser', runBrowserBinaryTests);
@@ -137,3 +140,6 @@ gulp.task('test', gulp.parallel([
137140
runNodeBinaryTests,
138141
runAllKarmaTests
139142
]));
143+
144+
exports.runNodeUnitTests = runNodeUnitTests;
145+
exports.runBrowserUnitTests = runBrowserUnitTests;

src/database/api/Query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class Query {
217217
* @param context
218218
* @return {!firebase.Promise}
219219
*/
220-
once(eventType: string, userCallback: (a: DataSnapshot, b?: string) => any,
220+
once(eventType: string, userCallback?: (a: DataSnapshot, b?: string) => any,
221221
cancelOrContext?, context?: Object) {
222222
validateArgCount('Query.once', 1, 4, arguments.length);
223223
validateEventType('Query.once', 1, eventType, false);

src/database/core/util/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const buildLogMessage_ = function(var_args) {
114114
* Use this for all debug messages in Firebase.
115115
* @type {?function(string)}
116116
*/
117-
export var logger = console.log.bind(console);
117+
export var logger = null;
118118

119119

120120
/**

src/database/realtime/BrowserPollConnection.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ export class BrowserPollConnection implements Transport {
123123
return;
124124

125125
//Set up a callback that gets triggered once a connection is set up.
126-
this.scriptTagHolder = new FirebaseIFrameScriptHolder((command, arg1, arg2, arg3, arg4) => {
127-
this.incrementIncomingBytes_(arguments);
126+
this.scriptTagHolder = new FirebaseIFrameScriptHolder((...args) => {
127+
const [command, arg1, arg2, arg3, arg4] = args;
128+
this.incrementIncomingBytes_(args);
128129
if (!this.scriptTagHolder)
129130
return; // we closed the connection.
130131

@@ -152,8 +153,9 @@ export class BrowserPollConnection implements Transport {
152153
} else {
153154
throw new Error('Unrecognized command received: ' + command);
154155
}
155-
}, (pN, data) => {
156-
this.incrementIncomingBytes_(arguments);
156+
}, (...args) => {
157+
const [pN, data] = args;
158+
this.incrementIncomingBytes_(args);
157159
this.myPacketOrderer.handleResponse(pN, data);
158160
}, () => {
159161
this.onClosed_();

src/utils/nodePatches.ts

Lines changed: 96 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,116 +8,115 @@ CONSTANTS.NODE_CLIENT = true;
88
*/
99
(function() {
1010
var version = process['version'];
11-
if (version === 'v0.10.22' || version === 'v0.10.23' || version === 'v0.10.24') {
12-
/**
13-
* The following duplicates much of `/lib/_stream_writable.js` at
14-
* b922b5e90d2c14dd332b95827c2533e083df7e55, applying the fix for
15-
* https://github.com/joyent/node/issues/6506. Note that this fix also
16-
* needs to be applied to `Duplex.prototype.write()` (in
17-
* `/lib/_stream_duplex.js`) as well.
18-
*/
19-
var Writable = require('_stream_writable');
20-
21-
Writable['prototype']['write'] = function(chunk, encoding, cb) {
22-
var state = this['_writableState'];
23-
var ret = false;
24-
25-
if (typeof encoding === 'function') {
26-
cb = encoding;
27-
encoding = null;
28-
}
29-
30-
if (Buffer['isBuffer'](chunk))
31-
encoding = 'buffer';
32-
else if (!encoding)
33-
encoding = state['defaultEncoding'];
34-
35-
if (typeof cb !== 'function')
36-
cb = function() {};
37-
38-
if (state['ended'])
39-
writeAfterEnd(this, state, cb);
40-
else if (validChunk(this, state, chunk, cb))
41-
ret = writeOrBuffer(this, state, chunk, encoding, cb);
42-
43-
return ret;
44-
};
45-
46-
function writeAfterEnd(stream, state, cb) {
47-
var er = new Error('write after end');
48-
// TODO: defer error events consistently everywhere, not just the cb
11+
if (version !== 'v0.10.22' && version !== 'v0.10.23' && version !== 'v0.10.24') return;
12+
/**
13+
* The following duplicates much of `/lib/_stream_writable.js` at
14+
* b922b5e90d2c14dd332b95827c2533e083df7e55, applying the fix for
15+
* https://github.com/joyent/node/issues/6506. Note that this fix also
16+
* needs to be applied to `Duplex.prototype.write()` (in
17+
* `/lib/_stream_duplex.js`) as well.
18+
*/
19+
var Writable = require('_stream_writable');
20+
21+
Writable['prototype']['write'] = function(chunk, encoding, cb) {
22+
var state = this['_writableState'];
23+
var ret = false;
24+
25+
if (typeof encoding === 'function') {
26+
cb = encoding;
27+
encoding = null;
28+
}
29+
30+
if (Buffer['isBuffer'](chunk))
31+
encoding = 'buffer';
32+
else if (!encoding)
33+
encoding = state['defaultEncoding'];
34+
35+
if (typeof cb !== 'function')
36+
cb = function() {};
37+
38+
if (state['ended'])
39+
writeAfterEnd(this, state, cb);
40+
else if (validChunk(this, state, chunk, cb))
41+
ret = writeOrBuffer(this, state, chunk, encoding, cb);
42+
43+
return ret;
44+
};
45+
46+
function writeAfterEnd(stream, state, cb) {
47+
var er = new Error('write after end');
48+
// TODO: defer error events consistently everywhere, not just the cb
49+
stream['emit']('error', er);
50+
process['nextTick'](function() {
51+
cb(er);
52+
});
53+
}
54+
55+
function validChunk(stream, state, chunk, cb) {
56+
var valid = true;
57+
if (!Buffer['isBuffer'](chunk) &&
58+
'string' !== typeof chunk &&
59+
chunk !== null &&
60+
chunk !== undefined &&
61+
!state['objectMode']) {
62+
var er = new TypeError('Invalid non-string/buffer chunk');
4963
stream['emit']('error', er);
5064
process['nextTick'](function() {
5165
cb(er);
5266
});
67+
valid = false;
5368
}
69+
return valid;
70+
}
5471

55-
function validChunk(stream, state, chunk, cb) {
56-
var valid = true;
57-
if (!Buffer['isBuffer'](chunk) &&
58-
'string' !== typeof chunk &&
59-
chunk !== null &&
60-
chunk !== undefined &&
61-
!state['objectMode']) {
62-
var er = new TypeError('Invalid non-string/buffer chunk');
63-
stream['emit']('error', er);
64-
process['nextTick'](function() {
65-
cb(er);
66-
});
67-
valid = false;
68-
}
69-
return valid;
70-
}
71-
72-
function writeOrBuffer(stream, state, chunk, encoding, cb) {
73-
chunk = decodeChunk(state, chunk, encoding);
74-
if (Buffer['isBuffer'](chunk))
75-
encoding = 'buffer';
76-
var len = state['objectMode'] ? 1 : chunk['length'];
72+
function writeOrBuffer(stream, state, chunk, encoding, cb) {
73+
chunk = decodeChunk(state, chunk, encoding);
74+
if (Buffer['isBuffer'](chunk))
75+
encoding = 'buffer';
76+
var len = state['objectMode'] ? 1 : chunk['length'];
7777

78-
state['length'] += len;
78+
state['length'] += len;
7979

80-
var ret = state['length'] < state['highWaterMark'];
81-
// we must ensure that previous needDrain will not be reset to false.
82-
if (!ret)
83-
state['needDrain'] = true;
80+
var ret = state['length'] < state['highWaterMark'];
81+
// we must ensure that previous needDrain will not be reset to false.
82+
if (!ret)
83+
state['needDrain'] = true;
8484

85-
if (state['writing'])
86-
state['buffer']['push'](new WriteReq(chunk, encoding, cb));
87-
else
88-
doWrite(stream, state, len, chunk, encoding, cb);
85+
if (state['writing'])
86+
state['buffer']['push'](new WriteReq(chunk, encoding, cb));
87+
else
88+
doWrite(stream, state, len, chunk, encoding, cb);
8989

90-
return ret;
91-
}
92-
93-
function decodeChunk(state, chunk, encoding) {
94-
if (!state['objectMode'] &&
95-
state['decodeStrings'] !== false &&
96-
typeof chunk === 'string') {
97-
chunk = new Buffer(chunk, encoding);
98-
}
99-
return chunk;
100-
}
90+
return ret;
91+
}
10192

102-
/**
103-
* @constructor
104-
*/
105-
function WriteReq(chunk, encoding, cb) {
106-
this['chunk'] = chunk;
107-
this['encoding'] = encoding;
108-
this['callback'] = cb;
93+
function decodeChunk(state, chunk, encoding) {
94+
if (!state['objectMode'] &&
95+
state['decodeStrings'] !== false &&
96+
typeof chunk === 'string') {
97+
chunk = new Buffer(chunk, encoding);
10998
}
99+
return chunk;
100+
}
110101

111-
function doWrite(stream, state, len, chunk, encoding, cb) {
112-
state['writelen'] = len;
113-
state['writecb'] = cb;
114-
state['writing'] = true;
115-
state['sync'] = true;
116-
stream['_write'](chunk, encoding, state['onwrite']);
117-
state['sync'] = false;
118-
}
102+
/**
103+
* @constructor
104+
*/
105+
function WriteReq(chunk, encoding, cb) {
106+
this['chunk'] = chunk;
107+
this['encoding'] = encoding;
108+
this['callback'] = cb;
109+
}
119110

120-
var Duplex = require('_stream_duplex');
121-
Duplex['prototype']['write'] = Writable['prototype']['write'];
111+
function doWrite(stream, state, len, chunk, encoding, cb) {
112+
state['writelen'] = len;
113+
state['writecb'] = cb;
114+
state['writing'] = true;
115+
state['sync'] = true;
116+
stream['_write'](chunk, encoding, state['onwrite']);
117+
state['sync'] = false;
122118
}
119+
120+
var Duplex = require('_stream_duplex');
121+
Duplex['prototype']['write'] = Writable['prototype']['write'];
123122
})();

0 commit comments

Comments
 (0)