使用打字稿编写Firestore云功能测试时遇到一些问题

时间:2020-08-20 14:21:43

标签: typescript firebase unit-testing google-cloud-functions

我正在为节点10 编写云函数,并且正在对函数和测试使用 typescript (使用 jest v26.4.1 ),并且遇到了一些问题:如果尝试使用es6导入语法,则会遇到一些编译错误,而如果改用require,则不会自动完成与Firebase相关的工作。任何想法如何启用es6导入语法? 这是我的测试样本:

// with next two lines addes, I don't have autocompletion for imported things, but the code works just fine

const firebase = require('@firebase/testing');
const admin = require('firebase-admin');

// with next two lines addes, I have autocompletion but the code fails

// import * as firebase from '@firebase/testing';
// import * as admin from 'firebase-admin';

const projectId = 'monefy-afbd8';
process.env.GCLOUD_PROJECT = projectId;
process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080';
const app = admin.initializeApp({ projectId });
const db = firebase.firestore(app);

beforeAll(async () => {
  await firebase.clearFirestoreData({ projectId });
});

//Document written to /TestCollection/{docId} , trigger function to copy it to '/Copies/{docId}'

test('Documents from /TestCollection are copied to /Copies', async () => {
  const testDoc = {
    name: 'Summer',
    age: 21,
    city: 'Bucharest',
  };
  const ref = db.collection('TestCollection').doc();
  await ref.set(testDoc);

  const copyId = ref.id;
  const copyRef = db.collection('Copies').doc(copyId);
  const copyDoc = await copyRef.get();

  expect(copyDoc.data).toStrictEqual(testDoc);
});

错误:

D:\Work\React\monefy\functions\src\__tests__\first.test.ts:5
    import * as firebase from '@firebase/testing';
           ^

    SyntaxError: Unexpected token *

      at Runtime._execModule (../node_modules/jest-runtime/build/index.js:1179:56)

如何启用es6导入语法?

0 个答案:

没有答案