Skip to content

Latest commit

 

History

History
233 lines (190 loc) · 6.39 KB

testing.md

File metadata and controls

233 lines (190 loc) · 6.39 KB

Testing Bauta.js components

In this examples, we will use jest as the testing framework.

Test a Step function or a Pipeline

Since pipelines and step function result are the same thing, both can be tested using the same pattern.

Imagine we have the following pipeline:

const { pipe } = require('@axa/bautajs-core');

module.export.pipeline = pipe(() => {
    const someObj = {
        foo: 1,
        boo: 2
    }

    return someObj;
}, (someObj, ctx) => {
 ctx.log.info(someObj, 'This is my object');

 return someObj;
}, (someObj, ctx, bautajs) => {
 return {
     ...someObj,
     id: ctx.data.id,
     constantData: bautajs.staticConfig.myConstant
 }
});

To test this we need three main things:

  • Create an BautaJS application instance.
  • Create a Bauta.js context.
  • Execute the pipeline passing the previous elements as parameters.
 const { BautaJS, createContext } = require('@axa/bautajs-core');
 const { myPipeline } = require('./pipeline');

 describe('test', () => {
     let bautajsInstance;
     beforeAll(() => {
         // Create the bauta instance with the required constants for the pipeline.
         bautajsInstance = new BautaJS({
             staticConfig: {
               myConstant: 'this is a constant'
             }
         })
     })

     test('some test', async () => {
       // Create a context with data needed for the pipeline
       const ctx = createContext({ data: { id:'someId' } });

       await expect(myPipeline(null, ctx, bautajsInstance)).resolve.toStrictEql({
           foo: 1,
           boo: 1,
           id: 'someId',
           constantData: 'this is a constant'
       })
     });
 })

Test a logger in the pipeline

Assuming we have the previous mentioned pipeline and we want to test that the info log This is my object is triggered.

So let's create the test for it:

 const { BautaJS, createContext } = require('@axa/bautajs-core');
 const { myPipeline } = require('./pipeline');

 describe('test', () => {
     let bautajsInstance;
     beforeAll(() => {
         // Create the bauta instance with the required constants for the pipeline.
         bautajsInstance = new BautaJS({
             staticConfig: {
               myConstant: 'this is a constant'
             }
         })
     })
     test('some test', async () => {
       // Create a context with data needed for the pipeline
       const ctx = createContext({ data: { id:'someId' } });

       //! Very important line, since the provider will
       //! create a child log and you will lose the spy
       //! on the children logger.
       ctx.log.child = () => ctx.log;
       const spy = jest.spyOn(ctx.log, 'info');

       await expect(myPipeline(null, ctx, bautajsInstance)).resolve.toStrictEql({
           foo: 1,
           boo: 1,
           id: ctx.data.id,
           constantData: 'this is a constant'
       });

       expect(spy).toHaveBeenCalledTimes(1);
     });
 })

Test a datasource restProvider

Testing a restProvider also follow more or less same philosophy as the pipeline testing.

Imagine we have the following restProvider:

const { restProvider } = require('@axa/bautajs-rest-datasource');

module.export.myRestProvider = restProvider((client, prev, ctx, bautajs) => {

return client.get({
    url: `${bautajs.staticConfig.BASE_URL}`,
    body: {
        foo: prev.foo,
        boo: prev.boo
    }
});
});

To test this we need three main things:

  • Create a Bauta.js application instance.
  • Create a Bauta.js context.
  • Execute the pipeline passing the previous elements as parameters.
  const { BautaJS, createContext } = require('@axa/bautajs-core');
  const { myRestProvider } = require('./my-rest-provider');

  describe('test', () => {
      let bautajsInstance;
      beforeAll(() => {
          // Create the bauta instance with the required constants for the pipeline.
          bautajsInstance = new BautaJS({
              staticConfig: {
                BASE_URL: 'https://base.url'
              }
          })
      })
      test('some test', async () => {
        // Create a context
        const ctx = createContext({});
        // Rest provider needs to be builded before call it.
        const stepFn = myRestProvider();
        // Create data present in previous step or what is the same,
        // data resultant from the execution of previous steps in the pipeline we will plug in this rest call.
        const previousStepData = {
            foo: 'test',
            boo: 'type1'
        }

        await expect(stepFn(previousStepData, ctx, bautajsInstance)).resolve.toStrictEql({
            responseBodyField: 'ok'
        })
      });
  });

Test an operation resolver

Let's create our dummy resolver using the pipeline that we already defined on the previous sections:

dummy-resolver.js

const { resolver } = require('@axa/bautajs-core');
const { myPipeline } = require('./pipeline');

module.exports = resolver((operations) => {
    operations.getCats.setup(myPipeline)
});

index.js

const { BautaJS } = require('@axa/bautajs-core');
const bautaInstance =  new BautaJS({
    staticConfig: {
        myConstant: 'this is a constant'
    }
});

module.exports.bautajsInstance = bautajsInstance;

Now we can start our testing. For that we will need:

  • Bootstrap BautaJS instance on the test
  • Call the operation to test
  const { BautaJS, createContext } = require('@axa/bautajs-core');
  const { bautaInstance } = require('./index');

  describe('test', () => {
      let bautajsInstance;
      beforeAll(async() => {
          // bootstrap the bautajs instance.
          await bautaInstance.bootstrap();
      })
      test('some test', async () => {
        // Create a context
        const ctx = createContext({});
        // Rest provider needs to be builded before call it.
        const stepFn = myRestProvider();
        // Create data present in previous step or what is the same,
        // data resultant from the execution of previous steps in the pipeline we will plug in this rest call.
        const previousStepData = {
            foo: 'test',
            boo: 'type1'
        }

        await expect(bautaInstance.operations.getCats.run({data: {
            id:'someId'
        }})).resolve.toStrictEql({
            foo: 1,
            boo: 1,
            id: 'someId',
            constantData: 'this is a constant'
        })
      });
  });