Is there a better way to test Node.js application with ElasticSearch? - node.js

If use Ruby on Rails framework, we can run the test in a easy way. It can create a test database, and we can make some fake data. After all the test finished, the test database will be cleaned.
Now want to do the same test with Node.js, Express framework and ElasticSearch. If create index data in the real ES server, maybe not the good way(Think ES as development environment used). Is there a better way to do it as RoR above?
I have researched from internet, but didn't find good example.

Related

Integration Testing a Node Server without much configuration

I want to set up a CI/CD workflow which includes testing my server as a whole.
Using node I have a couple options such as
Jest,
Mocha,
Chai
or a different approach such as Postman/Newman.
I have tried out Jest and found it very difficult to adjust my databases such as Postgres and Redis to the mock environment. In fact, I didn't get it to work at all, presumably because of all the technologies involved.
Is there is a tool, maybe one of the others mentioned, that takes the server as it is, without any additional configuration, calls the endpoints of the Server and gives me results, which I can then use to further take action (push/rollback/discard changes)?

How to perform integration testing using Node.js?

I am writing a Node.js application that acts as GraphQL server. And I want to test if GraphQL is giving me the right results.
My guess is to make a request to server in each test and then watch if it is right or not. But I'm not sure how to set it up, how to run the server and clean the database before all tests and shutting it down after all tests are finished.
I worked with rspec using Ruby on Rails before, so I want to do something like rspec does in Node.js. Is there a way to accomplish it using Jest? And if not, how can I do something simular?
This isn't really an Jest specific question but more how to perform integration testing in Node.
The fact you're using Jest makes very little difference other than you'll want to ensure auto mocking is turned off.
Normally when writing integration style tests I'd use something like supertest to make requests and setup the database to use sqlLite or similar running a seed.
I'd recommend looking for similar tools like graphql-tester.

elasticsearch embedded in Node.js

I am new on developing in Node.js, I would like to know if there's a way to have ElasticSearch embedded in a Node.js application for testing purposes or if there's another way to test interaction with ElasticSearch without having an ElasticSearch running instance (mock ?)
There are no existing mock libraries for Elasticsearch and Node at the moment. If you know exactly what queries you are going to be performing, you could use a mocking library to mock the entire ES client and assert simple behaviors.
What I'd recommend, however, is actually running ES in a development environment for testing. I find that mocking external services is a pretty tricky thing to do in general, and your tests will likely be much more robust / trustworthy if running against an actual instance of the server.

Automated testing node js and database

I'm using jenkins to automate the testing of my node js application? How do I test the queries to the database? How does Jenkins know to build the mongo db database first? How does automated testing work with database?
I don't use Jenkins for CI, I tend to use TeamCity, but I think your questions can be answered independently of which CI technology you use.
There is no definite answer to your questions, it all depends:
If you are looking to implement full integration tests, including querying a real database, then I would suggest creating a separate database just for testing like - mydb-test. You would have to configure your tests to use this database via config etc. In case you want to isolate your tests from the data access layer, then you need to mock the data access. This can be done by using a mock library, this will be easier to use depending on how well architected is your application code, where dependency injection IMHO is really important on this matter.

Mock/Test Mongodb Database Node.js

I am learning nodejs and I have a mongodb database with which i have to interact with. I am currently thinking of using mocha for a unit test framework and zombie.js for a acceptance test framework. I was wondering how could I do full scale acceptance tests which hit the mongodb database. Is there a framework/module that helps with replacing the database with a test database or does either mocha or zombie.js have functionality that can easily be used to replace the database.
Also is there a framework that is similar to the idea of factories (instead of fixtures) in creating database objects.
A similar concept that I have encountered in the rails world is in rspec, there is a spec_helper.rb file which runs before the tests are run which set the projects configuration to decide which database to hit when running tests. And it uses database_cleaner to clean out the test database before tests are run. For factories, i have used Factory girl to create factory objects from database schema again in the rails world.
Thanks
If your database access is a separate module, you can mock out the module or parts of the module (e.g., the configuration part) using one of the following:
sinon
rewire
horaa
sandboxed-module
The answers to the following related question lists a number of possible solutions/approaches:
How do you mock MySQL (without an ORM) in Node.js?
In order to spin up a real in-memory mongodb for testing, https://github.com/nodkz/mongodb-memory-server helps as well.

Resources