angular-ngrx-nx-realworld-example-app icon indicating copy to clipboard operation
angular-ngrx-nx-realworld-example-app copied to clipboard

Jest tests fail

Open redzimskidev opened this issue 3 years ago • 2 comments

I'm trying to run unit tests in this repository but they fail with some jest errors I can't solve. Reproduction steps:

git clone https://github.com/stefanoslig/angular-ngrx-nx-realworld-example-app.git ./ npm install npm install -g nx nx run-many --target=test --all

The result of running the tests is the following error:

 ● Test suite failed to run

TypeError: Cannot read properties of undefined (reading 'html')

  at new JSDOMEnvironment (../../../node_modules/jest-environment-jsdom/build/index.js:72:44)
  at async TestScheduler.scheduleTests (../../../node_modules/@jest/core/build/TestScheduler.js:317:13)
  at async runJest (../../../node_modules/@jest/core/build/runJest.js:407:19)
  at async _run10000 (../../../node_modules/@jest/core/build/cli/index.js:339:7)
  at async runCLI (../../../node_modules/@jest/core/build/cli/index.js:190:3)`

After some googling I fixed this with running this command:

npm install --save-dev jest-environment-jsdom

But then I get errors like this:

● Test suite failed to run
                                                                                                                                
Jest encountered an unexpected token                                                                                            
                                                                                                                                
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.                                                                                        
                                                                                                                                
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. 

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.    
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

C:\Users\redzi\Documents\repos\angular-ngrx-nx-realworld-example-app-main\node_modules\rxjs\dist\esm5\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { Observable } from './internal/Observable';
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

  at Runtime.createScriptFromCode (../../node_modules/jest-runner/node_modules/jest-runtime/build/index.js:1796:14)
  at Object.<anonymous> (../../node_modules/@angular/core/fesm2020/core.mjs:7:69)

FAIL   conduit  apps/conduit/src/app/layout/footer/footer.component.spec.ts`

I presume it's an issue with something I'm doing or my setup since I've noticed CI/CD pipelines configured which would report this issue, but I've tried it on two PCs with the same results. What am I doing wrong?

redzimskidev avatar Jul 25 '22 18:07 redzimskidev

I've managed to confirm the commit that broke this behavior was this one: https://github.com/stefanoslig/angular-ngrx-nx-realworld-example-app/commit/47fb23efd82d090fedd20624d44ac37b0260b045 with Jest upgraded to 28. Tests before that run, albeit some are failing for other reasons.

redzimskidev avatar Jul 26 '22 17:07 redzimskidev

Hi @Marred . Thanks so much for your investigation. In the next days I'm planning to work more on the unit tests(fix them and increase the coverage) and I will fix this bug as well.

Thanks for reporting it!

stefanoslig avatar Jul 30 '22 19:07 stefanoslig

The tests are running again. Now I need to fix them. Working on it.

stefanoslig avatar Aug 08 '22 20:08 stefanoslig