How to use readConfigs method in Jest

Best JavaScript code snippet using jest

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

validRequest.js

Source: validRequest.js Github

copy
1const readConfigs = require("../configs/readConfigs");
2const ReadConfigs = new readConfigs();
3ReadConfigs.getAllServicesPingEndpoints()
4const validateRequest = (req, res, next) => {
5    const url = req.path;
6    const method = req.method;
7    const contextRoot = "/" + url.split("/")[1];
8    const path = url.replace(contextRoot, "");
9    const destinationService = ReadConfigs.getDestinationService(contextRoot, path, method);
10    if(!destinationService.isValid) {
11        return res.status(400).send({
12                    error: 'Your request is not valid !!'
13                })
14    }
15    req.destinationService = destinationService
16    req.destinationPath = path
17    req.destinationMethod = method
18    req.destinationHeaders = req.headers
19    req.destinationBody = req.body
20    req.contextRoot = contextRoot
21    next()
22}
23
24module.exports = validateRequest
Full Screen

frontend.js

Source: frontend.js Github

copy
1/* eslint-disable no-alert */
2/* eslint-disable no-unused-vars */
3
4const
5  inputDown = document.getElementById('down_delay');
6const inputRele = document.getElementById('release_delay');
7const inputHotk = document.getElementById('hotkey');
8const inputCores = document.getElementById('useCores');
9
10const oneThread = document.getElementById('one');
11const twoThread = document.getElementById('two');
12const customThread = document.getElementById('custom');
13const saveButton = document.getElementById('btn_save');
14const btnMacro = document.getElementById('btn_macro');
15
16const radioGodMode = document.getElementById('god_mode');
17const BTN_NAME = ['Saved!', 'Save configs'];
18// BUTTON STATES
19const BTN_NORMAL = 1;
20const BTN_CHANGED = 0;
21
22window.onload = function loadSavedUserConfigs() {
23  // load configs
24  window.readConfigs = JSON.parse(window.config.load());
25  inputDown.value = window.readConfigs.hold_delay;
26  inputRele.value = window.readConfigs.release_delay;
27  inputHotk.value = window.readConfigs.hotkey.toUpperCase();
28  radioGodMode.checked = window.readConfigs.god_mode;
29  const checkRatio = (elem) => elem.setAttribute('checked', 'checked');
30  switch (window.readConfigs.cores_to_use) {
31    case 1: checkRatio(oneThread); break;
32    case 2: checkRatio(twoThread); break;
33    default:
34      checkRatio(customThread);
35      inputCores.value = window.readConfigs.cores_to_use;
36      break;
37  }
38};
39
40// save configs
41function saveCnfs() {
42  let nCoresToUse = 1;
43
44  // verify processor cores, if custom is marked
45  if (customThread.checked) {
46    try {
47      nCoresToUse = parseInt(inputCores.value, 10);
48      const avaliableCores = window.cpu.getCores();
49      const cpuModel = window.cpu.getName();
50
51      if (nCoresToUse > avaliableCores) {
52        alert(`Sir, your ${cpuModel} has only ${avaliableCores} cores.`);
53        return;
54      }
55    } catch (err) {
56      alert('Use integer values only in custom cores.');
57    }
58  } else if (twoThread.checked) nCoresToUse = 2;
59
60  // verify all inputs
61  if (
62    inputDown.value === '' || inputRele.value === '' || inputHotk.value === ''
63  ) {
64    alert('Fill all inputs!');
65  } else {
66    saveButton.innerHTML = 'Saving...';
67    const objectConfig = {
68
69      hold_delay: inputDown.value,
70      release_delay: inputRele.value,
71      hotkey: inputHotk.value.toLowerCase(),
72      cores_to_use: nCoresToUse,
73      god_mode: radioGodMode.checked,
74    };
75
76    window.config.save(objectConfig);
77    setTimeout(() => {
78      saveButton.innerHTML = 'Saved!';
79      alert('Restart app to apply new configs!');
80    }, 3);
81  }
82}
83
84function changeMacroState() {
85  const arrayInput = document.querySelectorAll('input');
86  const enable = (btnMacro.textContent === 'START');
87
88  if (window.cpu.getCores() >= inputCores.value) {
89    btnMacro.innerHTML = (enable) ? 'STOP' : 'START';
90    arrayInput.forEach((input) => {
91      const inputVar = input;
92      inputVar.disabled = enable;
93      inputVar.contentEditable = !enable;
94    });
95
96    window.macro.changeState();
97  } else {
98    alert(`Select a number of cores less or equal than ${window.cpu.getCores()}`);
99  }
100}
101
102// LAYOUT FUNCTIONS
103function changeState() {
104  if (saveButton.textContent === BTN_NAME[BTN_CHANGED]) {
105    saveButton.innerText = BTN_NAME[BTN_NORMAL];
106  }
107}
108
109function toUpper() {
110  inputHotk.value = inputHotk.value.toUpperCase();
111}
112
Full Screen

configs.js

Source: configs.js Github

copy
1var eson = require('eson');
2var path = require('path');
3var env = process.env.NODE_ENV;
4
5function readConfigs (filename) {
6  return eson()
7    .read(path.join(__dirname, '/configs/', filename+'.json'));
8}
9module.exports = readConfigs(env);
10module.exports.readConfigs = readConfigs;
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)