PACT, the way to code
Published: 2021-05-24 01:50:16

---
title: PACT, the way to code
published: true
description: 
tags: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpqsvtx2vwdk7uxeoylb.jpg
---

Hello Developers! Many of us use various design patterns with React Development to make React Apps more clean, clearly understandable, and more structured. But still we are facing issues with coding standards, we changed at one place and it breaks at another place. Is there any resolution for this? Yes, there is. PACT is a tool that allows you to create contract between various components of your app that tell you which components are compatible with each other. It can help you create a consistent structure in your React Apps.

**What is PACT?**

PACT stands for Prettier Acceptance Criteria Templates. It is a tool for creating pre-commit tests to enforce a consistent code style. PACT takes your current code style and compares it to the desired style. If your code matches, the test passes and your commit is accepted. If it doesn’t match, the test fails and your commit is rejected.

**How does it work?**

PACT is a tool that can be used to enforce a consistent code style. It takes your current code style and compares it to the desired style. If your code matches, the test passes and your commit is accepted. If it doesn’t match, the test fails and your commit is rejected.

For example, in a pre-commit hook, you could specify a config file with an array of rules that your project should follow and a command for checking if your code is compliant with those rules:

```js
var config = [{
    rule: {
        "error": true,
        "message": "Use double quotes instead of single",
        "matcher": "source.js",
        "lineRange": [1, 3]
    },
    input: "var x = 'foo"
    ' , }, { 
    rule: {
        "error": false,
        "message": "Use one space after function/array bracket",
        "matcher": "source.js",
        "lineRange": [4, 4]
    },
    input: "var x = function() {}, foo[",
}, {
    rule: {
        "error": true,
        "message": "Use two spaces after function/array bracket",
        "matcher": "source.js",
        "lineRange": [6, 6]
    },
    input: "foo[] = 'bar'",
}]

var check = require ( 'preact-pact' ).commands.check ; 
var pact = require ( 'preact-pact' ); 
check(pact( config )).run();
```

The whole thing is wrapped in a function that takes a configuration file and returns the promise that resolves to an array of errors that would be returned by the check command.

The input and matcher properties of each rule tell Weasel what range of code to check for compliance. Since Weasel is using source-map’s precise mappings, it’s possible to check specific lines or ranges of lines.

The check command looks for each rule in the config file and runs the command specified in the command property.


PACT can be used with any editor, but we will use VSCode as our example.

Install PACT via npm.

```npm install pact-cli --global```

Create a pact file for the folder where you want to enforce the code style. For this example, let’s create a pact file for the app root.

```pact create <app-name>```

This will create a pact file with the name <app-name>.js .

Add some rules to the pact file.

This pact file will have a single rule for the whole app.

```js
module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level 
                },
                message: 'Indentation should be 2 spaces per indent level.'
            }
        }
```

To run the pact, you need to npm install the project and then run the pact test.

```
npm install
npm run pact
```

The pact test will run and return a green check if it passes. In our case, it will return a red “x” because we haven’t added our rules into our app yet.

We can also add a pact test to enforce the code style for a file.

Add the following at the top of our index.js file.

```js
module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level }, message: 'Indentation should be 2 spaces per indent level.' 
            },
        }
```

Now, when we run the pact test, it will return a green check because the file already matches the code style.

If you want to enforce the code style for an individual file, you can add it to the pact file and run the test.

```js
module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level }, message: 'Indentation should be 2 spaces per indent level.',
                file: 'index.js' 
            }
        }
```

Now, when you run the test, it will return a red “x” again.



**Why we should use PACT?**

PACT is a good tool that offers many advantages. But the main advantage is that it allows you to build a consistent React App. It is a very good tool for enforcing a consistent project structure, code style, and workflow. This is why PACT should be used in modern React App Development.

Conclusion

PACT is a tool for enforcing a consistent code style across a project. It can help you create a consistent structure in your React Apps.It is a very good tool for enforcing a consistent project structure, code style, and workflow. This is why PACT should be used in modern React App Development.

Hope you like it. Share your feedback in the comment section below.