Comments (4)
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.
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.
Hey, I will close this issue because is resolved.
from adonis-bull.
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)
- How to test jobs with Japa?
- No context in bull exceptions handler like in default one.
- Support for addBulk
- Properties names in job are with underscores
- Support AdonisJS 5?
- BullMQ connection options deprecation
- keyPrefix issue no data passed, Error: Missing lock for job (v5) HOT 1
- How can we keep the data persistent when server goes down
- How to protect the bull board behind a login form?
- Conflicting readme and description - is this library ready for Adonis v5 or not?
- v5 job file malformed HOT 1
- Is this project officially out of support? HOT 3
- Remove repeatable jobs HOT 2
- Adonis v6 HOT 1
- What is the recommended setup for multiple workers?
- Cannot read property 'bull' of undefined HOT 1
- General improvements (v5)
- how do i schedule a task to repeat every 1st of the month or choose the day? HOT 1
- Adonis v5 Key Prefix HOT 2
- Dúvida sobre o Bull HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from adonis-bull.