Skip to main content

Testing

If we want we can create tests for our package, for this purpose jest matchers have been created to test the package.

If we have not configured the tests in the creation of the package, we can add the matchers with the following command:

npm i --save-dev @opensecdevops/jest-osdo

Once installed, we can add the matchers in the jest configuration file:

{
"jest": {
"setupFilesAfterEnv": ["@opensecdevops/jest-osdo"]
}
}

Once the matchers have been added, we can create the tests for our package.

Test of the function toBeStructure

It allows us to check if the config.json has the correct structure.

const config = require("../config.json");

test('test validate json', () => {
expect(config).toBeStructure();
});

Test of the function toBeRender

It allows us to make sure that the final files we want to render with the received data are correct.

const fs = require("fs");
const path = require("path");

test("test generate gitleaks", () => {

const datas = {
gitleaks: {
allow_failure: false,
artifacts: false,
image: 2,
job_name: "gitleaks",
},
};


const yalm = fs.readFileSync(
path.resolve(__dirname, "./fixtures/renders/total.yml"),
"utf8"
);

render = [
{
file: ".gitlab-ci.yml",
view: yalm,
language: "yaml",
},
]

expect(render).toBeRender(datas);
});

In this case in the expect we have to pass the array with the final result we want and in the toBeRender function we pass the object with the data we want to render.

information

The render structure is an array of objects that is used by the generator to paint on the web.

Test of the toBeRules function

It allows us to check if the rules defined in the config.json are working as we want.

The [robust-validator] library (https://github.com/axe-api/validator) is used for validations.

describe('.toBeRules', () => {
test('First rules', () => {
const datas = {
npm: {
job_name: 'npm',
},
};

expect(true).toBeRules(datas);
});