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

[Question] Testing & (Mock) Classes #84

Open
johannesschobel opened this issue Jun 3, 2019 · 4 comments
Open

[Question] Testing & (Mock) Classes #84

johannesschobel opened this issue Jun 3, 2019 · 4 comments

Comments

@johannesschobel
Copy link

Dear @xmlking ,

thanks again for this really (!) awesome (!) starter package. This is a piece of art 😆

I recently added my own package to the libs folder and it worked like a charm. Then i started to write tests. In this context, however, i stumbled upon a few questions. Maybe you can help me with the latter?

1) Running tests via IDE:

Consider, for example, the following "dummy" test-case:

describe('Something', () => {
  it('should be ok', () => {
    expect(true).toBeTruthy();
  });
});

Obviously, this test should pass. As I use IntelliJ WebStorm, i see the green "run" buttons next to describe() and it() to specifically run one specific test or an entire suite. However, when clicking this button, it errors with:

ts-jest[config] (WARN) TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):

if i run the test via console

ng test libraryName

it runs all tests and they pass.

2) Configuration & Schemantics

When creating a new lib in the project via ng g lib NAME it automatically modifies the .\angular.json file. In this context, it creates a key in projects.NAME, where some basic configuration stuff is located.
However, i noticed, that in my "own generated" libs the key projects.NAME.architect.test.options.setupFile is missing. Further, the referenced file (e.g., libs/NAME/src/test-setup.ts) is also missing / not generated!

3) "Hiding" Test Classes in Packages

In order to test certain functionality, i created some dummy classes (e.g,. a Book and Author class). Respective classes are, for example, located in libs/NAME/test/mocks/book.ts.

Of course, these classes are only vital for this specific library and does not need to be available in other. How can i "hide" them in other packages (e.g., disable the "intelli sense" auto-completion) for such "test classes"? Is this possible?

Thank you so much for this awesome project! You rock!
All the best from Germany

@xmlking
Copy link
Owner

xmlking commented Jun 3, 2019

1) Running tests via IDE:

this is just a warning, your tests are passing, so you can ignore this warning...

image

if you really want to suppress this warning, add following globals block to lib specific jest.config.js or root jest.config.js

module.exports = {
  name: 'tree',
  preset: '../../jest.config.js',
  transform: {
    '^.+\\.[tj]sx?$': 'ts-jest'
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
  coverageDirectory: '../../coverage/libs/tree',

  globals: {
    'ts-jest': {
      diagnostics: {
        ignoreCodes: 'TS151001'
      }
    }
  }
};

2) Configuration & Schemantics

setupFile is not mandatory for all lib modules. It only required for angular libs, that need special plumbing i.e., jest-preset-angular
Note that, with resent release of nx 8.0.0 we can generate three types of lib modules

  1. Shared neutral libs, used to keep shared angular/nestjs code
ng g @nrwl/workspace:lib models     --tags=utils --defaults -d
  1. Angular libs
ng g lib home --routing
ng g lib core --tags=core-module --defaults -d
# same as about. @nrwl/angular is the `defaultCollection`
ng g @nrwl/angular:lib core --tags=core-module --defaults -d
  1. Publishable angular libs, which generate extra package.json in libs directory.
ng g lib ContextMenu --tags=public-module --publishable=true --defaults -d

see different types of lib modules in my repo, and PLAYBOOK.md

3) "Hiding" Test Classes in Packages

I am not sure the best solution for this.
may be try to name those files with .spec.js suffix or , keep all mocks in a folder __fixtures__ or __mocks__ and mark directory as Test Source Root in WebStorm

image

https://jestjs.io/docs/en/manual-mocks

@johannesschobel
Copy link
Author

Dear @xmlking ,
wow, thanks a lot for your fast and detailled answer..

1) Running Tests in IDE ( solved 👍 )

I already found this solution, but thought it may not be the proper way to fix it. However, when "hiding" this error (with your proposed solution) it raised another error.
The issue was described here jestjs/jest#8118 and is a bug with IntelliJ. It was, however, fixed in the latest EAP (early access). Installing the latest version fixed it for me!

2) Configuration & Schematics (solved 👍 )

I already noticed the different library versions - thanks for pointing / clearing this out..

3) "Hide" Test Classes ( 🤔 )

That was my original approach as well - however, i marked the entire /libs/NAME/tests folder as a Test Sources Root). Still, these models show up in IntelliSense Autocomplete 🤔 Not sure if it is possible to "hide" them..

Do you have a paypal account where i can donate a small amount of USD for your awesome help!?

All the best

@johannesschobel
Copy link
Author

johannesschobel commented Jun 4, 2019

3) "Hide" Test Classes

closest thing i get, is as follows:
In my /libs/NAME/tests folder, have a dedicated __mocks__ folder and put all classes required for testing in there.. Then mark the /libs/NAME/tests folder as Testing Root and the /libs/NAME/tests/__mocks__ folder as Excluded.

This way, for example, the /libs/NAME/tests/__mocks__/models/book.model.ts model, however, still can be imported in normal source code files, However, it does not get "auto-imported". In fact, it does not show up in the normal Add Import Statement options!

However, when defining them as Excluded, IntelliJ automatically warns you if you want to edit this file. Further, paths for import statements are not automatically adjusted and so on..

@xmlking
Copy link
Owner

xmlking commented Jun 4, 2019

@johannesschobel thank you for further researching those issues.
best thing you can help is validate new changes I had been pushing and letting me know if anything breaks...

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

No branches or pull requests

2 participants