Skip to content

Commit 353fc52

Browse files
mmermerkayaFeiyang1
authored andcommitted
Fix SauceLabs tests (#1920)
* Fix SauceLabs tests for Installations and Performance * add resolveJsonModule * stub storage differently so tests work in Firefox and IE (#1940) * stub storage differently so tests work in Firefox and IE * [AUTOMATED]: Prettier Code Styling * rewrite test setup * [AUTOMATED]: Prettier Code Styling * change compilation target to es5 * fix IE11 errors * fix storage IE11 tests * fix util IE11 test * fix performance IE11 tests * make installations run in IE * fix integration/typescript tests in saucelabs * [AUTOMATED]: Prettier Code Styling * revert change that doesn't work * fix lint * add babel loader
1 parent e24b1f4 commit 353fc52

File tree

10 files changed

+136
-55
lines changed

10 files changed

+136
-55
lines changed

config/karma.saucelabs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ module.exports = function(config) {
169169
preprocessors: {
170170
'packages/polyfill/index.ts': ['webpack', 'sourcemap'],
171171
'**/test/**/*.ts': ['webpack', 'sourcemap'],
172+
'**/*.test.ts': ['webpack', 'sourcemap'],
172173
'packages/firestore/test/**/bootstrap.ts': ['webpack', 'babel'],
173174
'integration/**/namespace.*': ['webpack', 'babel', 'sourcemap']
174175
},

config/webpack.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ module.exports = {
3131
options: {
3232
compilerOptions: {
3333
module: 'commonjs',
34-
downlevelIteration: true
34+
target: 'es5',
35+
downlevelIteration: true,
36+
resolveJsonModule: true
3537
}
3638
}
3739
}
@@ -49,11 +51,29 @@ module.exports = {
4951
},
5052
enforce: 'post',
5153
include: path.resolve(process.cwd(), 'src')
54+
},
55+
{
56+
test: /\.js$/,
57+
include: [/node_modules\/chai-as-promised/],
58+
use: {
59+
loader: 'babel-loader',
60+
options: {
61+
presets: [
62+
[
63+
'@babel/preset-env',
64+
{
65+
targets: ['ie 11']
66+
}
67+
]
68+
]
69+
}
70+
}
5271
}
5372
]
5473
},
5574
resolve: {
5675
modules: ['node_modules', path.resolve(__dirname, '../../node_modules')],
76+
mainFields: ['browser', 'main', 'module'],
5777
extensions: ['.js', '.ts']
5878
},
5979
plugins: [

integration/shared/validator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ function validateNamespace(definition, candidate) {
106106
}
107107

108108
module.exports = validateNamespace;
109+
module.exports.default = validateNamespace;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"typedoc": "0.15.0",
7171
"ts-node": "8.3.0",
7272
"typescript": "3.5.3",
73-
"yargs": "14.0.0"
73+
"yargs": "14.0.0",
74+
"babel-loader": "8.0.6"
7475
}
7576
}

packages/installations/src/helpers/buffer-to-base64-url-safe.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ import { expect } from 'chai';
1919
import '../testing/setup';
2020
import { bufferToBase64UrlSafe } from './buffer-to-base64-url-safe';
2121

22-
const TYPED_ARRAY_REPRESENTATION = new TextEncoder().encode('hello world');
23-
const BASE_64_REPRESENTATION = btoa('hello world');
22+
const str = 'hello world';
23+
const TYPED_ARRAY_REPRESENTATION = new Uint8Array(str.length);
24+
for (let i = 0; i < str.length; i++) {
25+
TYPED_ARRAY_REPRESENTATION[i] = str.charCodeAt(i);
26+
}
27+
28+
const BASE_64_REPRESENTATION = btoa(str);
2429

2530
describe('bufferToBase64', () => {
2631
it('returns a base64 representation of a Uint8Array', () => {

packages/performance/src/services/api_service.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ describe('Firebase Performance > api_service', () => {
3131
};
3232

3333
const mockWindow = { ...self };
34+
// hack for IE11. self.hasOwnProperty('performance') returns false in IE11
35+
mockWindow.performance = self.performance;
3436

3537
let api: Api;
3638

packages/performance/src/services/remote_config_service.test.ts

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import { stub, SinonStub, useFakeTimers, SinonFakeTimers } from 'sinon';
17+
import { stub, useFakeTimers, SinonFakeTimers, SinonStub } from 'sinon';
1818
import { expect } from 'chai';
1919
import { SettingsService } from './settings_service';
2020
import { CONFIG_EXPIRY_LOCAL_STORAGE_KEY } from '../constants';
21-
import { setupApi } from './api_service';
21+
import { setupApi, Api } from './api_service';
2222
import * as iidService from './iid_service';
2323
import { getConfig } from './remote_config_service';
2424
import { FirebaseApp } from '@firebase/app-types';
@@ -41,11 +41,10 @@ describe('Performance Monitoring > remote_config_service', () => {
4141
const APP_ID = '1:23r:web:fewq';
4242
const API_KEY = 'asdfghjk';
4343

44-
let fetchStub: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
45-
let storageGetItemStub: SinonStub<[string], string | null>;
4644
let clock: SinonFakeTimers;
4745

4846
setupApi(self);
47+
const ApiInstance = Api.getInstance();
4948

5049
function storageGetItemFakeFactory(
5150
expiry: string,
@@ -67,17 +66,47 @@ describe('Performance Monitoring > remote_config_service', () => {
6766
settingsService.tracesSamplingRate = 1;
6867
}
6968

70-
beforeEach(() => {
71-
fetchStub = stub(self, 'fetch');
72-
storageGetItemStub = stub(self.localStorage, 'getItem');
69+
// parameterized beforeEach. Should be called at beginning of each test.
70+
function setup(
71+
storageConfig: { expiry: string; config: string },
72+
fetchConfig?: { reject: boolean; value?: Response }
73+
): {
74+
storageGetItemStub: SinonStub<[string], string | null>;
75+
fetchStub: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
76+
} {
77+
const fetchStub = stub(self, 'fetch');
78+
79+
if (fetchConfig) {
80+
fetchConfig.reject
81+
? fetchStub.rejects()
82+
: fetchStub.resolves(fetchConfig.value);
83+
}
84+
7385
stub(iidService, 'getAuthTokenPromise').returns(
7486
Promise.resolve(AUTH_TOKEN)
7587
);
88+
7689
clock = useFakeTimers(GLOBAL_CLOCK_NOW);
7790
SettingsService.prototype.firebaseAppInstance = ({
7891
options: { projectId: PROJECT_ID, appId: APP_ID, apiKey: API_KEY }
7992
} as unknown) as FirebaseApp;
80-
});
93+
94+
// we need to stub the entire localStorage, because storage can't be stubbed in Firefox and IE.
95+
// stubbing on self(window) seems to only work the first time (at least in Firefox), the subsequent
96+
// tests will have the same stub. stub.reset() in afterEach doesn't help either. As a result, we stub on ApiInstance.
97+
// https://github.com/sinonjs/sinon/issues/662
98+
const storageStub = stub(ApiInstance, 'localStorage');
99+
const getItemStub: SinonStub<[string], string | null> = stub();
100+
101+
storageStub.value({
102+
getItem: getItemStub.callsFake(
103+
storageGetItemFakeFactory(storageConfig.expiry, storageConfig.config)
104+
),
105+
setItem: () => {}
106+
});
107+
108+
return { storageGetItemStub: getItemStub, fetchStub };
109+
}
81110

82111
afterEach(() => {
83112
resetSettingsService();
@@ -88,15 +117,14 @@ describe('Performance Monitoring > remote_config_service', () => {
88117
it('gets the config from the local storage if available and valid', async () => {
89118
// After global clock. Config not expired.
90119
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895330';
91-
storageGetItemStub.callsFake(
92-
storageGetItemFakeFactory(
93-
EXPIRY_LOCAL_STORAGE_VALUE,
94-
STRINGIFIED_CONFIG
95-
)
96-
);
120+
const { storageGetItemStub: getItemStub } = setup({
121+
expiry: EXPIRY_LOCAL_STORAGE_VALUE,
122+
config: STRINGIFIED_CONFIG
123+
});
124+
97125
await getConfig(IID);
98126

99-
expect(storageGetItemStub).to.be.called;
127+
expect(getItemStub).to.be.called;
100128
expect(SettingsService.getInstance().loggingEnabled).to.be.true;
101129
expect(SettingsService.getInstance().logEndPointUrl).to.equal(LOG_URL);
102130
expect(SettingsService.getInstance().logSource).to.equal(LOG_SOURCE);
@@ -111,12 +139,12 @@ describe('Performance Monitoring > remote_config_service', () => {
111139
it('does not call remote config if a valid config is in local storage', async () => {
112140
// After global clock. Config not expired.
113141
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895330';
114-
storageGetItemStub.callsFake(
115-
storageGetItemFakeFactory(
116-
EXPIRY_LOCAL_STORAGE_VALUE,
117-
STRINGIFIED_CONFIG
118-
)
119-
);
142+
143+
const { fetchStub } = setup({
144+
expiry: EXPIRY_LOCAL_STORAGE_VALUE,
145+
config: STRINGIFIED_CONFIG
146+
});
147+
120148
await getConfig(IID);
121149

122150
expect(fetchStub).not.to.be.called;
@@ -125,16 +153,15 @@ describe('Performance Monitoring > remote_config_service', () => {
125153
it('gets the config from RC if local version is not valid', async () => {
126154
// Expired local config.
127155
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895320';
128-
storageGetItemStub.callsFake(
129-
storageGetItemFakeFactory(
130-
EXPIRY_LOCAL_STORAGE_VALUE,
131-
'not a valid config and should not be used'
132-
)
156+
157+
const { storageGetItemStub: getItemStub } = setup(
158+
{ expiry: EXPIRY_LOCAL_STORAGE_VALUE, config: STRINGIFIED_CONFIG },
159+
{ reject: false, value: new Response(STRINGIFIED_CONFIG) }
133160
);
134-
fetchStub.resolves(new Response(STRINGIFIED_CONFIG));
161+
135162
await getConfig(IID);
136163

137-
expect(storageGetItemStub).to.be.calledOnce;
164+
expect(getItemStub).to.be.calledOnce;
138165
expect(SettingsService.getInstance().loggingEnabled).to.be.true;
139166
expect(SettingsService.getInstance().logEndPointUrl).to.equal(LOG_URL);
140167
expect(SettingsService.getInstance().logSource).to.equal(LOG_SOURCE);
@@ -149,13 +176,15 @@ describe('Performance Monitoring > remote_config_service', () => {
149176
it('does not change the default config if call to RC fails', async () => {
150177
// Expired local config.
151178
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895320';
152-
storageGetItemStub.callsFake(
153-
storageGetItemFakeFactory(
154-
EXPIRY_LOCAL_STORAGE_VALUE,
155-
'not a valid config and should not be used'
156-
)
179+
180+
setup(
181+
{
182+
expiry: EXPIRY_LOCAL_STORAGE_VALUE,
183+
config: 'not a valid config and should not be used'
184+
},
185+
{ reject: true }
157186
);
158-
fetchStub.rejects();
187+
159188
await getConfig(IID);
160189

161190
expect(SettingsService.getInstance().loggingEnabled).to.equal(false);
@@ -164,17 +193,19 @@ describe('Performance Monitoring > remote_config_service', () => {
164193
it('uses secondary configs if the response does not have all the fields', async () => {
165194
// Expired local config.
166195
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895320';
167-
storageGetItemStub.callsFake(
168-
storageGetItemFakeFactory(
169-
EXPIRY_LOCAL_STORAGE_VALUE,
170-
'not a valid config and should not be used'
171-
)
172-
);
173196
const STRINGIFIED_PARTIAL_CONFIG = `{"entries":{\
174197
"fpr_vc_network_request_sampling_rate":"0.250000",\
175198
"fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
176199
"state":"UPDATE"}`;
177-
fetchStub.resolves(new Response(STRINGIFIED_PARTIAL_CONFIG));
200+
201+
setup(
202+
{
203+
expiry: EXPIRY_LOCAL_STORAGE_VALUE,
204+
config: 'not a valid config and should not be used'
205+
},
206+
{ reject: false, value: new Response(STRINGIFIED_PARTIAL_CONFIG) }
207+
);
208+
178209
await getConfig(IID);
179210

180211
expect(SettingsService.getInstance().loggingEnabled).to.be.true;
@@ -183,14 +214,15 @@ describe('Performance Monitoring > remote_config_service', () => {
183214
it('uses secondary configs if the response does not have any fields', async () => {
184215
// Expired local config.
185216
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895320';
186-
storageGetItemStub.callsFake(
187-
storageGetItemFakeFactory(
188-
EXPIRY_LOCAL_STORAGE_VALUE,
189-
'not a valid config and should not be used'
190-
)
191-
);
192217
const STRINGIFIED_PARTIAL_CONFIG = '{"state":"NO TEMPLATE"}';
193-
fetchStub.resolves(new Response(STRINGIFIED_PARTIAL_CONFIG));
218+
219+
setup(
220+
{
221+
expiry: EXPIRY_LOCAL_STORAGE_VALUE,
222+
config: 'not a valid config and should not be used'
223+
},
224+
{ reject: false, value: new Response(STRINGIFIED_PARTIAL_CONFIG) }
225+
);
194226
await getConfig(IID);
195227

196228
expect(SettingsService.getInstance().loggingEnabled).to.be.true;

packages/polyfill/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'core-js/features/array/find';
2626
import 'core-js/features/array/find-index';
2727
import 'core-js/features/array/from';
2828
import 'core-js/features/array/some';
29+
import 'core-js/features/typed-array/iterator';
2930
import 'core-js/features/object/assign';
3031
import 'core-js/features/object/entries';
3132
import 'core-js/features/object/values';
@@ -35,3 +36,4 @@ import 'core-js/features/symbol';
3536
import 'core-js/features/symbol/iterator';
3637
import 'core-js/features/map';
3738
import 'core-js/features/set';
39+
import 'core-js/features/number/is-integer';

packages/util/test/errors.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,16 @@ describe('FirebaseError', () => {
105105

106106
it('has stack', () => {
107107
const e = ERROR_FACTORY.create('generic-error');
108-
assert.isDefined(e.stack);
109-
// Multi-line match trick - .* does not match \n
110-
assert.match(e.stack!, /FirebaseError[\s\S]/);
108+
109+
// In case of IE11, stack will be set only after error is raised, so we throw the error then test.
110+
// https://docs.microsoft.com/en-us/scripting/javascript/reference/stack-property-error-javascript
111+
try {
112+
throw e;
113+
} catch (error) {
114+
assert.isDefined(error.stack);
115+
// Multi-line match trick - .* does not match \n
116+
assert.match(error.stack, /FirebaseError[\s\S]/);
117+
}
111118
});
112119

113120
it('has function names in stack trace in correct order', () => {

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,6 +3079,16 @@ babel-generator@^6.18.0:
30793079
source-map "^0.5.7"
30803080
trim-right "^1.0.1"
30813081

3082+
3083+
version "8.0.6"
3084+
resolved "https://registry.npmjs.org/babel-loader/-/babel-loader-8.0.6.tgz#e33bdb6f362b03f4bb141a0c21ab87c501b70dfb"
3085+
integrity sha512-4BmWKtBOBm13uoUwd08UwjZlaw3O9GWf456R9j+5YykFZ6LUIjIKLc0zEZf+hauxPOJs96C8k6FvYD09vWzhYw==
3086+
dependencies:
3087+
find-cache-dir "^2.0.0"
3088+
loader-utils "^1.0.2"
3089+
mkdirp "^0.5.1"
3090+
pify "^4.0.1"
3091+
30823092
babel-messages@^6.23.0:
30833093
version "6.23.0"
30843094
resolved "https://registry.npmjs.org/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e"

0 commit comments

Comments
 (0)