4

Here are codes:

// component
   export class MockDateComponent implements OnInit {
     date: number;
     // ...
     initDate() {
       this.date = new Date().getTime()
     }
   } 

// spec
describe('...', () => {
  // ...
  it('should init date with current timestamp', () => {
    jasmine.clock().mockDate(new Date(1546263001000));
    component.initDate();
    expect(component.date).toBe(1546263001000);
  });
})

Codes above will pass. But whilte I add code below to specfile, it throw errors Error: Jasmine Clock was unable to install over custom global timer functions. Is the clock already installed? and I don't know why.

describe('...', () => {
  // ...
  beforeEach(() => {
    // ...
    jasmine.clock().install();
  });
  afterEach(() => {
    jasmine.clock().uninstall();
  });
  it('should init date with current timestamp', () => {
    // codes inner are the same
  });
})

The project was created by Angular Cli, here are version info:

@angular/common v6.1.10`
Angular CLI: 6.0.3
Node: 11.1.0
OS: darwin x64

Thank you for your answer!

ishowman
  • 61
  • 5
  • 1
    Have you checked this link? https://stackoverflow.com/questions/39600819/conflict-between-zone-js-and-jasmines-clock It says to uninstall first in `beforeEach` and then do install/uninstall – Amit Chigadani Jan 01 '19 at 11:38
  • I've tried that, it solve the error I mentioned above, but uninstall before install looks quite strange. What's more, the solution of your link throw new Error `Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.` – ishowman Jan 01 '19 at 13:07
  • I do not use timeout or interval in codes, but it seems that this is an error related to them. Jasmine really makes me confused – ishowman Jan 01 '19 at 13:10

0 Answers0