adaltas / adaltas/node-csv

Question: how to mock with jest

Open
#450 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
4.3k
Forks
299
Avg merge
16h 19m
Merged PRs (30d)
1

Description

Although this is for tracing bugs, since my question is specific to csv-parser I thought maybe it's the right place to ask. (hope you don't mind)

I have the following code to parse csv files:

const records = [];
const parser = fs
  .createReadStream(`${process.cwd()}/${file.destination}/${file.filename}`)
  .pipe(parse({}));

for await (const record of parser) {
  records.push(record);
}

I wanted to know how to mock this code in unit tests with jest. Any idea would be appreciated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named in the issue. Start with the shown createReadStream(...).pipe(parse({})) entry point and determine how the Jest unit test should isolate it; done means a repeatable unit test can supply mocked stream/parser behavior and verify the collected records.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
testing-qa
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.