1
2
submitted 2 months ago by hongminhee@lemmy.ml to c/javascript@lemmy.ml
2
3

cross-posted from: https://programming.dev/post/12974961

I have a function as such:

export type SendMessageParams = {
  chatSession?: ChatSession,
  // ... other params ...
};

const sendMessage = async ({
  chatSession,
  // ... other params ...
}: SendMessageParams): Promise<void> => {
  // await chatSession?.sendMessage()
  // somewhere in implementation
};

export default sendMessage;

ChatSession is from @google/generative-ai.

I'd like to mock it in my test file as such:

let defaultParams: SendMessageParams;

beforeEach(() => {
  jest.mock('@google/generative-ai', () => ({
    ChatSession: {
      sendMessage: async (content: string) => content,
    },
  }));
  defaultParams = {
    chatSession: new ChatSession('', ''),
    // ... other params ...
  };
});

afterEach(() => {
  jest.clearAllMocks();
});

it('should send message', async () => {
  // await sendMessage();
});

When I run npm run test, I get the error saying:

 FAIL  tests/logic/actions/sendMessage.test.ts
  ● should send message

    ReferenceError: fetch is not defined

      43 |   const sendMessageInner = async (messages: Message[]) => {
      44 |     setMessageListState(messages);
    > 45 |     const result = await chatSession?.sendMessage(content);
         |                    ^
      46 |     const responseText = result?.response.text();
      47 |     if (responseText) {
      48 |       const responseMessage: Message = {

      at makeRequest (node_modules/@google/generative-ai/dist/index.js:246:9)
      at generateContent (node_modules/@google/generative-ai/dist/index.js:655:28)
      at node_modules/@google/generative-ai/dist/index.js:890:25
      at ChatSession.sendMessage (node_modules/@google/generative-ai/dist/index.js:909:9)
      at sendMessageInner (src/logic/actions/sendMessage.ts:45:20)
      at src/logic/actions/sendMessage.ts:72:7
      at sendMessage (src/logic/actions/sendMessage.ts:59:3)
      at Object.<anonymous> (tests/logic/actions/sendMessage.test.ts:44:3)

...which hints that chatSession.sendMessage method still uses the real implementation instead of mock.

I'd like to know why this happens and what the solution would be.

Thanks in advance.


Environment

  • Node 20.11.0 (lts/iron)
  • Jest 29.7.0
  • @google/generative-ai 0.5.0 (if relevant)
3
2
submitted 6 months ago* (last edited 6 months ago) by velox_vulnus@lemmy.ml to c/javascript@lemmy.ml

I'm a full-stack developer, looking for an internship at the moment. I feel like I'm really tired of React and Svelte. I am so done with how there's a lot of moving parts and complexity introduced by various tools, like ESLint, Prettier, TypeScript, and Tailwind. Can I work on a back-end application of my choice, while also having the freedom to not use a JavaScript runtime, in a professional environment? Can I also do modern JS stuff like tree-shaking, minification and code-splitting? How viable would be it to do so? And would it be easier to maintain?

4
-3
submitted 6 months ago by xiffu@kbin.social to c/javascript@lemmy.ml

Validation of javascript forms - name, password, password retype validation and Number Validation

It is critical to check the user-submitted form since it may include incor- rect information. As a result, validation is required to authenticate the user. Because JavaScript allows form validation on the client side, data processing is faster than server-side validation.15 JavaScript form validation is preferred by the majority of web developers. We can validate name, password, email, date, cell numbers, and other data using JavaScript. https://chat-to.dev/post?id=12 #javascript #web #programmer

5
3
submitted 8 months ago by BillTheTailor@lemmy.ml to c/javascript@lemmy.ml

ChatGPT used to have this, and there was a popular forum that had it (though I can't remember what it was/is), where, when you'd click a "delete" link, the confirmation was RIGHT THERE: "delete" faded out, "OK / Cancel" faded in. In the same space. It was really elegant and unobtrusive.

Does anyone know if there's a library out there for it? I searched over github and google, but didn't find anything, probably because I couldn't get the search terms specific enough.

6
1
7
1
8
2
submitted 11 months ago* (last edited 11 months ago) by CheatCodeSam@lemmy.world to c/javascript@lemmy.ml

Hoping to get the ball rolling on this sub more. There’s too much action in the JavaScript scene for this community to only get a few post a month.

I’ll start with a classic, What the Heck is the Event Loop Anyway?

9
1
submitted 1 year ago* (last edited 1 year ago) by muhamadgaos@mastodon.social to c/javascript@lemmy.ml

Guys I need your help, where exactly the road map of Javascript Learning

@javascript

10
1
submitted 1 year ago by cons@lemmy.ml to c/javascript@lemmy.ml
11
1
submitted 1 year ago by gkd@programming.dev to c/javascript@lemmy.ml
12
1
submitted 1 year ago by zexu@lemmy.ml to c/javascript@lemmy.ml
13
1
Announcing TypeScript 5.0 Beta (devblogs.microsoft.com)
submitted 1 year ago by 0x1C3B00DA@lemmy.ml to c/javascript@lemmy.ml
14
1
submitted 1 year ago by 0x1C3B00DA@lemmy.ml to c/javascript@lemmy.ml
15
1
submitted 1 year ago by 0x1C3B00DA@lemmy.ml to c/javascript@lemmy.ml
16
1
submitted 1 year ago* (last edited 1 year ago) by cypherpunks@lemmy.ml to c/javascript@lemmy.ml

I'm writing some javascript (for the web) for the first time in a long time and I am realizing that I would be well served by using a bit of tooling like eslint and standardjs.

I am reluctantly willing to apt install nodejs but I am not willing to use npm because of my impression that it is a fractal of yolo curl | bash philosophy which will randomly install and automatically run malware or indistinguishable-from-malware garbage I don't want.

So, my question is: how can I install things like standardjs without using npm?

Please do not tell me that I should just use npm.

17
1
submitted 1 year ago by 0x1C3B00DA@lemmy.ml to c/javascript@lemmy.ml
18
1
submitted 1 year ago by 0x1C3B00DA@lemmy.ml to c/javascript@lemmy.ml
19
1
submitted 1 year ago by 0x1C3B00DA@lemmy.ml to c/javascript@lemmy.ml
20
1
submitted 1 year ago by 0x1C3B00DA@lemmy.ml to c/javascript@lemmy.ml
21
1
submitted 2 years ago by castarco@lemmy.ml to c/javascript@lemmy.ml

An introductory article on how to leverage some basic mathematical tricks and widely available browser APIs to generate beautiful animations.

22
1
submitted 2 years ago by miguel@lemmy.ml to c/javascript@lemmy.ml
23
1
24
1
submitted 2 years ago by cypherpunks@lemmy.ml to c/javascript@lemmy.ml
25
1
submitted 2 years ago by zexu@lemmy.ml to c/javascript@lemmy.ml
view more: next ›

JavaScript community

829 readers
1 users here now

A community about JavaScript, the ECMAScript standard, and programs that make use of JS such as Node.js.

founded 4 years ago
MODERATORS