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 5 years ago
MODERATORS
1
2
3
4
 
 

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)
5
2
submitted 8 months ago* (last edited 8 months ago) by [email protected] to c/[email protected]
 
 

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?

6
 
 

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

7
 
 

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.

8
9
10
2
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 
 

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?

11
 
 

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

@javascript

12
13
14
15
16
17
18
1
submitted 2 years ago* (last edited 2 years ago) by [email protected] to c/[email protected]
 
 

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.

19
20
21
22
23
 
 

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

24
25
view more: next ›