The tests are part of the feature
Tags: testing, productivity, development
Published: 2023-08-02 14:06:33

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](https://dev.to/woovi/how-to-ship-faster-to-production-2e1f), 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](https://dev.to/woovi/failing-tests-write-to-fail-and-fix-1k53) 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](https://dev.to/woovi/1-commit-per-pull-request-workflow-49f7). 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:
```tsx
const getStagedFiles = async () => {
  const output = execSync('git diff --name-only --cached', {
    encoding: 'utf-8',
  });

  return output.trim().split('\n');
};
```

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:

```tsx

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;
};
```

And finally, we run these tests: 

```tsx

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);
  }
};
```

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](https://woovi.com/) 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 <a href="https://unsplash.com/@ryoji__iwata?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Ryoji Iwata</a> on <a href="https://unsplash.com/photos/5siQcvSxCP8?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>