Reputation: 1340
I have a Node.js/Express project, and now I want to test one single file(one function) of it.
For now, the only way I know to do this is to call this function in the index.js
file, and after issuing npm run dev
all functions in the file will be run.
However, now I only want to test one single function, so I think that approach is not suitable.
So could anyone teach me how to run a single file/function in Node.js?
Thanks. The function/file I want to run is as below, which is a Reddit scraper.
const snoowrap = require("snoowrap");
async function redditCrawler(companyName, numOfMentionsToObtain) {
const scraper = new snoowrap({
userAgent: process.env.Reddit_userAgent,
clientId: process.env.Reddit_clientId,
clientSecret: process.env.Reddit_clientSecret,
refreshToken: process.env.Reddit_refreshToken,
});
const subreddit = await scraper.getSubreddit("all");
const topPosts = await subreddit.getHot({
time: "all",
limit: numOfMentionsToObtain,
});
let data = [];
topPosts.forEach((post) => {
// If a post doesn't have any content, just ignore it.
if (post.title.includes(companyName) && post.selftext) {
// If a post doesn't have a thumbnail picture, just use Reddit's logo.
if (post.thumbnail.substr(0, 8) !== "https://") {
post.thumbnail = "/imgs/reddit_icon.png";
}
data.push({
image: post.thumbnail,
title: post.title,
popularity: post.score,
content: post.selftext,
date: post.created, // In Unix time format(in milisecond).
platform: "Reddit",
});
}
});
return data;
}
module.exports = redditCrawler;
Upvotes: 2
Views: 2432
Reputation: 660
You can just run it from node directly via node yourFile.js
You would just need to actually call the function though so you'd want to stick redditCrawler();
at the bottom of your file and potentially remove module.exports
Upvotes: 2