I'm wanting to force a test returns failure, bring success, why this?
I am using the proctrator and I need to wait for a data that comes from the server to the screen, however I have to update the page every 1 minute, I could not do something that updates and at the same time wait for the answer, I had to make a "for" execute a promise with loop that repeats 14 times, which gives a total of 14 minutes and every minute it refreshes the page, when it arrives the dice throws me an exception that can end before the loop is over, so that I can move on to the next test .
To not fail in my test, I would like this exception to be true, but I could not put a function in the jasmine to modify it, I have a very simple example of what I would like to do.
describe('Start simulator False', () => {
it('expect a fake to turn true ', () => {
const addition = 5 + 5;
expect(addition).toBe(2);
});
});
This expectation that expects 10 instead of 2, I would like instead of seeing a fake would like to see a truth, I know it is wrong but I can create a unique function that only I can use when ignoring its basic logic because I I know it's true and the only alternative I have is this.
You can create your own custom equality testers. See the documentation : https://jasmine.github.io/tutorials/custom_equality
But i will replace the jasmine equality tester.
So maybe you want to add your own matcher : https://jasmine.github.io/tutorials/custom_matcher
Related
I'm making a command .report #user that creates a simple poll with 2 buttons added "agree" and "disagree". After certain time i want the bot to register user if agree's is more than disagree.
How can i make my bot count the results of voting after 7 days and then based on this either register on DB an user or send "Report Voting Failed".
What I hope is to be able to store the expiration date of the voting and that on that date the voting stops working and the corresponding action is taken.
You can use setTimeout() for this. setTimeout() takes in two parameters. One, a function which holds your code that will be ran after the time has passed. Two, the amount of time in milliseconds to wait before executing your code.
Here is an example of how to use this.
console.log('1') // will console.log 1 instantly
setTimeout(() => {
console.log('2') // will console.log 2 after 1 second.
},
1000) // 1s = 1000ms
Furthermore, you can use buttons to accomplish the task of reporting. Check out the guide on buttons for more info. Once a button is pushed, see whether it is an Agree or Disagree then do what you must from there. After the seven days is up, make the buttons unable to press, or whatever you want.
You might want to employ a node.js module such as ms to convert 7d into ms. This could be useful as 7 days in milliseconds is 604,800,000. Using ms,
you could do
const sevenDays = ms('7d') // returns 604800000
and then pass that into your setTimeout() function.
like so:
setTimeout(() => {
console.log('The seven days has passed')
}, sevenDays)
Note however your bot will need to stay online for the duration of these 7 days, otherwise it will not run as it has forgotten that the setTimeout() function was used in an earlier instance.
Most of Jest's expect(arg1).xxxx() methods will throw an exception if the comparison fails to match expectations. One exception to this pattern seems to be the toMatchSnapshot() method. It seems to never throw an exception and instead stores the failure information for later Jest code to process.
How can we cause toMatchSnapshot() to throw an exception? If that's not possible, is there another way that our tests can detect when the snapshot comparison failed?
This will work! After running your toMatchSnapshot assertion, check the global state: expect(global[GLOBAL_STATE].state.snapshotState.matched).toEqual(1);
Just spent the last hour trying to figure it out for our own tests. This doesn't feel hacky to me either, though a maintainer of Jest may be able to tell me whether accessing Symbol.for('$$jest-matchers-object') is a good idea or not. Here's a full code snippet for context:
const GLOBAL_STATE = Symbol.for('$$jest-matchers-object');
describe('Describe test', () => {
it('should test something', () => {
try {
expect({}).toMatchSnapshot(); // replace with whatever you're trying to test
expect(global[GLOBAL_STATE].state.snapshotState.matched).toEqual(1);
} catch (e) {
console.log(`\x1b[31mWARNING!!! Catch snapshot failure here and print some message about it...`);
throw e;
}
});
});
If you run a test (e.g. /Foobar.test.js) which contains a toMatchSnapshot matcher jest by default will create a snapshot file on the first run (e.g. /__snapshots__/Foobar.test.js.snap).
This first run that creates the snapshot will pass.
If you want the test to fail you need to commit the snapshot alongside with your test.
The next test builds will compare the changes you make to the committed snapshot and if they differ the test will fail.
Here is the official link to the Documentation on 'Snapshot Testing' with Jest.
One, less than ideal, way to cause toMatchSnapshot to throw an exception when there is a snapshot mismatch is to edit the implementation of toMatchSnapshot. Experienced Node developers will consider this to be bad practice, but if you are very strongly motivated to have that method throw an exception, this approach is actually easy and depending on how you periodically update your tooling, only somewhat error-prone.
The file of interest will be named something like "node_modules/jest-snapshot/build/index.js".
The line of interest is the first line in the method:
const toMatchSnapshot = function (received, testName) {
this.dontThrow && this.dontThrow(); const
currentTestName = ....
You'll want to split that first line and omit the calling of this.dontThrow(). The resulting code should look similar to this:
const toMatchSnapshot = function (received, testName) {
//this.dontThrow && this.dontThrow();
const
currentTestName = ....
A final step you might want to take is to send a feature request to the Jest team or support an existing feature request that is of your liking like the following: link
Firstly, I am very new to node.js and I just started learning about callback function. So, I came to know that callback function basically works to handle multiple requests.
Then I thought of writing a function to see how it normally works without using callback function and I am getting an error here.
[Screenshot of the output][1]
https://i.stack.imgur.com/30AvR.png
Here, I thought it would work like :
Every 5 seconds it would display
Order placed : 1
Delivered food with order number: 1
.
...
....
....
Delivered food with order number: 6
To achieve expectation you should use async series method
Please refer below stackoverflow
Nodejs async series - pass arguments to next callback
I'm writing a script to batch process some text documents and insert them into a mysql database. I'm trying to use the async library because using a standard while loop blocks the event queue and prevents the insert queries from getting run until all are generated. Since that may take 10 minutes or more, I get a timeout. So, I am trying to use async to avoid blocking the main thread. However, it's not working as expected. When I run the simplest form of the code below, using node test.js, in the command line, it only executes once, instead of infinitely. It seems like the computer is terminating the node process early since it is non-blocking. This, of course, is not what I want. Why is this, and how can I get it to work correctly?
//this code should run forever, constantly printing "working". However it only runs once.
var async = require('async')
async.whilst(function(){return true},function(){console.log("working")})
The second parameter for whilst() is a function that takes in a callback that needs to be called when the current iteration is "done."
So if you modify the code this way, you'll get what you're expecting:
var async = require('async');
async.whilst(function() {
return true
}, function(cb) {
console.log("working");
cb();
});
I have 2 nodeunit test cases in which if first fails i don't want to run 2nd test case.(Process should skip testing 2nd test case with proper result.) How do I do the same.
I have written sample code where In first test case I have specified number of expects should be one. Even if first test case fails, second test case still executes. What could be the issue here.
exports.testSomething = function(test){
test.expect(1);
test.ok(false, "this assertion should fail");
test.done();
};
exports.testSomethingElse = function(test){
test.ok(true, "this assertion should pass");
test.done();
};
Generally it is a bad idea to make separate tests dependent on eachother, and testing frameworks don't support this kind of thing because of that. Nodeunit tests are declared and processed for execution before any one test has started so there is no way to change which tests run.
For that specific example, if the add fails, then it makes sense that delete fails. It might be slightly annoying to get more output, but it's doing just what it is supposed to do.
It may not be applicable in this cause, but one option is to have a separate section for your delete tests where you use setUp to set up everything that delete needs without having to actually call add.
Update
test.expect is used to specify how many assertions were expected in a single test. You only really need it if your test has asynchronous logic. For example:
'test': function(test){
test.expect(5);
test.ok(true, 'should pass');
for (var i = 0; i < 4; i++){
setTimeout(function(){
test.ok(false, 'should fail');
}, 5);
}
test.done();
},
Without the test.expect(5), the test would pass because done is called before the line in the setTimeout has run, so the test has no way of knowing that is has missed something. By having the expect, it makes sure the test will properly fail.