Jest - Simple tests are slow

I am using Jest to test an angular application and it takes a very long time to run simple tests and I cannot figure out why.

My Jest setup in package.json

:

"jest": {
  "modulePaths": [
    "<rootDir>/src",
    "<rootDir>/node_modules"
  ],
  "testPathIgnorePatterns": [
    ".git/.*",
    "node_modules/.*"
  ],
  "transformIgnorePatterns": [
    "node_modules/.*",
    ".*\\.js"
  ],
  "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.js",
  "preset": "jest-preset-angular",
  "testEnvironment": "jsdom",
  "testRegex": "src/app/.*\\.spec\\.ts$",
  "moduleFileExtensions": [
    "ts",
    "js",
    "json"
  ],
  "verbose": true,
  "cacheDirectory": ".jest-cache",
  "coveragePathIgnorePatterns": [
    ".*\\.(shim\\.ngstyle|ngfactory)\\.ts"
  ],
  "globals": {
    "ts-jest": {
      "tsConfigFile": "./tsconfig.json"
    },
    "__TRANSFORM_HTML__": true
  }
}

      

My Jest setup file:

'use strict';
require('core-js/es6/reflect');
require('core-js/es7/reflect');
require('zone.js');
require('zone.js/dist/proxy.js');
require('zone.js/dist/sync-test');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
require('jest-zone-patch');

const getTestBed = require('@angular/core/testing').getTestBed;
const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing').BrowserDynamicTestingModule;
const platformBrowserDynamicTesting = require('@angular/platform-browser-dynamic/testing')  .platformBrowserDynamicTesting;

getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
);

      

Here's my simple test:

fdescribe('RichTextEditorComponent', () => {
  it('should be fast', () => {
    expect(true).toBeTruthy();
  });
});

      

Does anyone know why it takes 9+ seconds? enter image description here

+33


source to share


2 answers


I am also using Jest in my Angular project and I am not sure if this is a good solution.

When you configure your test unit, you can use NO_ERRORS_SCHEMA

and you do not need to add all the nested components declarations

to compile the component you want to test.



beforeEach(async () => {
    return TestBed.configureTestingModule({
      declarations: [
        MyComponent
      ],
      schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents();
  });

      

Your tests with Jest are unit tests, so with this solution you will only be testing your component. If you want to test interoperability between components, you will run end-to-end tests with Protractor or Puppeteer.

+1


source


I think the answer should eventually come from the Angular team. The documentation for platformBrowserDynamicTesting is sparse ( https://angular.io/api/platform-browser-dynamic/testing/platformBrowserDynamicTesting ).



Perhaps platformBrowserDynamicTesting emulates a browser and loads the entire DOM for your application into memory. In this case, a reasonable 10 second increase in the speed of the Angular app (without any cached JavaScript). I may be misinterpreting this, but according to your reports, it looks like the real test runs in 6 milliseconds, which apparently should meet your "quick test" requirement. I would be interested to know how long the testing will take if you add another simple "must be fast 2" test. If the total is still less than 10 seconds, this indicates that your actual tests are taking very little time compared to deploying the Angular platformBrowserDynamicTesting utility.

0


source







All Articles