Showing the state of variables when jest finds an error - jestjs

I'm doing a tutorial on jest (link) and I wrote a sum function and tested it.
function sum(a, b) {
return a + b;
}
Is it possible to show the state of variables when jest finds an error?
To give you an example
Now you get this but not the state of variables:
But I would like to see:
error when:
i=0 or j=0

When testing result of given function, it's better to run multiple tests with one expect instead of one test with multiple checks. To do so, I would recommend you checking out jest-each library. Then you can run multiple tests with one expect. jest-each gives you also possibility to insert tested arguments in the name of the test, so to wrapping this up, with jest-each you could write something like that:
each`
a | b | expected
${1} | ${1} | ${2}
${6} | ${9} | ${15}
${4} | ${2} | ${6}
`
.it('adding $a to $b should give $expected', ({ a, b, expected }) => {
expect(sum(a, b)).toBe(expected)
})
I'm not sure but I guess if you go deep down in documentation you will find also a way to generate dynamically arguments to pass it to test.

Related

Pass output logs from a program into a function and store the return code in a variable at the same time

I have a shell script which has a function to Log statements. SomeProgram is another program which is run from my shell script and the logs from it are passed into the function LogToFile.
#!/bin/sh
LogToFile() {
[[ ! -t 0 ]] && while read line; do echo "$line" >> $MY_LOG_FILE; done
for arg; do echo "$arg" >> $MY_LOG_FILE; done
}
SomeProgram | LogToFile
Question:
All is good until here. But I have been trying to get the return code from SomeProgram and store it in a variable. How can I do that without loosing the functionality of logs from SomeProgram going into my LogToFile function. I tried the following options but in vain.
RETVAL=SomeProgram | LogToFile
RETVAL=(SomeProgram) | LogToFile
RETVAL=(SomeProgram | LogToFile)
Is it possible to pass the output of a program to a function parameter and collect the return value in another variable at the same time?
I figured it out eventually. PIPESTATUS is the tool to use here.
Following is the way I can use it to get the return code of SomeProgram into RETVAL for example.
SomeProgram | LogToFile
RETVAL=${PIPESTATUS[0]}
Above is the way of getting the output of the program on the left of the pipe. PIPESTATUS is an array which contains the return codes of all the programs run adjacent to the pipe commands.
PIPESTATUS[1] could give the output of the LogToFile for example if LogToFile was a program.

regarding functions in bash

I have a general doubt regarding how to works functions in bash, let me explain, I have the next script called testscript.sh and it contains the following :
#!/bin/bash
export var="some text"
function clock {
# some arguments
echo "the variable var is [$var]"
}
$#
So when i run the script in the next way :
.
/testscript.sh clock
The value of "var" is empty unless i put inside of the function, so the question here is : is there any method to call the individually functions as I'm trying to do and all the variables outside of them i can call them inside the functions ?
thanks
I beg to differ, as per the following transcript:
pax> cat testscript.sh
#!/bin/bash
export var="some text"
function clock {
# some arguments
echo "the variable var is [$var]"
}
$#
pax> ./testscript.sh clock
the variable var is [some text]
As you can see there, the variable is very much set to the expected value. Hence, if it's coming out blank, you have an issue not related to the code you've shown us.

How to use the value of a string in bash as a variable name

Hopefully I can make it clear. I would like to create a filename out of different strings in bash. For example hmd.sh so h, m, d are different values (number 0..9 or letter aA..zZ). So for example I want to convert
h=1 m=11 and d=12 to 1aA.sh. h=> 1, m=>a and d=>A
To declare variables like
a01=1; a02=2 .. a09=9, a10=0; a11=a; a12=b and so on. h(1)=a01=1 m(11)=a11=a
and
d(12)=a12=A.
To test it I wrote this:
#!/bin/bash
dd01="1"
aa="01"
bb="dd$aa"
echo $bb
But of course $bb is dd01 and not its value. How can I make $bb its value of 1?
Associative arrays make this kind of thing much more readable.
However your answer is "variable indirection"
$ echo $bb
dd01
$ echo ${!bb}
1
Do not listen to any advice suggesting eval -- you open yourself up to all kinds of code injection.
The only way to expand a variable inside another is in an array when the variable is enclosed in the key's brackets like [$var].
You could store your values in an associative array, and reference them like so:
declare -A arr
arr[dd01]="1"
arr[aa]="01"
arr[bb]="dd${arr[aa]}"
echo ${arr[${arr[bb]}]}
Using arrays like this may be more convoluted for this example than referencing the variable name using ${!bb} syntax, but if you need to do this while keeping different sets of variables that may need to reference each other, creating an associative array may make more organizational sense.
I rewrote your code example as follows which gives you the value of 1 which is what you are trying to achieve.
#!/bin/bash
dd01="1"
aa="01"
bb="dd$aa"
echo $[$bb]
This did the trick:
#!/bin/bash
dd=1234567890aAbBcC
aa="11"
echo ${dd:(aa-1):1}
Appearantly 1 is the 0 position and aa can also be like 01 and still work!
Thank for all the advices.
I found my answer here:
http://tldp.org/LDP/abs/html/string-manipulation.html
This should do the job:
#!/bin/bash
dd01="1"
aa="01"
bb="dd$aa"
eval echo \$$bb
You can use the '$' operator to access the value of a variable. For example...
d = 'Hi'
e = ' there '
f = 'friend.'
foo = '$d$e$f'
This would cause the value of foo to be 'Hi there friend.'
Hope this helps.
Use eval:
#!/bin/bash
dd01="1"
aa="01"
eval bb=\$dd$aa
echo $bb
This script outputs the expected 1.

Shell Script: Is there a difference between "local foo" and "local foo="?

I found following code in /etc/init.d/functions on CentOS.
status() {
local base pid lock_file= pid_file=
...
4 variables are declared.
Two of them are not initialized, base and pid.
But rest of them are initialized with empty value, lock_file and pid_file.
I tested following code and found no differences.
local a b=
echo "a is $a, length is ${#a}"
echo "b is $b, length is ${#b}"
Is there any differences between them?
Yes, there is a difference. Consider the following function:
x() {
local a b=
echo ${a-X}
echo ${b-X}
}
Calling this function in bash-4.x results in this output:
$ x
X
$
The ${parameter−word} parameter expansion expands to the expansion of word (in this case X) if the parameter is unset, or to the parameter value if it is set.
From the example output, it is obvious that local a leaves the variable a unset, while local b= explicitly sets it to the empty (null) string.
EDIT:
On the other hand, on bash-3.x you get this:
$ x
$
A call to set within the function verifies that local a in bash-3.x initializes that variable to the empty string. This, however, seems to have been a bug. From the bash changelog:
This document details the changes between this version, bash-4.0-beta,
and the previous version, bash-4.0-alpha.
...
e. Fixed a bug that caused local variables to be created with the empty
string for a value rather than no value.

Get a list of function names in a shell script [duplicate]

This question already has answers here:
How do I list the functions defined in my shell? [duplicate]
(8 answers)
Closed 4 years ago.
I have a Bourne Shell script that has several functions in it, and allows to be called in the following way:
my.sh <func_name> <param1> <param2>
Inside, func_name() will be called with param1 and param2.
I want to create a help function that would just list all available functions, even without parameters.
The question: how do I get a list of all function names in a script from inside the script?
I'd like to avoid having to parse it and look for function patterns. Too easy to get wrong.
Update: the code. Wanted my help() function be like main() - a function added to the code is added to the help automatically.
#!/bin/sh
# must work with "set -e"
foo ()
{
echo foo: -$1-$2-$3-
return 0
}
# only runs if there are parameters
# exits
main ()
{
local cmd="$1"
shift
local rc=0
$cmd "$#" || rc=$?
exit $rc
}
if [[ "$*" ]]
then
main "$#"
die "how did we get here?"
fi
You can get a list of functions in your script by using the grep command on your own script. In order for this approach to work, you will need to structure your functions a certain way so grep can find them. Here is a sample:
$ cat my.sh
#!/bin/sh
function func1() # Short description
{
echo func1 parameters: $1 $2
}
function func2() # Short description
{
echo func2 parameters: $1 $2
}
function help() # Show a list of functions
{
grep "^function" $0
}
if [ "_$1" = "_" ]; then
help
else
"$#"
fi
Here is an interactive demo:
$ my.sh
function func1() # Short description
function func2() # Short description
function help() # Show a list of functions
$ my.sh help
function func1() # Short description
function func2() # Short description
function help() # Show a list of functions
$ my.sh func1 a b
func1 parameters: a b
$ my.sh func2 x y
func2 parameters: x y
If you have "private" function that you don't want to show up in the help, then omit the "function" part:
my_private_function()
{
# Do something
}
typeset -f returns the functions with their bodies, so a simple awk script is used to pluck out the function names
f1 () { :; }
f2 () { :; }
f3 () { :; }
f4 () { :; }
help () {
echo "functions available:"
typeset -f | awk '/ \(\) $/ && !/^main / {print $1}'
}
main () { help; }
main
This script outputs:
functions available:
f1
f2
f3
f4
help
You call this function with no
arguments and it spits out a
"whitespace" separated list of
function names only.
function script.functions () {
local fncs=`declare -F -p | cut -d " " -f 3`; # Get function list
echo $fncs; # not quoted here to create shell "argument list" of funcs.
}
To load the functions into an array:
declare MyVar=($(script.functions));
Of course, common sense dictates that
any functions that haven't been
sourced into the current file before
this is called will not show up in the
list.
To Make the list read-only and
available for export to other scripts
called by this script:
declare -rx MyVar=($(script.functions));
To print the entire list as newline
separated:
printf "%s\n" "${MyVar[#]}";
The best thing to do is make an array (you are using bash) that contains functions that you want to advertise and have your help function iterate over and print them.
Calling set alone will produce the functions, but in their entirety. You'd still have to parse that looking for things ending in () to get the proverbial symbols.
Its also probably saner to use something like getopt to turn --function-name into function_name with arguments. But, well, sane is relative and you have not posted code :)
Your other option is to create a loadable for bash (a fork of set) that accomplishes this. Honestly, I'd prefer going with parsing before writing a loadable for this task.

Resources