0

I am currently writing tests for a sort of Diagnostic test for a friend. I did a Python version of it, and what I want to do is to stop the test if the problem is taking too long. For example in Python this is how I did it:

@timeout_decorator.timeout(5)
def test_intersection_that_will_break_if_solution_is_too_slow(self):
    a = [i for i in range(0, 50000)]
    b = [i for i in range(0, 50000)]
    result = intersection.intersection(a, b)
    self.assertEqual(result, a)

In JavaScript, in Chaijs, I have this, but don't know how to stop the execution after a certain period of time. I am not the best at js either or chai, but maybe some sort of setTimeOut?

context('if a = [0,1,2,3...,49999] and b = [0,1,2,3...,49999] ', () => {
    it("should return [0,1,2,3...,49999] ", () => {
        const a = [];
        const b = [];
        for (let i = 0; i < 50000; i += 1) {
            a.push(i);
            b.push(i);
        }
        result = intersection(a, b);
        expect(result).to.eql(a);
    })
})

I want to know how to do that in Chai, because when I do the tests for a fibonacci problem, I want to stop the execution if it is taking too long(although I believe that chai/mocha stops the execution after 2000 ms but I still want to encourage my friend to look for a more optimal solution rather than getting the right answer with a red (1193ms)).

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Well, than maybe try this kind of timeout https://mochajs.org/#test-level Looks like it would fit your needs. Timeouts can be set for whole test suite or specific tests. – Hekmatyar Oct 17 '21 at 01:29
  • https://stackoverflow.com/questions/16947360/how-to-kill-a-javascript-function You can't actually just *kill* a function. If all the work it's doing is synchronous, the function itself would need to occasionally check in to see if it should stop. Nothing as simple as the python version I'm afraid (for synchronous functions at least). – DemiPixel Oct 17 '21 at 01:31
  • @Hekmatyar I tried that, but it says that this.timeout is not a function, I think this is on classes so it might be why it is saying that since I don't really have a class. As I said, I am not an expert on js so sorry if it is something very obvious and I don't see it. – Adilson Jurado Oct 17 '21 at 01:36
  • @DemiPixel mmm interesting. In Python what I did was to stop the program if it was more than 5 seconds, so I thought that Chai would have something similar. – Adilson Jurado Oct 17 '21 at 01:39
  • @Adilson Jurado it say's that this.timeout is not a function because of arrow function. Those lose the "context" of this. If you'll use ``` it("should return [0,1,2,3...,49999] ", function () { ``` it will work fine. Here I just wrote some example test just to make sure: it('TimeoutWorks', function () { this.timeout(1); const arr = new Array(999999).fill(0); arr.map(x => Math.sqrt(x + Math.random())).forEach(x => assert(x < 100000)); }); // sorry but code formatting in comments doesn't work – Hekmatyar Oct 17 '21 at 01:43
  • Ohhh so what I was getting before when I had it like that, was this error: Error: Timeout of 5000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. And so even though my function works in an O(n), it would still wait for those 5 seconds, and I wanted it to do that only if it is taking more than 5 seconds aka in a bad time complexity solution – Adilson Jurado Oct 17 '21 at 01:48
  • I used a O(n) solution to test if the timeout was going to still wait for 5 seconds even if the program runs just fine when the function is as fast as it could be, but what I wanted was for that to happen only if it has a solution like O(2^n) or something really bad – Adilson Jurado Oct 17 '21 at 01:49
  • 1
    If you desperately want a solution... this one gonna be painful to look at https://jsfiddle.net/t2e7v9rL/ Basically you can set a timeout at the start of the test passing a function to it that will deliberately fail the test... Mocha stops execution on first failed assertion so it will stop right there and then. I don't know how bout Chai but in plain Mocha you can pass third argument that will be the "fail title" of the test to make it at least a bit more obvious why it failed, but the solution isn't very clean imo. But it will work as you expect. And arrow function won't be a problem either – Hekmatyar Oct 17 '21 at 02:13
  • Ok don't take the comment up there to heart. It looks like it's quite inconsistent. I'll try to find some proper solution as now I'm intrigued myself. – Hekmatyar Oct 17 '21 at 02:45
  • I’m curious too haha. It’s shocking how easy it is in python with unittest but with js it’s much harder – Adilson Jurado Oct 17 '21 at 03:59

0 Answers0