DEV Community

Cover image for PACT, the way to code
Diego Vallejo
Diego Vallejo

Posted on

PACT, the way to code

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:

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();
Enter fullscreen mode Exit fullscreen mode

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 .js .

Add some rules to the pact file.

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

module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level 
                },
                message: 'Indentation should be 2 spaces per indent level.'
            }
        }
Enter fullscreen mode Exit fullscreen mode

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

npm install
npm run pact
Enter fullscreen mode Exit fullscreen mode

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.

module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level }, message: 'Indentation should be 2 spaces per indent level.' 
            },
        }
Enter fullscreen mode Exit fullscreen mode

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.

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' 
            }
        }
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
destructor1702 profile image
Diego Vallejo • Edited

Plot twist:
This post was created using GPT-3 and is full of untrue information, the pact library does not exist yet... I just copied, formatted and designed the header image

proof

Stay tuned for more serious uses that I am giving this incredible tool