Skip to content

Commit 8298cf8

Browse files
fix firestore node tests (#5741)
* fix firestore node tests * Create plenty-trains-switch.md * IntelliJ and VSCode runners for Babel (#5756) * format Co-authored-by: Sebastian Schmidt <[email protected]>
1 parent 34fbca4 commit 8298cf8

24 files changed

+739
-415
lines changed

.changeset/plenty-trains-switch.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@firebase/firestore-compat": patch
3+
"@firebase/firestore": patch
4+
---
5+
6+
fix firestore node tests

.vscode/launch.json

+5-15
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@
2929
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
3030
"cwd": "${workspaceRoot}/packages/firestore",
3131
"args": [
32-
"--require", "ts-node/register/type-check",
32+
"--require", "babel-register.js",
3333
"--require", "index.node.ts",
3434
"--timeout", "5000",
3535
"test/{,!(browser|integration)/**/}*.test.ts",
3636
"--exit"
3737
],
38-
"env": {
39-
"TS_NODE_CACHE": "NO",
40-
"TS_NODE_COMPILER_OPTIONS" : "{\"module\":\"commonjs\"}"
41-
},
4238
"sourceMaps": true,
4339
"protocol": "inspector"
4440
},
@@ -49,17 +45,15 @@
4945
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
5046
"cwd": "${workspaceRoot}/packages/firestore",
5147
"args": [
52-
"--require", "ts-node/register/type-check",
48+
"--require", "babel-register.js",
5349
"--require", "index.node.ts",
5450
"--require", "test/util/node_persistence.ts",
5551
"--timeout", "5000",
5652
"test/{,!(browser|integration)/**/}*.test.ts",
5753
"--exit"
5854
],
5955
"env": {
60-
"USE_MOCK_PERSISTENCE": "YES",
61-
"TS_NODE_CACHE": "NO",
62-
"TS_NODE_COMPILER_OPTIONS" : "{\"module\":\"commonjs\"}"
56+
"USE_MOCK_PERSISTENCE": "YES"
6357
},
6458
"sourceMaps": true,
6559
"protocol": "inspector"
@@ -71,15 +65,13 @@
7165
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
7266
"cwd": "${workspaceRoot}/packages/firestore",
7367
"args": [
74-
"--require", "ts-node/register/type-check",
68+
"--require", "babel-register.js",
7569
"--require", "index.node.ts",
7670
"--timeout", "5000",
7771
"test/{,!(browser|unit)/**/}*.test.ts",
7872
"--exit"
7973
],
8074
"env": {
81-
"TS_NODE_CACHE": "NO",
82-
"TS_NODE_COMPILER_OPTIONS" : "{\"module\":\"commonjs\"}",
8375
"FIRESTORE_EMULATOR_PORT" : "8080",
8476
"FIRESTORE_EMULATOR_PROJECT_ID" : "test-emulator"
8577
},
@@ -93,7 +85,7 @@
9385
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
9486
"cwd": "${workspaceRoot}/packages/firestore",
9587
"args": [
96-
"--require", "ts-node/register/type-check",
88+
"--require", "babel-register.js",
9789
"--require", "index.node.ts",
9890
"--require", "test/util/node_persistence.ts",
9991
"--timeout", "5000",
@@ -102,8 +94,6 @@
10294
],
10395
"env": {
10496
"USE_MOCK_PERSISTENCE": "YES",
105-
"TS_NODE_CACHE": "NO",
106-
"TS_NODE_COMPILER_OPTIONS" : "{\"module\":\"commonjs\"}",
10797
"FIRESTORE_EMULATOR_PORT" : "8080",
10898
"FIRESTORE_EMULATOR_PROJECT_ID" : "test-emulator"
10999
},

config/mocharc.node.js

+5
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ if (process.env.CI) {
3434
config.reporter = 'min';
3535
}
3636

37+
// Firestore uses babel to compile tests in Nodejs
38+
if (process.env.NO_TS_NODE) {
39+
delete config.require;
40+
}
41+
3742
module.exports = config;

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@
6262
"repo-scripts/*"
6363
],
6464
"devDependencies": {
65-
"@babel/core": "7.15.5",
66-
"@babel/plugin-transform-modules-commonjs": "7.15.4",
67-
"@babel/preset-env": "7.15.6",
65+
"@babel/core": "7.16.0",
66+
"@babel/plugin-transform-modules-commonjs": "7.16.0",
67+
"@babel/preset-env": "7.16.0",
68+
"@babel/register": "7.16.0",
69+
"@babel/preset-typescript": "7.16.0",
70+
"babel-plugin-transform-import-meta": "2.1.0",
6871
"@changesets/changelog-github": "0.4.1",
6972
"@changesets/cli": "2.17.0",
7073
"@types/chai": "4.2.22",

packages/firestore-compat/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function configureForFirebase(
8484
const firestoreExp = container.getProvider('firestore').getImmediate()!;
8585
return firestoreFactory(app, firestoreExp);
8686
},
87-
ComponentType.PUBLIC
87+
'PUBLIC' as ComponentType.PUBLIC
8888
).setServiceProps({ ...firestoreNamespace })
8989
);
9090
}

packages/firestore-compat/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// eslint-disable-next-line import/no-extraneous-dependencies
1919
import firebase from '@firebase/app-compat';
20-
import { FirebaseNamespace } from '@firebase/app-types';
20+
import type { FirebaseNamespace } from '@firebase/app-types';
2121
import * as types from '@firebase/firestore-types';
2222

2323
import { name, version } from '../package.json';

packages/firestore/.idea/runConfigurations/All_Tests__Emulator_.xml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/All_Tests__Emulator_w__Mock_Persistence_.xml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Integration_Tests__Emulator_.xml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Integration_Tests__Emulator_w__Mock_Persistence_.xml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Unit_Tests.xml

+1-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/Unit_Tests__w__Mock_Persistence_.xml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/.idea/runConfigurations/firestore_lite_Tests__Emulator_.xml

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/babel-register.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require('@babel/register')({ extensions: ['.js', '.ts'] });

packages/firestore/babel.config.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [
3+
"@babel/preset-typescript",
4+
["@babel/preset-env", {"targets": {"node": "10"}, "modules": "cjs"}]
5+
],
6+
"plugins": ["babel-plugin-transform-import-meta"]
7+
}

packages/firestore/lite/register.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function registerFirestore(): void {
5858
}
5959
return firestoreInstance;
6060
},
61-
ComponentType.PUBLIC
61+
'PUBLIC' as ComponentType.PUBLIC
6262
)
6363
);
6464
// RUNTIME_ENV and BUILD_TARGET are replaced by real values during the compilation

packages/firestore/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"test:browser": "karma start --single-run",
3535
"test:browser:debug": "karma start --browsers=Chrome --auto-watch",
3636
"test:node": "node ./scripts/run-tests.js --main=test/register.ts --emulator 'test/{,!(browser|lite)/**/}*.test.ts'",
37-
"test:node:prod": "ts-node-script ./scripts/run-tests.ts --main=test/register.ts 'test/{,!(browser|lite)/**/}*.test.ts'",
37+
"test:node:prod": "node ./scripts/run-tests.js --main=test/register.ts 'test/{,!(browser|lite)/**/}*.test.ts'",
3838
"test:node:persistence": "node ./scripts/run-tests.js --main=test/register.ts --persistence --emulator 'test/{,!(browser|lite)/**/}*.test.ts'",
3939
"test:node:persistence:prod": "node ./scripts/run-tests.js --main=test/register.ts --persistence 'test/{,!(browser|lite)/**/}*.test.ts'",
4040
"test:travis": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/firestore-test-runner.ts",

packages/firestore/scripts/run-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
17-
*/exports.__esModule=true;var path_1=require("path");var child_process_promise_1=require("child-process-promise");var yargs=require("yargs");var argv=yargs.options({main:{type:"string",demandOption:true},platform:{type:"string",default:"node"},emulator:{type:"boolean"},persistence:{type:"boolean"}}).argv;var nyc=path_1.resolve(__dirname,"../../../node_modules/.bin/nyc");var mocha=path_1.resolve(__dirname,"../../../node_modules/.bin/mocha");process.env.TS_NODE_CACHE="NO";process.env.TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}';process.env.TEST_PLATFORM=argv.platform;var args=["--reporter","lcovonly",mocha,"--require","ts-node/register","--require",argv.main,"--config","../../config/mocharc.node.js"];if(argv.emulator){process.env.FIRESTORE_EMULATOR_PORT="8080";process.env.FIRESTORE_EMULATOR_PROJECT_ID="test-emulator"}if(argv.persistence){process.env.USE_MOCK_PERSISTENCE="YES";args.push("--require","test/util/node_persistence.ts")}args=args.concat(argv._);var childProcess=child_process_promise_1.spawn(nyc,args,{stdio:"inherit",cwd:process.cwd()}).childProcess;process.once("exit",(function(){return childProcess.kill()}));process.once("SIGINT",(function(){return childProcess.kill("SIGINT")}));process.once("SIGTERM",(function(){return childProcess.kill("SIGTERM")}));
17+
*/exports.__esModule=true;var path_1=require("path");var child_process_promise_1=require("child-process-promise");var yargs=require("yargs");var argv=yargs.options({main:{type:"string",demandOption:true},platform:{type:"string",default:"node"},emulator:{type:"boolean"},persistence:{type:"boolean"}}).parseSync();var nyc=path_1.resolve(__dirname,"../../../node_modules/.bin/nyc");var mocha=path_1.resolve(__dirname,"../../../node_modules/.bin/mocha");var babel=path_1.resolve(__dirname,"../babel-register.js");process.env.NO_TS_NODE="true";process.env.TEST_PLATFORM=argv.platform;var args=["--reporter","lcovonly",mocha,"--require",babel,"--require",argv.main,"--config","../../config/mocharc.node.js"];if(argv.emulator){process.env.FIRESTORE_EMULATOR_PORT="8080";process.env.FIRESTORE_EMULATOR_PROJECT_ID="test-emulator"}if(argv.persistence){process.env.USE_MOCK_PERSISTENCE="YES";args.push("--require","test/util/node_persistence.ts")}args=args.concat(argv._);var childProcess=child_process_promise_1.spawn(nyc,args,{stdio:"inherit",cwd:process.cwd()}).childProcess;process.once("exit",(function(){return childProcess.kill()}));process.once("SIGINT",(function(){return childProcess.kill("SIGINT")}));process.once("SIGTERM",(function(){return childProcess.kill("SIGTERM")}));

packages/firestore/scripts/run-tests.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ const argv = yargs.options({
3939

4040
const nyc = resolve(__dirname, '../../../node_modules/.bin/nyc');
4141
const mocha = resolve(__dirname, '../../../node_modules/.bin/mocha');
42+
const babel = resolve(__dirname, '../babel-register.js');
4243

43-
process.env.TS_NODE_CACHE = 'NO';
44-
process.env.TS_NODE_COMPILER_OPTIONS = '{"module":"commonjs"}';
44+
// used in '../../config/mocharc.node.js' to disable ts-node
45+
process.env.NO_TS_NODE = "true";
4546
process.env.TEST_PLATFORM = argv.platform;
4647

4748
let args = [
4849
'--reporter',
4950
'lcovonly',
5051
mocha,
5152
'--require',
52-
'ts-node/register',
53+
babel,
5354
'--require',
5455
argv.main,
5556
'--config',

packages/firestore/src/platform/node/grpc_connection.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ import { Deferred } from '../../util/promise';
4040

4141
// This is a hack fix for Node ES modules to use `require`.
4242
// @ts-ignore To avoid using `--module es2020` flag.
43-
const require = module.createRequire(import.meta.url);
43+
const requireInESM = module.createRequire(import.meta.url);
4444
// eslint-disable-next-line @typescript-eslint/no-require-imports
45-
const { version: grpcVersion } = require('@grpc/grpc-js/package.json');
45+
const { version: grpcVersion } = requireInESM('@grpc/grpc-js/package.json');
4646

4747
const LOG_TAG = 'Connection';
4848
const X_GOOG_API_CLIENT_VALUE = `gl-node/${process.versions.node} fire/${SDK_VERSION} grpc/${grpcVersion}`;

packages/firestore/src/platform/node/load_protos.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import { fileURLToPath } from 'url';
2020

2121
// __filename and __dirname globals are unavailable in ES modules
2222
// @ts-ignore To avoid using `--module es2020` flag.
23-
const __filename = fileURLToPath(import.meta.url);
24-
const __dirname = dirname(__filename);
23+
const __filenameInESM = fileURLToPath(import.meta.url);
24+
const __dirnameInESM = dirname(__filenameInESM);
2525

2626
import { loadPackageDefinition, GrpcObject } from '@grpc/grpc-js';
2727
import { loadSync } from '@grpc/proto-loader';
@@ -44,7 +44,7 @@ export const protoLoaderOptions: IConversionOptions = {
4444
*/
4545
export function loadProtos(): GrpcObject {
4646
const root = resolve(
47-
__dirname,
47+
__dirnameInESM,
4848
process.env.FIRESTORE_PROTO_ROOT || '../../protos'
4949
);
5050
const firestoreProtoFile = join(root, 'google/firestore/v1/firestore.proto');

packages/firestore/src/register.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function registerFirestore(
5555
firestoreInstance._setSettings(settings);
5656
return firestoreInstance;
5757
},
58-
ComponentType.PUBLIC
58+
'PUBLIC' as ComponentType.PUBLIC
5959
)
6060
);
6161
registerVersion(name, version, variant);

packages/firestore/test/util/node_persistence.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as fs from 'fs';
1919
import * as os from 'os';
2020

2121
// @ts-ignore: There are no types for indexeddbshim.
22-
import * as registerIndexedDBShim from 'indexeddbshim';
22+
import registerIndexedDBShim from 'indexeddbshim';
2323

2424
import { FakeWindow, SharedFakeWebStorage } from './test_platform';
2525

0 commit comments

Comments
 (0)