DEV Community

Cover image for The tests are part of the feature
Vinicius Blazius Goulart for Woovi

Posted on

The tests are part of the feature

At Woovi, we make a lot of changes every day, whether they involve new features or fixes. We have a strict policy that all of our pull requests must include at least one test to validate the implemented feature or fix.

To maintain a seamless process of continuously shipping to production, it is essential to ensure that everything continues to work as expected. Therefore, we always deliver Pull Requests with associated tests to validate that the changes make sense and function correctly.

A Pull Request lacking tests indicates that either the modifications made do not have any existing tests or that they are not functioning as intended. In both cases, we adhere to the culture that a Pull Request must have tests attached to it.

We follow the culture of creating small Pull Requests. Each commit is clearly defined and has a specific purpose. By incorporating tests with each Pull Request, we can better understand the impact of each commit and easily identify the scenarios it affects. This approach significantly simplifies the reviewing process for our team.

We have implemented a range of powerful tools and processes here at our company, including an automated testing system to ensure the effectiveness of code changes.

Firstly, we retrieve the files that have been modified using the following code:

const getStagedFiles = async () => {
  const output = execSync('git diff --name-only --cached', {
    encoding: 'utf-8',
  });

  return output.trim().split('\n');
};
Enter fullscreen mode Exit fullscreen mode

Once we have the modified file list, we delve into each file's directory to locate the associated test files. We look specifically for the tests folder, which houses our tests. Any .spec.ts or .spec.tsx files we find will be added to the array of files to be tested:


const findTestFiles = async (filePaths: string[]) => {
  const testFiles: string[] = [];

  for (const filePath of filePaths) {
    const fileDirectory = path.dirname(filePath);
    const testDirectory = path.join(fileDirectory, '__tests__');

    try {
      const files = await fs.readdir(testDirectory);
      const testFilesInDir = files.filter(
        (file) => file.endsWith('.spec.ts') || file.endsWith('.spec.tsx'),
      );

      testFiles.push(
        ...testFilesInDir.map((file) => path.join(testDirectory, file)),
      );
    } catch (err) {
      // Ignore if __tests__ directory does not exist
    }
  }

  return testFiles;
};
Enter fullscreen mode Exit fullscreen mode

And finally, we run these tests:


const runTests = async (testFiles: string[]) => {
  if (testFiles.length === 0) {
    //eslint-disable-next-line
    console.log('No tests to run.');

    return;
  }

  const testFilesArg = testFiles.join(' ');

  try {
    execSync(`TZ=utc yarn test ${testFilesArg}`, { stdio: 'inherit' });
  } catch (err) {
    //eslint-disable-next-line
    console.error('Some tests failed.');
    process.exit(1);
  }
};
Enter fullscreen mode Exit fullscreen mode

By automating this process, we can effortlessly include tests with every Pull Request we make. Whether modifying an existing test to accommodate a new flow or introducing a completely new test, the system facilitates the validation of these changes.

With these advanced automation practices in place, we are confident in the quality and reliability of our codebase. This enables us to ship faster to production while maintaining a high level of confidence in our software.


Woovi is a Startup that enables shoppers to pay as they please. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.


Photo by Ryoji Iwata on Unsplash

Top comments (0)