question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not working unless used in combination with @angular-builders/jest

See original GitHub issue

Anybody else have problems with using this preset without the builder?

If I run ng test and use the builder it works, but if I run npx jest it fails to load the html files.

When I run ng test --showConfig and npx jest --showConfig they config files that are spit out are identical (except for a prettier setting and an extra moduleNameMapper for image files) so I don’t understand why this is failing.

Here’s my local jest.config.js file, not that it should matter:

module.exports = {
  "globals": {
    "ts-jest": {
      "tsConfig": "<rootDir>/public/tsconfig.spec.json",
      "stringifyContentPathRegex": "\\.html$",
      "astTransformers": [require.resolve("jest-preset-angular/InlineHtmlStripStylesTransformer.js")]
    }
  },  
  "preset": "jest-preset-angular",
  "transform": {
    '^.+\\.(ts|html)$': 'ts-jest',
    "^.+\\.js$": "babel-jest"
  },  
  "setupFilesAfterEnv": ["<rootDir>/public/setupJest.ts"],
  "testMatch": [
    "**/__tests__/**/*.+(ts|js)?(x)",
    "**/(*.)+(spec).+(ts|js)?(x)"
  ],
  "moduleFileExtensions": ["js", "json", "jsx", "node", "ts", "tsx", "html"],
  "moduleNameMapper": {
    "^src/(.*)$": "<rootDir>/src/$1",
    "^app/(.*)$": "<rootDir>/public/app/$1",
    "^modules/(.*)$": "<rootDir>/public/modules/$1",
    "^assets/(.*)$": "<rootDir>/public/assets/$1",
    "^environments/(.*)$": "<rootDir>/public/environments/$1"
  }
}

Here’s the extra module name mapper:

'\\.(jpg|jpeg|png)$':
      '<my root dir>/src/node_modules/@angular-builders/jest/src/jest-config/mock-module.js'

The reason why I am having this issue is because I have a PR into @angular-builders because, currently, it’s not respecting maxWorkers and I used to do this on my CI:

npx jest -w 2 which worked fine until I upgraded to jest-preset-angular 7. Then I had to start using the builder, but I can’t use -w 2, so I have to use -i which slows down my CI.

Not sure whose bug it is that I can’t get jest-preset-angular to work without also using the builder.

Here’s the relevant stuff:

@angular-builders/jest”: “^7.4.1”, “jest”: “^24.7.1”, “jest-preset-angular”: “^7.0.1”,

EDIT: A little further info, it seems to work fine actually, unless I’m I haven’t mocked components in my tests. It loads the html file for the component I’m testing directly, but if I load other components in my test for a given component, it fails to load the templates for those other components.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
PhiSpindlercommented, Jul 9, 2019

I think it was a problem with the jest cache.

Fix it by using npx jest --clearCache

0reactions
akmjenkinscommented, Apr 7, 2019

I can no longer reproduce this, I think I had some dependencies out of whack or something else. Very sorry for wasting your time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular CLI: “ng test” with Jest in 3 minutes - Just Jeb
Today I'm going to show you how to setup your Angular CLI workspace to work with Jest while keeping it clear of boilerplate...
Read more >
angular-builders/jest
Jest runner for Angular build facade. Allows ng test run with Jest instead of Karma. Latest version: 15.0.0, last published: 6 days ago....
Read more >
Angular CLI builders
A number of Angular CLI commands run a complex process on your code, such as linting, building, or testing. The commands use an...
Read more >
How to Set Up Angular Unit Testing with Jest
In this article, I will show you how to ditch Karma and Jasmine and use Jest as your unit testing framework and runner....
Read more >
Angular 12 setting up Unit test with JEST when running no ...
I faced the same problem, but fixed it by setting up jest using this schematic. https://github.com/briebug/jest-schematic#readme.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found