Search code examples
angularstubangular-library

Angular library include stubs and testing helper files


I have a simple Angular library for internal use with some pipes and directives, the library is used in several projects. When the developers need to write the unit testing, they are creating a stub file for the pipe. They have the same stub in 5 projects duplicate always the same code.

What I want to do is provide this stub file for the pipe or directive, something similar to what does the RouterTestingModule, including a test module or include the stub itself inside my library, but I don't have any clue of how to do it.

Should I have to create a LibraryTestingModule and include it inside the main public_api and also include it in LibraryModule, that make sense?


Solution

  • My workaround to solve this was the following:

    At the same level of src path, create a testing folder. Inside it include the next files

    /src
    /testing
    ┣ /src
    ┃ ┣ /stubs // my testing files here
    ┃ ┗ index.ts
    ┣ index.ts // barrel file
    ┣ package.json
    ┣ public_api.ts // similar to index.ts, entry point for public API of the package
    ┗ library-testing.module.ts // Angular module
    

    For me, this was the trick, including the following lines inside the package.json

    {
      "ngPackage": {
        "lib": {
          "entryFile": "public_api.ts",
        }
      }
    }
    

    And with all these, now I can use my-lib/some-pipe.ts and in testing files, now I have access to my-lib/testing/some-stub-pipe.ts.