Giter Club home page Giter Club logo

Comments (4)

HigoRibeiro avatar HigoRibeiro commented on May 18, 2024 2

Hi @lamana-team unfortunately this provider is not prepared to work cool with tests, so you need to add a palliative strategy.

When running your tests you shouldn't use the bull as production, I guess that's why what your job is sending the e-mail.

So, what can you do is make a fake provider like below:

// test/mocks/BullMock.js
class BullMock {
  constructor () {
    this.reset()
  }

  call (...args) {
    const [funcName, ...rest] = args
    this.spy.calls.push({ funcName, args: rest, fail: this.fail })
    this.spy.called = true

    if (this.fail) {
      throw new Error(this.fail)
    }
  }

  makeFail (err) {
    this.fail = typeof err === 'string' ? err : 'error'
  }

  clearFail () {
    this.fail = null
  }

  async add (...args) {
    this.call('add', ...args)
  }

  async schedule(...args) {
    this.call('schedule', ...args)
  }

  reset () {
    this.spy = {
      calls: [],
      called: false,
      fail: null
    }
  }
}

module.exports = new BullMock()

Then, inside the vowfile:

const ace = require('@adonisjs/ace')
const { ioc } = require('@adonisjs/fold')
const BullMock = require('./test/mocks/BullMock')

ioc.singletonFake('Bull', () => BullMock)

Now, when you call a test actually you'll use the mock instead of the RealProvider.

You can know if your job was called on your test file:

const Bull = use('Bull') // It's the mock.

test('my awesome test', async ({ assert }) => {
  // ...
  
 assert.equal('add', Bull.spy.calls[0].funcName)
 assert.isTrue(Bull.spy.called)
});

You should test your job separate as unit test:

const AwesomeJob = use('App/Jobs/AwesomeJob')
const Mail = use('Mail')

test('my awesome test', async ({ assert }) => {
  Mail.fake()

  const job = new AwesomeJob()

  await job.handle({ data: myData })

  // my assertion

  Mail.restote()
});

That's it!

from adonis-bull.

lamana-team avatar lamana-team commented on May 18, 2024

I have been working on this problem for a few hours and although I have not been able to find a solution, however I managed to do something paleative.

What I did was create a condition within the control based on the environment. If the variable NODE_ENV is equal to 'testing' I send the email inside the controller instead of running the JOB.

In this case, considering that the job is exclusively for sending an email and has no other logic involved, I think everything is fine. But I still think there is a better way.

from adonis-bull.

HigoRibeiro avatar HigoRibeiro commented on May 18, 2024

Hey, I will close this issue because is resolved.

from adonis-bull.

Agilulfo1820 avatar Agilulfo1820 commented on May 18, 2024

I did same but it doesn't work on my Adonis v4.
I have a job launched by a controller. I'm testing the endpoint and I want to be sure that the job was launched.

@HigoRibeiro I set up everything as you said but my actual job is starting and Bull.spy is always undefined

from adonis-bull.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.