使用vitest测试Firebase可调用函数

a64a0gku  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(200)

Hi写了这个函数,它接受一个URL并发送回用Metascrapper抓取的页面的元数据

import * as functions from "firebase-functions";
import metascraper from "metascraper";
import metascraperTitle from "metascraper-title";
import metascraperDescription from "metascraper-description";

export const getMetadata = functions.https.onCall(async (data, context) => {
  try {
    const url = data.url;
    const res = await fetch(url);
    const html = await res.text();
    const metadata = await metascraper([
      metascraperTitle(),
      metascraperDescription(),
    ])({html, url});
    return {
      title: metadata.title,
      description: metadata.description,
    };
  } catch (error) {
    functions.logger.error(error);
  }
  return false;
});

我写了这个测试来测试这个可调用函数:

import {describe , it, expect} from "vitest"
import { getMetadata } from "./getMetadata";

describe('getMetadata', () => {
  it('should return metadata for a given URL', async () => {
    const data= { 'url': 'https://example.com' };
    const context = { };
   
    const result = await getMetadata(data, context);
    
    expect(result).toEqual({ title: 'Example Domain', description: null });
  });
});

这是我得到的错误

FAIL  src/getMetadata.test.ts > getMetadata > should return metadata for a given URL
TypeError: res.on is not a function
 ❯ node_modules/firebase-functions/lib/common/providers/https.js:392:17
 ❯ Module.<anonymous> node_modules/firebase-functions/lib/common/providers/https.js:391:16
 ❯ src/getMetadata.test.ts:10:26
      8|     const context = { };
      9|    
     10|     const result = await getMetadata(data, context);
       |                          ^
     11|     
     12|     expect(result).toEqual({ title: 'Example Domain', description: null });

有人可以帮助我不明白错误,如何修复它?

fafcakar

fafcakar1#

使用firebase-functions-test包测试onCall函数。简单的解释是,您将使用该包 Package 您的原始函数,然后调用 Package 的函数。
下面是一个修改过的函数示例:

import * as functions from "firebase-functions";

export const getMetadata = functions.https.onCall(async (data, context) => {
  try {
    const url = data.url;
    const res = await fetch(url);
    const html = await res.text();
    return {
      url,
      html
    };
  } catch (error) {
    functions.logger.error(error);
  }
  return false;
});

下面是一个测试,它将成功通过:

import { describe , it, expect } from "vitest";
import { getMetadata } from "./getMetadata";
import firebaseFunctionsTest from "firebase-functions-test";

// Extracting `wrap` out of the lazy-loaded features
const { wrap } = firebaseFunctionsTest();

describe('getMetadata', () => {
  it('should return metadata for a given URL', async () => {
    // Wrap your function here and then call your wrapped function
    const wrappedFirebaseFunction = wrap(getMetadata);
    const data = { url: 'https://example.com' };
    const result = await wrappedFirebaseFunction(data);

    expect(result.url).toEqual('https://example.com');
  });
});

输出:

vitest run index.test.js

 RUN  v0.31.0 /Users/foo/temp

 ✓ index.test.js (1)

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  10:36:23
   Duration  703ms (transform 21ms, setup 0ms, collect 223ms, tests 67ms, environment 0ms, prepare 64ms)

您可以在Unit testing of Cloud Functions上阅读更多关于单元测试函数的信息。

相关问题