some errors about jasmine clock in Angular unit test

Viewed 1227

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!

0 Answers
Related