nyc icon indicating copy to clipboard operation
nyc copied to clipboard

How to use istanbul with Serverless?

Open gtamas opened this issue 2 years ago • 2 comments

This is not a bug report, just a question. Tried stackoverflow, but nobody replied.

I'm using Serverless famework and its offline plugin to start up my endpoints on localhost. This starts an offline server.

My code is written in Typescript.

Then I run integration tests with coverage like so:

.nycrc

  "extends": "@istanbuljs/nyc-config-typescript",
    "extension": [
      ".ts"
    ],
    "exclude": [
      "**/*.d.ts",
      "**/*.spec.ts"
    ],
    "reporter": [
      "html",
      "lcov",
      "text-summary"
    ],
    "lines": 90,
    "branches": 90,
    "functions": 90,
    "statements": 90,
    "report-dir": "coverage",
    "all": true  

.nyc-intrc

{
    "extends": "./.nycrc",
    "include": [
      "src/**/*.ts"
    ]
}

package.json

 "test:coverage:integration": "nyc --nycrc-path .nyc-intrc npm run test:integration",
"test:integration": "mocha -r ts-node/register/transpile-only -r source-map-support/register --recursive 'src/*/__tests__/*.spec.ts' --timeout 1500000"

This runs fine but I get an inaccurate report. Some files have test coverage, while others are reported having 0 coverage, even though that code was triggered and executed by the tests.

I mean, in one of the files I have a console.log statement. If I run nyc with unit tests, I get accurate results for this file. I can see the console.log has been executed 1 times.

But integration tests reports the same file as having 0 coverage.

Is this a bug? Or am I doing something wrong? Can I generate coverage reports of integration tests.

gtamas avatar Feb 17 '22 13:02 gtamas

I think this is not working because the integration tests don't directly execute some parts of the code, while unit tests do that.

But then how to instrument the entire code, so when I run integration tests I get accurate reports?

gtamas avatar Feb 17 '22 13:02 gtamas

@gtamas I was having you same issue but finally I fixed it.

In my case I need to replace recursive like --recursive \"**/**/**/*.spec.ts\".

For example adjusting for your case:

--recursive \"src/**/__tests__/*.spec.ts\"

carmenates09 avatar Feb 23 '22 00:02 carmenates09