DEV Community

Jia Hua Zou
Jia Hua Zou

Posted on • Updated on

Prerelease 2.6

Version 2.6 is coming soon. I have been working on the tests for the search function for Telescope.

At first it sounded like a simple task, but when I start to dig into the problem it was getting a lot more difficult. The simplest thing that I did was to setup the initial setup for the tests. Issue-2757. Even that had some problems. I thought the config setup was correct but later found out that the configuration was not properly setup even tho the test can still work fine. This was because when we ran the jest command to run our test, the command included the setup config. We don't want that because different not all test will need the same setup.

Before:
 "jest": "cross-env NODE_ENV=test LOG_LEVEL=error MOCK_REDIS=1 FEED_URL_INTERVAL_MS=200 jest -c jest.config.js --",
After:
"jest": "cross-env LOG_LEVEL=error jest -c jest.config.js --",
Enter fullscreen mode Exit fullscreen mode

With this change, each test can use their correct config file. After that, in the jest.config.js for the search. I would need to add the proper setting so like so:

const baseConfig = require('../../../jest.config.base');

module.exports = {
  ...baseConfig,
  rootDir: '../../..',
  setupFiles: ['<rootDir>/src/api/search/jest.setup.js'],
  testMatch: ['<rootDir>/src/api/search/test/*.test.js'],
  collectCoverageFrom: ['<rootDir>/src.api/search/src/**/*.js'],
};
Enter fullscreen mode Exit fullscreen mode

The jest.setup.js is in the same directory as the jest.config.js. All it does is to set a mock Elastic to 1(true).

Once that is all setup, I can create my test file. If I do not create a test, Jest will throw an error when every we try to run the test command. So what I add was a simple test to test out if it can return error 400.

const request = require('supertest');
const { app } = require('../src');

describe('/query routers', () => {
  it('return error 400 if no params given', async () => {
    const res = await request(app).get('/');
    expect(res.status).toBe(400);
  });
});```



I know this will always be true because the connection to a mock elastic was not created. My next step would be trying to understand how we can create the connection to a mock elastic and dummy data.
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)