Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion]Support jest.useFakeTimers and fakeAsync() #520

Open
JiaLiPassion opened this issue Sep 30, 2020 · 6 comments
Open

[Discussion]Support jest.useFakeTimers and fakeAsync() #520

JiaLiPassion opened this issue Sep 30, 2020 · 6 comments

Comments

@JiaLiPassion
Copy link
Contributor

I created a PR in the angular repo angular/angular#39016 to support integration between fake timer APIs of jest and fakeAsync().

Any feedback is appreciate!


After enable this feature, calling jest.useFakeTimers() will make all test
run into fakeAsync() automatically.

beforeEach(() => {
    jest.useFakeTimers('modern');
  });
  afterEach(() => {
    jest.useRealTimers();
  });

  test('should run into fakeAsync() automatically', () => {
    const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
    expect(fakeAsyncZoneSpec).toBeTruthy();
  });

Also there are mappings between jest and zone APIs.

  • jest.runAllTicks() will call flushMicrotasks().
  • jest.runAllTimers() will call flush().
  • jest.advanceTimersByTime() will call tick()
  • jest.runOnlyPendingTimers() will call flushOnlyPendingTimers()
  • jest.advanceTimersToNextTimer() will call tickToNext()
  • jest.clearAllTimers() will call removeAllTimers()
  • jest.getTimerCount() will call getTimerCount()

For detail usage, please refer to the test file

https://github.com/angular/angular/pull/39016/files#diff-53fe8d9816dc08e106333ff822445d7fR103-R413

@ahnpnl
Copy link
Collaborator

ahnpnl commented Sep 30, 2020

I left a comment for that PR. In general, README here should be updated later related to that PR.

@Wykks
Copy link

Wykks commented Jan 15, 2021

This solve my issues with fakeAsync and rxjs. Thanks!

@markusmo
Copy link

I saw that this is already merged with Angular. But my issue is, that the example test provided in this thread does not work for me.

could it be reagarding my jest.config.js:

module.exports = {
    extensionsToTreatAsEsm: ['.ts'],
    globals: {
        'ts-jest': {
            useESM: true,
            tsconfig: '<rootDir>/src/tsconfig.spec.json',
            stringifyContentPathRegex: '\\.(html|svg)$',
        },
    },
    moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
    moduleNameMapper: {
        '@shared/(.*)': '<rootDir>/src/app/shared/$1',
    },
    transform: {
        '^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular',
    },
    testEnvironment: 'jsdom',
    setupFilesAfterEnv: ['<rootDir>/setup-jest.ts', 'jest-extended/all', 'jest-marbles'],
    reporters: ['jest-spec-reporter'],
    transformIgnorePatterns: [`<rootDir>/node_modules/(?!.*\\.mjs$|${['@angular', '@ngrx', 'd3'].join('|')})`],
};

@dedalusMohantyMa
Copy link

any updates on this?

@johncrim
Copy link
Contributor

I have tried this as well, and I'm pretty sure it doesn't work any more. Or at least, it doesn't work for ESM tests. You could try switching to CJS/non ESM mode and see if it works in that scenario with latest dependencies. Or you could just add fakeAsync() to your test methods.

@dedalusMohantyMa
Copy link

I already use fakeAsync everywhere, I was happy to get rid of it and create blocks where I can test components without it :-)
Seems like I have to investigate some time to reconfigure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants