i have a problem with my project, i need sharing the configurations parameters between modules, y have my entry point
app.js
const { requestConfiguracion } = require('./clases/servicios');
( async () => {
const dbConfig = await requestConfiguracion();
})();
servicios.js
const axios = require('axios').default;
const requestConfiguracion = async () => {
try {
const request = await axios.get('http://localhost/config/getConfig');
return request.data;
} catch (error) {
console.error(error)
}
}
module.exports = {
requestConfiguracion,
}
I need that the configuration in dbConfig is available for the other modules.
You can create a separated module to read and export that dbConfig.
// db-config.js
const { requestConfiguracion } = require('./clases/servicios');
let dbConfig = null;
module.exports.getDbConfig = async () => {
if (dbConfig) return dbConfig; // cache dbConfig for next time
dbConfig = await requestConfiguracion();
return dbConfig;
};
And in other modules:
// app.js
const { getDbConfig } = require('./db-config');
async function someFunction() {
const dbConfig = await getDbConfig();
// do something
}
Since your requestConfiguracion() function is an async function, you have to use await every time trying to get that config.
Related
The script by itself works great (entering the url manually, writing a json file using the fs module, node script_name.js) but within a Express get request it returns undefined.
So I've built a simple frontend to let the user enter the subreddit name to be scraped.
And here's where the problem is:
Express controller
const run = require("../run");
requestPosts: async (req, res) => {
try {
const { subreddit } = req.body;
const response = await run(subreddit);
//console.log(response);
res.json(response);
} catch (error) {
console.error(error);
}
},
Cheerio functions
const axios = require("axios");
const { load } = require("cheerio");
let posts = [];
async function getImage(postLink) {
const { data } = await axios(postLink);
const $ = load(data);
return $("a.post-link").attr("href");
}
async function run(url) {
try {
console.log(url);
const { data } = await axios(url);
const $ = load(data);
$(".thing.linkflair.link").map(async (i, e) => {
const title = $(e)
.find(".entry.unvoted .top-matter .title .title")
.text();
const user = $(e)
.find(".entry.unvoted .top-matter .tagline .author")
.text();
const profileLink = `https://old.reddit.com/user/${user}`;
const postLink = `https://old.reddit.com/${$(e).find("a").attr("href")}`;
// const thumbail = $(e).find("a img").attr("src");
const image = await getImage(postLink);
posts.push({
id: i + 1,
title,
postLink,
image,
user: { user, profileLink },
});
});
const nextPage = $(".next-button a").attr("href");
if (nextPage) {
await run(nextPage);
} else {
return posts;
}
} catch (error) {
console.error(error);
}
}
module.exports = run;
I've tried working with Promise((resolve, reject) => {}).
I think it's returning undefined because maybe the code its not synchronized.
(idk if it makes sense, i've just started programming)
.map() is not promise-aware and does not wait for your promises to finish. So, $(".thing.linkflair.link").map() finishes long before any of the asynchronous functions inside its callback do. Thus you try to return posts BEFORE it has been populated.
Passing an async callback to .map() will return an array of promises. You can use Promise.all() on those promises to know when they are done and once you're doing that, you may as well just return each post object rather that using a higher level scoped/shared object, thus making the code more self contained.
I would suggest this code:
async function run(url) {
try {
console.log(url);
const { data } = await axios(url);
const $ = load(data);
const posts = await Promise.all($(".thing.linkflair.link").map(async (i, e) => {
const title = $(e)
.find(".entry.unvoted .top-matter .title .title")
.text();
const user = $(e)
.find(".entry.unvoted .top-matter .tagline .author")
.text();
const profileLink = `https://old.reddit.com/user/${user}`;
const postLink = `https://old.reddit.com/${$(e).find("a").attr("href")}`;
// const thumbail = $(e).find("a img").attr("src");
const image = await getImage(postLink);
// return a post object
return {
id: i + 1,
title,
postLink,
image,
user: { user, profileLink },
};
}));
const nextPage = $(".next-button a").attr("href");
if (nextPage) {
const newPosts = await run(nextPage);
// add these posts to the ones we already have
posts.push(...newPosts);
}
return posts;
} catch (error) {
console.error(error);
}
}
I managed to get the data on #1, but I can't print it out in Express #2, how can I resolve the data and output it?
I believe this is my code error where I can't place the correct async/await.
Here's my code:
minify.js
const { getContent } = require('./get');
const { minifyJS } = require('./processing');
async function getMinify(req, res) {
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error.content);
}
}
module.exports = { getMinify }
get.js
const got = require('got');
async function getContent(remoteUrl) {
const code = await got(remoteUrl);
return code.body;
}
module.exports = { getContent }
processing.js
const { minify } = require('uglify-js');
async function minifyJS(data) {
const result = await minify(data);
return result.code;
}
module.exports = { minifyJS }
app.js
const express = require('express');
const { getMinify } = require('./minify');
const app = express();
app.get('/*', getMinify);
app.listen(5000);
based on uglify-js, result of minify(data) function has two object code and error.
and minify(data) don't need to await
processing.js should changed
const { minify } = require("uglify-js");
async function minifyJS(data) {
let { error, code } = minify(data);
if (error)
throw error;
return code;
}
module.exports = { minifyJS };
and for handling of two .js file (get.js and processing.js) changed
minify.js
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error); // remove .content
}
I am trying to write test using a class , but I am getting an error.
Here is the test:
import assert from 'assert'
const ethers = require('ethers');
const zksync = require('zksync');
const ZKSync = require('../../../../app/scripts/controllers/zksync');
describe('zkSync', function () {
let zkSync
before(async () => {
// zkSync = new ZKSync(new Proxy({}, ethers, zksync))
zkSync = new ZKSync(ethers, zksync);
})
describe('initAccount', function () {
it('registers an account on zksync', async () => {
const TEST_SEED = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium'
const ethersProvider = await new ZKSync.getEthereumProvider(ethers,'rinkeby')
const zkSyncProvider = await new ZKSync.getZkSyncProvider('testnet');
const aliceRinkebyWallet = new ethersProvider.Wallet.fromMnemonic(TEST_SEED);
const aliceZKsyncWallet = new ZKSync.initAccount(aliceRinkebyWallet,zkSyncProvider);
assert.strictEqual(await aliceZKsyncWallet.isSigningKeySet(), true, 'account is registered.')
})
})
})
Here is the code it calls:
const ethers = require('ethers')
const zksync = require('zksync')
export default class ZKSync {
constructor (ethers, zksync) {
// const initState = opts.initState || {}
// this.store = new ObservableStore(initState)
// this.keyringController = opts.keyringController
this.ethers = ethers
this.zksync = zksync
}
async getZkSyncProvider (zksync, networkName) {
let zkSyncProvider
try {
zkSyncProvider = await zksync.getDefaultProvider(networkName)
} catch (error) {
console.log('Unable to connect to zkSync.')
console.log(error)
}
return zkSyncProvider
}
async getEthereumProvider (ethers, networkName) {
let ethersProvider
try {
// eslint-disable-next-line new-cap
ethersProvider = new this.ethers.getDefaultProvider(networkName)
} catch (error) {
console.log('Could not connect to Rinkeby')
console.log(error)
}
return ethersProvider
}
async initAccount (rinkebyWallet, zkSyncProvider) {
const zkSyncWallet = await this.zksync.Wallet.fromEthSigner(rinkebyWallet, zkSyncProvider)
return zkSyncWallet
}
}
I run the tests with mocha test/unit/app/controllers/zksync-lib-test.js.
However , I get the following error:
TypeError: ZKSync is not a constructor
I will appreciate any pointers on this.
I am trying to mock the promise version of fs.writeFile using Jest, and the mocked function is not being called.
Function to be tested (createFile.js):
const { writeFile } = require("fs").promises;
const createNewFile = async () => {
await writeFile(`${__dirname}/newFile.txt`, "Test content");
};
module.exports = {
createNewFile,
};
Jest Test (createFile.test.js):
const fs = require("fs").promises;
const { createNewFile } = require("./createFile.js");
it("Calls writeFile", async () => {
const writeFileSpy = jest.spyOn(fs, "writeFile");
await createNewFile();
expect(writeFileSpy).toHaveBeenCalledTimes(1);
writeFileSpy.mockClear();
});
I know that writeFile is actually being called because I ran node -e "require(\"./createFile.js\").createNewFile()" and the file was created.
Dependency Versions
Node.js: 14.1.0
Jest: 26.6.3
-- Here is another attempt at the createFile.test.js file --
const fs = require("fs");
const { createNewFile } = require("./createFile.js");
it("Calls writeFile", async () => {
const writeFileMock = jest.fn();
jest.mock("fs", () => ({
promises: {
writeFile: writeFileMock,
},
}));
await createNewFile();
expect(writeFileMock).toHaveBeenCalledTimes(1);
});
This throws the following error:
ReferenceError: /Users/danlevy/Desktop/test/src/createFile.test.js: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: writeFileMock
Since writeFile is destructured at import time instead of being consistently referred as fs.promises.writeFile method, it cannot be affected with spyOn.
It should be mocked as any other module:
jest.mock("fs", () => ({
promises: {
writeFile: jest.fn().mockResolvedValue(),
readFile: jest.fn().mockResolvedValue(),
},
}));
const fs = require("fs");
...
await createNewFile();
expect(fs.promises.writeFile).toHaveBeenCalledTimes(1);
It make sense to mock fs scarcely because unmocked functions provide side effects and potentially have negative impact on test environment.
Mock "fs/promises" async functions in jest
Here is a simple example using fs.readdir(), but it would also apply to any of the other async fs/promises functions.
files.service.test.js
import fs from "fs/promises";
import FileService from "./files.service";
jest.mock("fs/promises");
describe("FileService", () => {
var fileService: FileService;
beforeEach(() => {
// Create a brand new FileService before running each test
fileService = new FileService();
// Reset mocks
jest.resetAllMocks();
});
describe("getJsonFiles", () => {
it("throws an error if reading the directory fails", async () => {
// Mock the rejection error
fs.readdir = jest.fn().mockRejectedValueOnce(new Error("mock error"));
// Call the function to get the promise
const promise = fileService.getJsonFiles({ folderPath: "mockPath", logActions: false });
expect(fs.readdir).toHaveBeenCalled();
await expect(promise).rejects.toEqual(new Error("mock error"));
});
it("returns an array of the .json file name strings in the test directory (and not any other files)", async () => {
const allPotentialFiles = ["non-json.txt", "test-json-1.json", "test-json-2.json"];
const onlyJsonFiles = ["test-json-1.json", "test-json-2.json"];
// Mock readdir to return all potential files from the dir
fs.readdir = jest.fn().mockResolvedValueOnce(allPotentialFiles);
// Get the promise
const promise = fileService.getJsonFiles({ folderPath: "mockPath", logActions: false });
expect(fs.readdir).toBeCalled();
await expect(promise).resolves.toEqual(onlyJsonFiles); // function should only return the json files
});
});
});
files.service.ts
import fs from "fs/promises";
export default class FileService {
constructor() {}
async getJsonFiles(args: FilesListArgs): Promise<string[]> {
const { folderPath, logActions } = args;
try {
// Get list of all files
const files = await fs.readdir(folderPath);
// Filter to only include JSON files
const jsonFiles = files.filter((file) => {
return file.includes(".json");
});
return jsonFiles;
} catch (e) {
throw e;
}
}
}
I know this is an old thread, but in my case, I wanted to handle different results from readFile (or writeFile in your case). So I used the solution Estus Flask suggested with the difference that I handle each implementation of readFile in each test, instead of using mockResolvedValue.
I'm also using typescript.
import { getFile } from './configFiles';
import fs from 'fs';
jest.mock('fs', () => {
return {
promises: {
readFile: jest.fn()
}
};
});
describe('getFile', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('should return results from file', async () => {
const mockReadFile = (fs.promises.readFile as jest.Mock).mockImplementation(async () =>
Promise.resolve(JSON.stringify('some-json-value'))
);
const res = await getFile('some-path');
expect(mockReadFile).toHaveBeenCalledWith('some-path', { encoding: 'utf-8' });
expect(res).toMatchObject('some-json-value');
});
it('should gracefully handle error', async () => {
const mockReadFile = (fs.promises.readFile as jest.Mock).mockImplementation(async () =>
Promise.reject(new Error('not found'))
);
const res = await getFile('some-path');
expect(mockReadFile).toHaveBeenCalledWith('some-path', { encoding: 'utf-8' });
expect(res).toMatchObject('whatever-your-fallback-is');
});
});
Note that I had to cast fs.promises.readFile as jest.Mock in order to make it work for TS.
Also, my configFiles.ts looks like this:
import { promises as fsPromises } from 'fs';
const readConfigFile = async (filePath: string) => {
const res = await fsPromises.readFile(filePath, { encoding: 'utf-8' });
return JSON.parse(res);
};
export const getFile = async (path: string): Promise<MyType[]> => {
try {
const fileName = 'some_config.json';
return readConfigFile(`${path}/${fileName}`);
} catch (e) {
// some fallback value
return [{}];
}
};
THe following code is my async code i want to import in my app.js file
module.exports = {
foo : async () =>{
const axios = require('axios')
const [, pairA, pairB ] = require('./pairs.json')
const { SERVER_URL } = require('./lib/sdk')
const fundAccounts = async (pairs) => await Promise.all(
pairs.map(
async (pair) => await axios.get('/friendbot', {
baseURL: SERVER_URL,
params: { addr: pair.publicKey }
})
)
)
fundAccounts([pairA, pairB])
.then(() => console.log('ok'))
.catch((e) => { console.error(e); throw e})
}
}
the following is my server.js file trying to import and check the output
var funding = require('./scripts/2_fundAccounts');
console.log(typeof(funding));
console.log(funding.foo())
// i AM GETTING undifined
Next i tried with set time out method
function calling() {
var funding = require('./scripts/2_fundAccounts');
funding.foo()
}
setTimeout(calling,4000)
Any suggestions how to import the code and make it work
Try this:
module.exports = async function foo () {
// etc etc...
}