eslint: no return assign - eslint

So I'm having a problem with a no return assign but my script seems good can someone help me with it?
The script is:
(function() {
'use strict';
let root = document.querySelector(":root");
root.style.setProperty("--tv-color-pane-background", "#000");
setTimeout(() => document.querySelector("html.theme-dark .tv-floating-toolbar").style.backgroundColor = "black", 1000);
})();
on the settimeout line It's giving me this error:enter image description here

Related

Jest in Node.js. How to test a function that executes shell commands

I have not been able to test this function with Jest, I would appreciate your help. Thank you.
/**
* #function now
* #description When a Bash script is executed, it instantly displays the responses that appear on the screen.
* #param {string} script Bash script
* #example now('echo "Hello World!"')
*/
function now(script) {
let execute = exec(script)
execute.stdout.on('data', (data) => {
if (data.charAt(data.length - 1) === '\n') {
console.log(data.toString().slice(0, -1))
} else {
console.log(data.toString())
}
})
}
Since exec executes asynchronously, you'll need to change the now function a bit in order to test it. As it's written, you don't have a way for the caller to know when now is finished. The simplest ways to change this are:
Make it return a Promise that resolves when it's done
Make it take a callback that is called when it's done
Promise
function now(script) {
return new Promise((resolve, reject) => {
let execute = exec(script)
execute.stdout.on('data', (data) => {
if (data.charAt(data.length - 1) === '\n') {
console.log(data.toString().slice(0, -1))
} else {
console.log(data.toString())
}
})
// Resolve the Promise when the shell exits
execute.on('exit', resolve);
// Error handling omitted for brevity, you can call reject() on failure
});
}
Here's the test. There are various ways to check that console.log is called. This one uses a spy but you could also inject a log function and default it to console.log.
it('logs from shell command', async () => {
jest.spyOn(console, "log");
await greet('echo "foo"');
expect(console.log).toBeCalledWith('foo');
});
Callback
function now(script, callback) {
let execute = exec(script)
execute.stdout.on('data', (data) => {
if (data.charAt(data.length - 1) === '\n') {
console.log(data.toString().slice(0, -1))
} else {
console.log(data.toString())
}
})
// Resolve the Promise when the shell exits
execute.on('exit', callback);
// Error handling omitted for brevity, you can pass an error to the callback on failure
}
The expect needs to be in the callback so the test waits until the shell has exited. Note that you need to call done for the test to finish. Promises are generally more ergonomic, but callbacks have their uses as well so I included them both.
it('logs from shell command', (done) => {
jest.spyOn(console, "log");
greet('echo "foo"', () => {
expect(console.log).toBeCalledWith('foo');
done()
});
});
Using information from #helloitsjoe I got what I was looking for.
Modify the promise so that it correctly displays the messages on the screen.
function now(script) {
return new Promise((resolve, reject) => {
let execute = exec(script)
execute.stdout.on('data', (data) => {
console.log(data.toString().slice(0, -1))
process.stdout.cursorTo(0)
})
execute.on('exit', resolve);
})
}
I use the test with the promise of #helloitsjoe, for it to work I need to install npm i -D regenerator-runtime and import it with import 'regenerator-runtime/runtime'.
Thank you very much #helloitsjoe.

chai-as-promised's eventually passes wrong rejection

suddenly I realized that in many of my tests it wrongly passes the fail test, I tried to understand the reason, here's is an example that passes the test which is wrong
describe('...', () => {
it('...', () => {
return chai.expect(Promise.reject('boom')).to.eventually.not.rejected;
});
});
can anyone help me to figure out what am doing wrong? thanks
to.not.eventually is not the same as to.eventually.not
You’re using not, it should be before the eventually
So change your test to below code to use to.not.eventually to see it won’t get passed and it’ll fail
const { describe, it } = require('mocha');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
const { expect } = chai;
describe('...', () => {
it('...', () => {
return expect(Promise.reject(new Error('boom'))).to.not.eventually.rejected;
});
});

node util promisify working example please

I am trying to understand promises. Can someone point out what is wrong with this code? What needs to be added or edited to return the value passed?
const util = require('util')
const echo = (word) => {
return word
}
let echoPromise = util.promisify(echo)
let promise = echoPromise()
promise("promise").then((data) => { // tried promise.then(...)
console.log(data) // echo's 'promise' ?
})
.catch((err) => {
console.error
})
console.log(echo('word')) // echos word

NodeJS Async/Await Exporting a Variable

Me again busting b***s ...
I'm sorry to have to come back to you guys, but I find the information available online very confusing and can't seem to find an appropriate answer to my problem.
So if one of you wizards/gods of the Node could help me, I'd greatly appreciate it.
I'm trying to export a variable that yields from a promise to a different module.
Here's my code:
The main:
//app.js <--- This is where I need the variable exported.
var sp1 = require('./module');
var somePromise2 = new Promise((resolve, reject) => {
resolve('Hey! It worked the second time!');
});
async function exec() {
const message1 = await sp1.msg
const message2 = await somePromise2
console.log('Success', message1, message2);
}
exec()
and the module with the promise:
//module.js
var somePromise1 = new Promise((resolve, reject) => {
var msg = '';
resolve(msg = 'Hey! It worked!');
});
module.exports = {
somePromise1,
}
As you can see the somePromise1, is actually the same as somePromise2 but in a different module. Thing is, I apparently can't seem to get the msg variable to be exported, it yields an undefined (if I do everything locally: in the same file, it works seemlessly).
Thanks in advance for your help and sorry in advance if you find this is a duplicate of an existing question...
I'crawled SO since yesterday for an answer and moved the code around but nothing seems to apply...
You have an error in importing and a mistake in using the promise:
//app.js <--- This is where I need the variable exported.
var sp1 = require('./module').somePromise1;
var somePromise2 = new Promise((resolve, reject) => {
resolve('Hey! It worked the second time!');
});
async function exec() {
const message1 = await sp1;
const message2 = await somePromise2;
console.log('Success', message1, message2);
}
exec()

Nodejs async.series not executing all the methods

I am trying to use 'async' for my work, so I have written a sample program to make sure that works. async.parallel() works as expected, but not the async.series(). Not sure what I am missing. Can anyone take a look at this sample code and point out the problem/mistake?
async.series([task1, task2]) is just executing 'task1' ONLY.
const async = require('async');
var firstThing = function() {
setTimeout(function(){console.log('IN the First thing')}, 1000);
};
var secondThing = function () {
setTimeout(function(){console.log('IN the second thing')}, 1500);
};
async.series(
[
firstThing,
secondThing
],
function (err, result) {
console.log('blah blah '+result);
});
when I run this code, I get
IN the First thing
and exits. Why is the second task not being called? what am I missing?
Thanks.
You have to call back when you finish each of the functions you want to run in series:
const async = require('async');
var firstThing = function(callback) {
setTimeout(function(){console.log('IN the First thing')}, 1000);
callback(/* pass error or callback*/);
};
var secondThing = function (callback) {
setTimeout(function(){console.log('IN the second thing')}, 1500);
callback(/* pass error or callback*/);
};

Resources