Running Mocha tests in Webstorm - testing

I'm running Mocha tests for React in Webstorm's terminal window using the following NPM start command:
"tsc && mocha --compilers js:babel-core/register --require ./test_helper.js \"test/**/*.#(js|jsx)\" --watch --watch-extensions js,jsx,tsx"
How do I create a Webstorm Mocha configuration so that I can run tests in Webstorm's test window?
Thanks.

Add a configuration for Mocha:
Run -> Edit Configurations -> Press "+" -> Select "Mocha".
Fields of the form:
Node interpreter: your_node_executable_path
Working directory: root_path_of_your_app
Mocha package: mocha_path (normally within your node_modules folder).
Test directory: root_path_of_your_app \test
By default Mocha looks the tests inside test folder and there, it tries to run test.js.
All the Mocha commands (--compilers, --require, etc) can be included in mocha.opts. Mocha tries to read this file in \test\mocha.opts.
mocha.opts requires one command per line.

Related

What are the equivalent CI commands for Vue projects based on Vite instead of the Vue CLI?

I created a new Vue project via npm init vue#latest. I used the Vue CLI before and want to get into Vite now. Inside my Github action I used the Vue CLI service commands before but now I'm looking for the equivalent commands using Vite.
Check that the code style is fine (no errors, no warnings)
Inside my workflow I previously used the command
npm run lint -- --no-fix --max-warnings=0
Based on the lint script command
eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore
I added the lint:ci script command
eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --max-warnings 0 --ignore-path .gitignore
Check that unit tests are passing
Inside my workflow I previously used the command
npm run test:unit
Based on the test:unit script command
vitest --environment jsdom
I added the test:unit:ci script command
vitest --environment jsdom --run
Check that e2e tests are passing
Inside my workflow I previously used the command
npm run test:e2e -- --headless
There already is a test:e2e:ci script command and based on the Cypress docs I think I have to use it this way (which worked for me)
- name: Check if e2e tests are passing
uses: cypress-io/github-action#v2
with:
build: npm run build
start: npm run test:e2e:ci
Do you have any better solutions? I'm not sure if this is the most elegant way or if Vue/Vite already provide some commands I don't know about yet.
Thanks in advance!

Missing node_modules bin on PATH

I have run the command
yarn add -D jest to install jest to my project.
This does successfully add jest to my node_modules
> find . -name jest
./node_modules/.bin/jest
./node_modules/jest
When I use iterm2 to run jest however I get the following output
> jest
zsh: command not found: jest
FWIW When I use the IntelliJ terminal it does work
> jest
Determining test suites to run...^C
What am I missing in the iterm environment to be able to have node_modules bin in my classpath depending on the current repo?
An OS shell doesn't know about your locally installed node_modules, but IntelliJ terminal does. So if you want to run jest from outside of an IDE you should perform several additional steps.
The most common way to run locally installed packages is to define a separate script in the "scripts" section of your package.json file. Then you will be able to run it using the yarn/npm itself from any terminal. You can find an exact example in the Yarn docs.
{
"name": "my-package",
"scripts": {
"test": "jest"
}
}
yarn run test
Or you could install jest globally so it will be accessible from anywhere, but it's not a best practice.

When I run cypress as npm run cypress run build fails

I have installed cypress using npm as npm install cypress --save-dev .
I used the same command in .gitlab-ci.yml file
When i run the command npm run cypress run locally , IDE opens and when i double click the spec.js file , then the tests run.
But I i use the same command on the gitlab pipeline , it says
cypress open "run"
It looks like this is your first time using Cypress: 4.1.0
[07:45:16] Verifying Cypress can run /osmc/ux/framework-acceptance-tests/cache/Cypress/4.1.0/Cypress [started]
[07:45:18] Verifying Cypress can run /osmc/ux/framework-acceptance-tests/cache/Cypress/4.1.0/Cypress [completed]
Opening Cypress...
and build fails .
Am i missing anything here ?
It's because it's opening the test runner, which is used locally via npx cypress open.
From that output it looks like you're running npx cypress open run, which isn't a real command and will open the runner
In CI you need to use npx cypress run, which will run tests without user interaction. https://docs.cypress.io/guides/guides/command-line.html#How-to-run-commands
Your gitlab.yml file should accomodate for the npm installations.
One example is as below. Meanwhile also please check the test/run command for your specs under package.json file. use the same command to trigger the test in pipeline.
stages:
- test
test:
image: cypress/browsers:node12.14.1-chrome85
stage: test
script:
npm i
npm run start:ci &
npx cypress run
You should have your .gitlab-ci.yml with:
stages:
- test
cypress-test:
image: cypress/browsers:node16.14.0-slim-chrome99-ff97
stage: test
script:
- npm ci
- npx cypress run

How to run e2e tests headless with command line in cypress

I am using cypress testing server.
When I am running my tests with cypress window, its working right.
$./node_modules/.bin/cypress open --project tests/e2e/
But when I am trying to run it with command line, my tests didn't worked.
In screenshots I see, that my link doesn't loading and my page is empty and my tests result is false.
I am running with selected command
$ ./node_modules/.bin/cypress run --config pageLoadTimeout=10000,watchForFileChanges=false --project tests/e2e/
How I can run my tests with command line
First you should navigate to your project folder, for example if your cypress test is lying in below folder
C:/Project/
From the command prompt you should cd to the project folder, ie cd C:/Project/, now copy the cypress bin path as follows C:\Project\node_modules\.bin\ paste the path and then from there run the following command:
cypress run C:\Project\cypress\integration\examples\test-spec.js
note: This is tested and run in windows 10 environment

Mocha Run Configuration in IntelliJ

I have configured mocha to run testcases in my react app via mocha intelliJ mocha plugin , i have defined the configuration as below :
I have helper Javascript file which is suppose to be loaded by mocha before test hence i added --config in mocha option in above screenshot and the content of that file is below :
--recursive
--require helper.js
Issue is it seems like this helper is not loading hence i am getting errors like :
ReferenceError: regeneratorRuntime is not defined
windows is not defined
where in the windows is defined in helper.js hence my issue is how to load helper.js from mocha run configuration present in intelliJ
Alternative to this when i run below command in terminal this works fine and hence runs all test cases in terminal for mocha :
mocha --require babel-register --require ignore-styles --require ./test/test.helper.js
but i want to run single test from intelliJ only hence any pointers to this issue is greatly appreciated .
The right way to pass mocha.opts to mocha is --opts option, like --opts config/mocha.opts (path has to be relative to project root folder). The path to the local module in mocha.opts should also be relative to the project root.
For example, if my helper is located in <project dir>/specs/util/test.helper.js, and mocha.opts - in <project dir>/config/mocha.opts, .opts would be
--require ./specs/util/test.helper.js
and run configuration would look as follows: