Node Testing: how to run bash commands after mocha test cases - node.js

I'm using Mocha and Selenium to do an e2e test for my node web app. Now the problem is, I want to tear down databases after the execution of all test cases.
Backend uses two databases - mongo and postgres. I created new empty testing databases of each, just for the e2e testing. During the testing some records are inserted into databases. So, at the end of testing (when mocha test cases which invoke selenium are all executed), I want to tear down the test databases and create new ones. These operations are apparently done by bash commands.
So, is this a good practice and if so, how can I achieve this, like writing bash commands in js file(which execute the mocha test cases)?

A better approach would be to drop tables (and collections) and recreate them via migrations instead of dropping whole database if you have reasonable number of migrations. This has an added advantage of validating your down migrations which can be handy if you had to revert.
You can run migrations (down and then up) at the start of your tests . We are using db-migrate and this is how you can do this in package.json
"scripts": {
"test": "npm run testdb && <test command>",
"testdb": "db-migrate reset --env test && db-migrate up --env test"
}

Related

Running tests in a directory parallely in Vitest

I am using Vitest as the testing framework for my project.
I have a directory called canRunInParallel which contains multiple test files, like A.spec.ts, B.spec.ts ..... Z.spec.ts. Since this directory contains multiple test files, and none of the tests can race condition, I want to configure Vitest to run all these tests concurrently, so that I can improve my testing time.
Can anyone help me in figuring out how to achieve the same (by most probably modifying the configuration of Vitest runner)?
This functionality is not yet supported by Vitest.
You can only run the tests in a test suite (test file) concurrently using Vitest.

Run a function before all the test modules in Vitest

I am using Vitest as my testing framework in a project.
I have multiple test files in the project, let's say A.spec.ts and B.spec.test. I am using the standard test script (vitest run --no-threads --coverage) to test my code. I want to run a certain function (to purge and clean the testing database), before and after all the test suites are run (i.e. before all the tests in A.spec.ts and B.spec.ts, and after them as well).
Is there any way to achieve the same? I read about the methods like beforeAll and afterAll, but they work in the context of a file, and thus do not help with my use case.
you should try global setup
globalsetup

Is there a way to set up a clean testing environment for Cypress each time I run tests?

We use Cypress for thorough e2e testing on our site.
The Tech stack is React + Node(koa.js).
We have a high test coverage since we tend to mock most of the user actions (most of the crud methods as well).
It happens sometimes that a test suite fails during the execution (or is interrupted by something) so we have a duplicated entry on the next run (create test fails). Then I need to manually delete testing entries from the site and re-run the pipeline.
We want to make sure that we have a clean database for testing on each run. I could use an advice. Thanks in advance!

Is there a way to set test coverage for tests (f.e. 95%) using cypress.io in TeamCity?

We are using TeamCity to run cypress.io for our NodeJs application and some of the tests are failing due to timeouts. These timeouts seem based on latency to the database (AWS RDS) and vary from build-to-build.
What we would like to do is to try setting test coverage to a 95% success rate and see if this allows the build to continue.
There is an option in TeamCity to have build steps to run regardless if the previous steps failed, but we would like our tests to not run in this fashion.
Any advice would be appreciated. Thanks!
We ended up modifying the tests so that they would behave as expected in the new environment. We also decided to run the tests as they were built to run with a local Postgres database.
The significant issue we were dealing with was our Cypress tests were extremely fragile when moving to an RDS database. The tests were configured for a local dev environment with a local Postgres database and moving to RDS in the CI environment broke them.
My recommendation is for anyone setting up automated tests to make sure tests run in your CI environment as they do in their development, not to configure/edit your tests to pass in CI.
In other words, if your tests break in your CI environment, then they need to be fixed in the dev environment.

Can I make jest run Javascript tests serially?

I have some tests that interact with a database. I have the test code set to clear out the test table before/after each test. This doesn't work with jest though, since it's running multiple tests in parallel, so it starts another test that clears out the test db while the first one's running!
Is it possible to set jest into a serial mode (without writing my own test runner, which seems complicated)?
Start Jest using the --runInBand CLI option:
jest --runInBand
That option causes Jest to
Run all tests serially in the current process

Resources