1

Good Day,

I am quite new to Jasmine Framework and trying hard to understand a test suite for a function given below:

constructor() {
        this.showToast = true;
}

simulateSendingMessage() {
        return new Promise((resolve) => {
            setTimeout(() => {
                this.showToast = false;
                resolve("Hello");
            }, 4500);
        });
    }

Test Suite:

it('should wait half a second', (done) => {
        const obj: Car = new Car();
        const clock = jasmine.clock().install();
        let messagePromise = obj.simulateSendingMessage();
        clock.tick(4500);
        messagePromise.then(() => {
            expect(obj.showToast).toBe(false);
            clock.uninstall();
            done();
        });
    });

The above test case executes successfully.

Step 1: Create an instance of Class.

const obj: Car = new Car();

Step 2: Install the clock.

const clock = jasmine.clock().install();

Step 3: Assign the promise to the variable.

let messagePromise = obj.simulateSendingMessage();

Step 4: And then moving the time ahead using .tick

clock.tick(4500);

Step 5: Wait for the promise to resolve uninstall the clock and test the expectations.

Here I have a set of question's, which confuse me from a StackOverflow link which says

The install() function actually replaces setTimeout with a mock function that jasmine gives us more control over.

  • Does this mean that what ever time I pass in the tick will overwrite the actual time passed in "setTimeout"?

But this does not actually happen. Then why the author says below?(Because we have to actually wait for the time given in "setTimeout")

because no actual waiting is done. Instead, you manually move it forward with the tick() function, which is also synchronous.

  • Author also provided an example

Suppose you had a function that internally set a timeout of 5 hours. Jasmine just replaces that setTimeout call so that the callback will be called when you call tick() so that the internal counter reaches or exceeds the 5 hour mark. It's quite simple!

If in the function we have a setTimeout to execute in 5hr's then does the test case have to wait for 5hr's? If I am getting this wrong what would be the test suite for it?

Please help me get over these hurdles. Any suggestion would be appreciated.

Also, I have created a GitHub repository where I wanted to test the exact function but with .tick(10) milliseconds but my test case execution of a single spec is taking a time of around 4999 ms to complete(Don't know why)

https://github.com/dollysingh3192/ts-babel-template/tree/testPromise

Car.js Car.spec.js

enter image description here

Thank you in advance.

Dolly
  • 2,213
  • 16
  • 38
  • 1
    It does not replace the time passed into `setTimeout`; it replaces `setTimeout` with a function that is named exactly the same, but doesn't wait for time to pass; it just compares the time passed to `tick` with that passed to `setTimeout`; if the latter is less than the former, it runs the code. – Heretic Monkey Jan 28 '21 at 14:49
  • @HereticMonkey Thanks for your response. Just one more small help from you and all my doubts will get clear is that I have a code repository on GITHUB ```https://github.com/dollysingh3192/ts-babel-template/tree/testPromise``` where I am writing the exact spec and function stated above, wherein ```setTimeout``` I state time of 4999 and inside test suite, I am using ```.tick(10)``` But on running ```npm run test``` it is taking the time of around 4999 ms to execute that single test. Any suggestion would be helpful – Dolly Jan 28 '21 at 17:35
  • Well, you're only `tick`ing 10; you still got 4989 to go :). So, if you want it to take less time, `tick` more. If you `tick(5000)`, the timeout will have fired on the next line. – Heretic Monkey Jan 28 '21 at 17:46
  • Sorry, but as per your comment ``` if the latter is less than the former, it runs the code.``` Isn't that mean that the ```tick``` that takes 10ms as compared to 4999ms of ```setTimeout``` Here latter is 10ms and it is less than 4999ms. The content inside setTimeout should execute just after 10ms of a tick? and the overall execution of spec should be less? (Attached screenshot) – Dolly Jan 28 '21 at 18:11
  • You can think of `tick` as "move time forwards N milliseconds". Does that clear it up for you? – Kraylog Jan 28 '21 at 18:17
  • 2
    No, the number sent to `setTimeout` is not changed by `tick`. Think of `tick` as moving the arms on an old analog clock ⏱. The distance `setTimeout` needs to go is the same: 4999 ms. When you run the test, everything starts at 0 and moves forward normally. If you call `tick`, it moves the hands forward that many milliseconds. So `tick(10)` moved it 10 ms forward, then kept ticking. `setTimeout` thought 10 ms went by, so it only needed to wait another 4989 ms. If you called `tick(4998)`, `setTimeout` would only wait 1 ms before triggering. – Heretic Monkey Jan 28 '21 at 18:22
  • @Dolly Deleting and adding the same comment repeatedly is not a good way of getting my attention. I saw your message the first time. I didn't have time to respond then, and I don't now. Please respect my time (volunteered as it is). – Heretic Monkey Jan 29 '21 at 16:35

0 Answers0