Jest.js TypeError [ERR_INVALID_ARG_TYPE]:“path”参数必须是字符串类型,接收到Object的示例

wbrvyc0a  于 2023-06-20  发布在  Jest
关注(0)|答案(2)|浏览(159)

我正在使用安全规则tutorial中的source code来尝试使用Jest对我的JavaScript异步函数async_create_post进行集成测试,该函数用于我的firebase HTTP函数create_post
测试文件:root/tests/handlers/posts.test.js
待测试文件:root/functions/handlers/posts.js
教程中的帮助程序代码:root/tests/rules/helpers.js
下面是所涉及的源代码:

posts.test.js

const { setup, teardown} = require("../rules/helpers");
const {
  async_get_all_undeleted_posts,
  async_get_post,
  async_delete_post,
  async_create_post
} = require("../../functions/handlers/posts");

describe("Post Creation", () => {
  afterEach(async () => {
    await teardown();
  });

  test("should create a post", async () => {

    const db = await setup();

    const malloryUID = "non-existent uid";
    const firstPost = {
      body: "First post from Mallory",
      author_id: malloryUID,
      images: ["url1", "url2"]
    }

    const before_post_snapshot = await db.collection("posts").get();
    expect(before_post_snapshot.docs.length).toBe(0);

    await async_create_post(firstPost); //fails at this point, expected to create a new post, but instead threw an error
    const after_post_snapshot = await db.collection("posts").get();
    expect(after_post_snapshot.docs.length).toBe(1);
  });

});

posts.js

const {admin, db } = require('../util/admin');
//admin.initializeApp(config); //my credentials
//const db = admin.firestore();
const { uuid } = require("uuidv4");

const {
  success_response,
  error_response
} = require("../util/validators");

exports.async_create_post = async (data, context) => {

  try {    
    const images = [];
    data.images.forEach((url) => {
      images.push({
        uid: uuid(),
        url: url
      });
    })
    const postRecord = {
      body: data.body,
      images: images,
      last_updated: admin.firestore.FieldValue.serverTimestamp(),
      like_count: 0,
      comment_count: 0,
      deleted: false,
      author_id: data.author_id
    };

    const generatedToken = uuid();

    await db
      .collection("posts")
      .doc(generatedToken)
      .set(postRecord);

    // return success_response();
    return success_response(generatedToken);
  } catch (error) {
    console.log("Error in creation of post", error);
    return error_response(error);
  }
}

当我在Webstorm IDE中运行测试时,有一个终端运行Firebase emulators:start,我得到以下错误消息。

console.log
    Error in creation of post TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Object
        at validateString (internal/validators.js:120:11)
        at Object.basename (path.js:1156:5)
        at GrpcClient.loadProto (/Users/isaac/Desktop/project/functions/node_modules/google-gax/src/grpc.ts:166:23)
        at new FirestoreClient (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:118:38)
        at ClientPool.clientFactory (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/index.js:330:26)
        at ClientPool.acquire (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/pool.js:87:35)
        at ClientPool.run (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/pool.js:164:29)
        at Firestore.request (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/index.js:961:33)
        at WriteBatch.commit_ (/Users/isaac/Desktop/project/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:485:48)
        at exports.async_create_post (/Users/isaac/Desktop/project/functions/handlers/posts.js:36:5) {
      code: 'ERR_INVALID_ARG_TYPE'
    }

      at exports.async_create_post (/Users/isaac/Desktop/project/functions/handlers/posts.js:44:13)

Error: expect(received).toBe(expected) // Object.is equality

Expected: 1
Received: 0
<Click to see difference>

    at Object.<anonymous> (/Users/isaac/Desktop/project/tests/handlers/posts.test.js:59:45)

Error in creation of post来自posts.js中的console.log("Error in creation of post", error);,因此错误显示在本文的标题中。
我想知道为什么从posts.test.js调用async_create_post会导致这个错误,并且没有像预期的行为那样用额外的记录填充我的数据库。如果需要更多的信息来解决这个问题,请告诉我。
下面是一些代码片段,可以给予更多的上下文。

helpers.js[从仓库复制]

const firebase = require("@firebase/testing");
const fs = require("fs");

module.exports.setup = async (auth, data) => {
  const projectId = `rules-spec-${Date.now()}`;

  const app = firebase.initializeTestApp({
    projectId,
    auth
  });

  const db = app.firestore();

  // Apply the test rules so we can write documents
  await firebase.loadFirestoreRules({
    projectId,
    rules: fs.readFileSync("firestore-test.rules", "utf8")
  });

  // write mock documents if any
  if (data) {
    for (const key in data) {
      const ref = db.doc(key); // This means the key should point directly to a document
      await ref.set(data[key]);
    }
  }

  // Apply the actual rules for the project
  await firebase.loadFirestoreRules({
    projectId,
    rules: fs.readFileSync("firestore.rules", "utf8")
  });

  return db;
  // return firebase;
};

module.exports.teardown = async () => {
  // Delete all apps currently running in the firebase simulated environment
  Promise.all(firebase.apps().map(app => app.delete()));
};

// Add extensions onto the expect method
expect.extend({
  async toAllow(testPromise) {
    let pass = false;
    try {
      await firebase.assertSucceeds(testPromise);
      pass = true;
    } catch (error) {
      // log error to see which rules caused the test to fail
      console.log(error);
    }

    return {
      pass,
      message: () =>
        "Expected Firebase operation to be allowed, but it was denied"
    };
  }
});

expect.extend({
  async toDeny(testPromise) {
    let pass = false;
    try {
      await firebase.assertFails(testPromise);
      pass = true;
    } catch (error) {
      // log error to see which rules caused the test to fail
      console.log(error);
    }

    return {
      pass,
      message: () =>
        "Expected Firebase operation to be denied, but it was allowed"
    };
  }
});

index.js

const functions = require('firebase-functions');

const {
  async_get_all_undeleted_posts,
  async_get_post,
  async_delete_post,
  async_create_post
} = require('./handlers/posts');


exports.create_post = functions.https.onCall(async_create_post);
oxalkeyp

oxalkeyp1#

我在NextJs 13.3.1 appDir中遇到了这个问题。nextJs支持将父元数据共享到其子路由,例如开放图图像。该错误是在使用ResolvingMetadata获取generateMetadata()函数的父元数据时导致的,13.3.1版不支持ResolvingMetadata。它发生在这样一个动态路由中:[id]/page.tsx

import type { Metadata, ResolvingMetadata } from "next";

export async function generateMetadata(
  {
    params: { id },
  }: {
    params: { id: string };
  },
  parent: ResolvingMetadata
): Promise<Metadata> {
  // your metadata implementation here ...
}

我将父元数据实现移到下面的实现中,它工作得很好。

import type { Metadata } from "next";

export async function generateMetadata({
  params: { id },
}: {
  params: { id: string };
}): Promise<Metadata> {
   // your metadata implementation here ...
}

它在13.4.4版本中运行良好,在它之前的任何其他版本中都不太确定。

trnvg8h3

trnvg8h32#

错误消息意味着path模块的一个方法(如path.join)期望它的一个参数是字符串,但得到了其他东西。
我通过二进制搜索注解程序直到错误消失才找到了有问题的行。
也许你的某个模块使用了path,而你提供了错误的参数。

相关问题