seneca-msg-test

    3.4.0 • Public • Published

    seneca-msg-test

    npm version Build Dependency Status Coveralls Maintainability Known Vulnerabilities Gitter

    Structured testing of seneca plugin messages.

    Run Seneca messages in series (not parallel) to validate behavior against expectations.

    Example

    See example folder

    Note

    To use @hapi/joi, require with:

    const Joi = require('seneca-msg-test').Joi
    
    

    This ensures that the Joi versions match.

    Test Specification

    # file: test-spec.js
    module.exports = {
      print: true,
      pattern: 'role:foo',
      data: {
        foo: {
          bar: {
            b0: { id: 'b0', b: 0 },
            b1: { id: 'b1', b: 1 }
          }
        }
      },
      calls: [
        {
          // combined with top level pattern to form msg: 
          // role:foo,cmd:get,id:b0
          pattern: 'cmd:get',
          params: { id: 'b0' },
     
          // output result must match this Optioner (Joi-based) structure
          // https://github.com/rjrodger/optioner
          out: { b: 0 }
        },
        {
          // name a call to reference it later
          name: 'list-0',
          pattern: 'cmd:list',
          params: {},
          out: [{b: 0}, {b: 1}]
        },
        {
          pattern: 'cmd:get',
          // use https://github.com/rjrodger/inks back reference syntax
          params: { id: '`list-0:out[1].id`' },
          out: { b: 1 }
        },
      ]
    }
    

    Test code

    # basic.js
    const Seneca = require('Seneca')
    const SenecaMsgTest = require('..')
    
    const seneca = Seneca().test()
    
    // Test specification
    const test_spec = require('./test-spec.js')
    
    
    // Define some simplistic message actions
    seneca
      .use('promisify')
      .use('entity')
      .message('role:foo,cmd:get', async function(msg) {
        return this.entity('foo/bar').load$(msg.id)
      })
      .message('role:foo,cmd:list', async function(msg) {
        return this.entity('foo/bar').list$()
      })
    
    // Use this inside your testing code
    const run_msgs = SenecaMsgTest(seneca, test_spec)
    
    async function run_test() {
      await run_msgs()
    }
    
    run_test()
    

    Printed output (optional)

    CALL   :  cmd:get { id: 'b0' }
    ERROR  :  null
    RESULT :  $-/foo/bar;id=b0;{b:0}
    
    
    CALL   :  cmd:list {}
    ERROR  :  null
    RESULT :  [ $-/foo/bar;id=b0;{b:0}, $-/foo/bar;id=b1;{b:1} ]
    
    
    CALL   :  cmd:get { id: 'b1' }
    ERROR  :  null
    RESULT :  $-/foo/bar;id=b1;{b:1}
    

    Install

    npm i seneca-msg-test

    DownloadsWeekly Downloads

    202

    Version

    3.4.0

    License

    MIT

    Unpacked Size

    26.7 kB

    Total Files

    12

    Last publish

    Collaborators

    • rjrodger