Cypress: Running Test using custom TZ value in Windows - node.js

I had my tests designed to be run in an environment which assumed to be in UTC.
I used cy.clock() to set a date-time in UTC format. The Web App to test will display current browser date-time in UTC format.
I had TZ=UTC in my environment variables and I run my test in Cypress Runner in Linux machine using TZ=UTC npx cypress open. The date and the time display on the web app is same as the date time I set using cy.clock()
However, the same method seems doesn't work in Windows. I couldn't use TZ=UTC npx cypress open in Windows. Instead I used npx cypress open. The time is correct but the date differs by one day. If I change my windows pc timezone to UTC, the both date and time is as expected.
Is there any other way other than change the computer timezone in Windows ? I do not need to do so in Linux
My tests runs fine in CI without the need for TZ before the script. Setting it in environment variable in cypress.json is sufficient.
Can someone please enlightened me on this please ?
My cypress.json
{
"projectId": "blahblahbah",
"baseUrl": "http://localhost:4200",
"numTestsKeptInMemory": 10,
"chromeWebSecurity": false,
"TZ": "UTC",
"env": {
"BASE_URL": "http://localhost:4200",
"TZ": "UTC",
},
"integrationFolder": "cypress/tests/",
"retries": {
"runMode": 2,
"openMode": 0
},
"reporter": "cypress-mochawesome-reporter",
"reporterOptions": {
"charts": true,
"reportPageTitle": "XXXX Test",
"embeddedScreenshots": true,
"inlineAssets": true,
"reportDir": "cypress/reports/",
"timestamp": "mmddyyyy_HHMMss"
},
"video": false
}

I think the Windows equiv to TZ=UTC npx cypress open is set TZ=UTC && npx cypress open.
There's also cross-env, allows a single script to work across different OS.
// package.json
{
"scripts": {
"cy:open": "cross-env TZ=UTC npx cypress open"
}
}
But it seems to be fussy about the shell
Windows Issues
Please note that npm uses cmd by default and that doesn't support command substitution, so if you want to leverage that, then you need to update your .npmrc to set the script-shell to powershell.
Another option is run-script-os
See Run different NPM scripts depending on the platform
// package.json
{
"scripts": {
"test": "run-script-os",
"test:win32": "set TZ=UTC && npx cypress open",
"test:darwin:linux": "TZ=UTC npx cypress open",
}
}

I found a workaround in Windows other than setting your computer clock.
You can use WSL2 to run Linux command in Windows 11. If you do not have Windows 11, update it. If your computer cannot support Windows 11, buy a new one.
Make sure you are in Windows 11 Build 22000
Install Docker. Followed the instruction to get Docker running successfully. That also includes setting up WSL2. In this step you will be directed to Microsoft guide to install WSL 2 and install a Linux distro from Microsoft Store. Make sure you did RESTART
Install driver for vGPU
Intel GPU driver for WSL
AMD GPU driver for WSL
NVIDIA GPU driver for WSL
Links can be obtained here: https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps
After that Open Powershell as Admin
wsl --update followed by wsl --shutdown
RESTART COMPUTER
Follow the below sub-steps if you wish to try to luck or increase chances of success.
Can do this for non Windows 11 PC
a. Install VcXsrv
(Multiple windows and Start no client is recommended)
On the page that lets you enable extra settings, disable access control
b. In VSCode, open project folder and a new terminal using dropdown. (There is a + button) Look for Ubuntu (WSL). Here you have access to the bash shell.
c. Type the following command in sequence
set DISPLAY variable to the IP automatically assigned to WSL2
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
echo $DISPLAY
# expect something like 192.168.64.1:0.0
sudo /etc/init.d/dbus start &> /dev/null
sudo visudo -f /etc/sudoers.d/dbus
After this command, an editor will launch. Add the following line with your distro username.
<your_username> ALL = (root) NOPASSWD: /etc/init.d/dbus
Remember to set permission in firewall to allow VcXsrv on domain, private and public network.
RESTART COMPUTER
In VSCode, open project folder and a new terminal using dropdown. (There is a + button) Look for Ubuntu (WSL). Here you have access to the bash shell.
Install cypress dependencies by using
sudo apt-get install libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb
Install npm and nodejs by using
'''
sudo apt-get install npm nodejs
'''
Note: Prior to 3 and . It is assumed that you have already done apt-get update
npm i
Open another New Terminal ( Ubuntu WSL) to run npm serve
You can attempt to
npx cypress open
to see if it works well.
Remember npm serve, npm i and npx cypress open need to be in bash ( Ubuntu WSL)
If you are using Windows 11 Build 22000 and above, you can try to exit VcXsrv, and do npx cypress open again. I tried and it still work.
Reference:
https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps
https://shouv.medium.com/how-to-run-cypress-on-wsl2-989b83795fb6
ANOTHER KISS SOLUTION: USE GIT BASH
Just launch Git Bash from VSCODE followed by TZ=UTC npx cypress open

Related

Facing "yarn not found" error while using terminal every time in mac

I want to use 'yarn' package manager to manage packages in my react-native project. I have successfully installed yarn package manager in my Mac by this command "npm install --global yarn" and set the env path by this command export PATH=/Users/admin/.npm-global/bin:$PATH.
All good I am able to use yarn after that.
The problem is when I again try to open terminal later than again it shows the "bash: yarn: command not found" and I need to set env path.
So how I set path permanently in my Mac, so that later I don't need to set env path again and again.
Anyone please help me.
You need to add export PATH=/Users/admin/.npm-global/bin:$PATH to your .bashrc file, in your home directory:
cat 'export PATH="/Users/admin/.npm-global/bin:$PATH"' >> ~/.bashrc
The content of the .bashrc file is run each time bash is started. (except maybe for login shells. In the case of a login shell, I believe .profile is run instead)

npm start hangs, does not sync changes (create-react-app on WSL)

I am new to web dev and am trying to follow the React tutorial. I have followed it successfully in the past on my machine. I am using Ubuntu 20.04 in WSL.
When I run:
npx create-react-app helloworld
cd helloworld
npm start
I get:
helloworld3#0.1.0 start /mnt/d/ProgrammingProjects/helloworld
react-scripts start
And it hangs there for about 5 minutes before the localhost opens. Once the localhost is open, it does not sync changes when I edit and save the App.js file.
I have tried completely uninstalling and reinstalling node and npm.
I was also having the problem with hot reloading in WSL2 . I tried almost every solution present on github , stackoverflow and where not . Trying to fix it from
CHOKIDAR_USEPOLLING=true to setting FAST_REFRESH = false in .env to changing network setting using netsh and what not for 2 days . finally decided to revert to WSL1.
I think it's because WSL 2 is running as a hyper-v virtual machine.
If you're using WSL2 you should revert to WSL1 for now.
wsl --set-version Ubuntu-18.04 1
Run the above command in Powershell. And check version of WSL after using
wsl -l -v
if it's version 1 now you are good to go.

Git Bash Command on Windows, yarn command not found

I following this online tutorial on downloading and installing web files from GIT HUB. I got down to the topic: Starting Our Static Website, there I'm prompted to enter the commands to download and install the web files. However, when I enter the command yarn install, I get the following prompt: bash: yarn: command not found
How can this problem be resolved?
I'm using GIT Bash command prompt on Windows 10.
If you are using Mintty (Bash) then you simply do npm install -g yarn
You probably just did not install yarn yet.
Have a look at the yarn installation for Windows here:
https://yarnpkg.com/lang/en/docs/install/
The official Yarn setup does not tell you you need to restart your computer, but you actually have to before you can use it from your shell. So, install it from
https://yarnpkg.com/lang/en/docs/install/#windows-stable
then restart your PC and you should be able to run yarn from your console.
Working in Visual Studio Code?
Open a new terminal
Click "Terminal" → "New Terminal"
or
Simply use ctrl+shift+` (backtick)
(this backtick button is on the left of the 1 key and below Esc)
Once the Terminal is active, install Yarn by running npm install -g yarn.
Verify the installation was successful by running yarn --version.
I followed the official yarn installation for windows but is loading a page no found on github
https://github.com/yarnpkg/yarn/releases/download/v1.22.17/yarn-1.22.17.msi
so I had to the manual way which is adding the path into the environment variables
asuming you already have installed npm, if you are not sure type in the terminal npm -v
install yarn on the terminal using npm install --global yarn
check the yarn packages has been installed on
C:\Users\yourusername\AppData\Roaming\npm\node_modules\yarn
if you can see the package on your node_modules you just need to add the bin folder that is inside the yarn into your environment variables
On the Windows taskbar, right-click the Windows icon and select System.
In the Settings window, under Related Settings, click Advanced system settings.
On the Advanced tab, click Environment Variables.
Click on Edit and add C:\Users\yourusername\AppData\Roaming\npm\node_modules\yarn
Press ok save open a new terminal and try running yarn -v
What resolved this for me was repairing my Node install:
Go to Add or remove programs
Search for "Node.js"
Choose Modify
Choose Next and then Repair
After doing that I was able to run yarn without issue.
If you install yarn through MSI on windows10, and the software installed on C:\Program Files (x86)\Yarn\bin, but the path set in C:\Users\{username}\AppData\Local\Yarn\.bin, so change the path(both user and system) to C:\Program Files (x86)\Yarn\bin.
Try to type yarn.cmd on git bash, it should work fine.
for example: yarn.cmd --version.
Please check first that yarn is install or not globally
C:\Users\username\AppData\Roaming\npm\node_modules\yarn
If there is, then add this to your environment variable
C:\Users\username\AppData\Roaming\npm\node_modules\yarn\bin
Download and install yarn wizard first in order to run commands related to yarn.Click on the below link to start the download automatically:
https://yarnpkg.com/latest.msi
I could reproduce the same issue on git bash. However when i tried the same in command prompt it worked!
on bash - $ yarn add axios
bash: yarn: command not found
on command prompt -
yarn add v1.22.5
....
[1/4] Resolving packages...
It looks like there might be an issue with Yarn and certain environments. See this GitHub issue for more details.
I had same issue and for me install yarn by the installer
From the yarn official website Yarn Official website
Read through this if nothing helps:
https://bobbyhadz.com/blog/not-recognized-as-internal-or-external-command-yarn
for me i had to add environment variables as npm did not install in the "correct"/standard folder
On Windows, the output of the npm config get prefix command will look something like: C:\Users\Your_User_Name\AppData\Roaming\npm.
To update the PATH on a Windows machine, you have to:
Open the start search and type in env and then click "Edit the system environment variables"
Then click "Environment Variables"
Edit the Path variable and add the output you got from the npm config get prefix command.
The path should look like C:\Users\Your_User_Name\AppData\Roaming\npm (make sure to replace the Your_User_name placeholder with your actual username).
even tho I installed npm and yarn corect, when I check the version, the same error already appears:
393330#785-5949 MINGW64 /c/Users/393330/Desktop/intranet-app
$ npm install --global yarn
changed 1 package in 344ms
393330#785-5949 MINGW64 /c/Users/393330/Desktop/intranet-app
$ yarn --version
bash: yarn: command not foundstrong text
If you don't have npm, you can do the following (taken from yarn installation instructions):
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
Use this command on macbook
brew install yarn

How to run rm command on windows 10?

"build": "rm -rf ./build && mkdir ./build && ./node_modules/.bin/babel -d ./build ./src"
This is the command in package.json and it gives me an error saying:
rm is not recognized as internal or external command.
Invalid switch /build
That script was written for the UNIX shell, which does not work on windows. The correct way to do this in a cross-platform way is to use rimraf & mkdirp.
Also, the ./node_modules/.bin/babel portion could be shortened to simply babel (./node_modules/.bin/babel doesn't work on windows IIRC).
Properly written, the script should be:
"build": "rimraf ./build && mkdirp ./build && babel -d ./build ./src"
For this script to work, you will have to install rimraf and mkdirp. You can do this by running:
npm install --save-dev rimraf mkdirp
The --save-dev flag will add rimraf and mkdirp to your package.json's devDependencies section so that they will automatically be installed with future npm installs.
Use rd /s /q "folder name" instead of rm -rf "folder name"
In order to run bash commands on Windows you need to install Bash complied for Windows. Install Cygwin and add bin directory to you PATH variable.
Windows 10 does not provide a UNIX shell by default. You'll need the appropriate UNIX utilities (such as rm) and a shell that supports the syntax you specified.
You have a few options:
Use the Windows 10 Bash Shell - Recent versions of Windows 10 now provide beta support for running Ubuntu within Windows without requiring a virtual machine.
Use Cygwin for development - Cygwin provides a shell of your choice and plenty of UNIX / Linux utilities.
Run a Virtual Machine with a Linux Guest - There are many options for running a VM on Windows. You can use Hyper-V, VirtualBox, or VMware Player. For a guest operating system, Ubuntu is a popular choice, but Fedora and Debian are also common alternatives.
Not to Bash, but in Windows you can use the built-in remove directory (rd) command:
RD /S /Q "folder-name"
Follow this link:
https://learn.microsoft.com/en-us/windows/nodejs/setup-on-wsl2
You will install a linux client for windows, and then instal node.js for that client and you can run rm commands easily

Unable to use "node" command via SSH

I have set up a Ubuntu 14.04 server in Virtual box and installed with node.js.
I could run the command "node server.js" normally in the virtual box.
However, when I use my windows 10 command prompt to connect the server by "ssh user#192.168.x.x" and run "node server.js", it shows up:
The program 'node' can be found in the following packages:
* node
* nodejs-legacy
Ask your administrator to install one of them
What should I do?
Most likely due to the way you installed node, it's not in your PATH.
This can be due to logging in with a different user than the one you used to install ( that has different path settings ), or it can be due to how the shell is configured w.r.t. login and non-login shells which in essence govern what configuration scripts are sourced and thus what is the value of $PATH.
You could try to run running with:
echo $PATH
Both directly and trough ssh to compare the configuration.
Try the following:
nodejs server.js

Resources