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

Coverage of untested files not collected #90

Open
marekdedic opened this issue Dec 14, 2021 · 8 comments
Open

Coverage of untested files not collected #90

marekdedic opened this issue Dec 14, 2021 · 8 comments
Labels
question Further information is requested

Comments

@marekdedic
Copy link

Hi,
in my configuration, I am testing svelte files written in typescript. In my jest.config.json I have:

{
  "collectCoverage": true,
  "collectCoverageFrom": [
    "src/**/*.svelte",
    "src/**/*.js",
    "src/**/*.ts"
  ],
  "coverageDirectory": "coverage",
  "coverageProvider": "babel",
  "projects": [
    {
      "displayName": "frontend",
      "preset": "ts-jest/presets/default",
      "testEnvironment": "jsdom",
      "moduleNameMapper": {
        "^.+\\.scss$": "identity-obj-proxy"
      },
      "transform": {
        "^.+\\.svelte$": [
          "svelte-jester",
          {
            "preprocess": "__tests__/frontend/svelte.config.js"
          }
        ],
        "^.+\\.js$": ["babel-jest", { "configFile": "./__tests__/frontend/babel.config.js" }]
      }
    }
  ]
}

With this, coverage is not collected from untested svelte files - I do get coverage of any svelte file that has a test, however, files without any tests are not listed in the coverage report.

I am using [email protected] and [email protected] - I've found similar issues here, but they should've been fixed with these versions...

@marekdedic marekdedic changed the title coverage of untested files not collected Coverage of untested files not collected Dec 14, 2021
@rvisharma
Copy link

rvisharma commented Dec 18, 2021

note - this is a hack/workaround 🔨

@marekdedic - was stuck with same issue, couldn't find a proper solution for this, but was able to find a workaround for it based on this - jestjs/jest#1211 (comment)

the linked issue, creates a file at runtime and with all require statements, instead i added a dummy spec file coverage.spec.js with below content, that basically traverses all *.svelte files and requires all of them

const glob = require('glob');
const cwd = require('path').join(process.cwd(), 'app', 'modules');
const pattern = '*.svelte';

glob
  .sync(pattern, { cwd, matchBase: true, nosort: true })
  .forEach((file) => require(file));

@sebastianrothe sebastianrothe added the question Further information is requested label Feb 1, 2022
@sebastianrothe
Copy link
Collaborator

sebastianrothe commented Feb 1, 2022

Can you try to change your coverageProvider to v8?

I think you have to set this as well: moduleFileExtensions: ["ts", "svelte", "js"],

Can you provide a sample repo, where the bug surfaces?

@marekdedic
Copy link
Author

Hi,I have previously tried both of these, but not both of them at once, which works! Switching back to babel however makes the tests error out completely :/

You can see it in action on this branch of my repo: https://github.com/skaut/shared-drive-mover/tree/svelte-jester-coverage-test

@tobiasbueschel
Copy link

+1

I also get the same error:

Running coverage on untested files...Failed to collect coverage from /path/to-app/src/components/SomeComponent/index.svelte
ERROR: Cannot find module 'file:///path/to-app/svelte.config.js'
Require stack:
- /path/to-app/node_modules/svelte-jester/dist/transformer.cjs
- /path/to-app/node_modules/@jest/reporters/node_modules/jest-util/build/requireOrImportModule.js
- /path/to-app/node_modules/@jest/reporters/node_modules/jest-util/build/index.js
- /path/to-app/node_modules/@jest/reporters/node_modules/@jest/transform/build/shouldInstrument.js
- /path/to-app/node_modules/@jest/reporters/node_modules/@jest/transform/build/ScriptTransformer.js
- /path/to-app/node_modules/@jest/reporters/node_modules/@jest/transform/build/index.js
- /path/to-app/node_modules/@jest/reporters/build/generateEmptyCoverage.js
- /path/to-app/node_modules/@jest/reporters/build/CoverageWorker.js
- /path/to-app/node_modules/jest-worker/build/workers/processChild.js
STACK: Error: Cannot find module 'file:///path/to-app/svelte.config.js'
Require stack:
- /path/to-app/node_modules/svelte-jester/dist/transformer.cjs
- /path/to-app/node_modules/@jest/reporters/node_modules/jest-util/build/requireOrImportModule.js
- /path/to-app/node_modules/@jest/reporters/node_modules/jest-util/build/index.js
- /path/to-app/node_modules/@jest/reporters/node_modules/@jest/transform/build/shouldInstrument.js
- /path/to-app/node_modules/@jest/reporters/node_modules/@jest/transform/build/ScriptTransformer.js
- /path/to-app/node_modules/@jest/reporters/node_modules/@jest/transform/build/index.js
- /path/to-app/node_modules/@jest/reporters/build/generateEmptyCoverage.js
- /path/to-app/node_modules/@jest/reporters/build/CoverageWorker.js
- /path/to-app/node_modules/jest-worker/build/workers/processChild.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at /path/to-app/node_modules/svelte-jester/dist/transformer.cjs:77:141
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Failed to collect coverage from /path/to-app/src/components/_layout.svelte
ERROR: Cannot find module 'file:///path/to-app/svelte.config.js'
Require stack:
//...

Here is my jest config - v8 was unfortunately not an option for me as it outputs an incorrect test coverage for my Svelte components:

import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
  preset: 'ts-jest',
  testEnvironment: 'jsdom',
  verbose: true,
  testMatch: ['<rootDir>/src/**/*.test.(js|ts)'],
  transform: {
    '^.+\\.svelte$': [
      'svelte-jester',
      {
        preprocess: true
      }
    ],
    '\\.svg$': '<rootDir>/transformSvg.js',
    '^.+\\.(ts|js)$': 'ts-jest',
    '.+\\.(css|styl|less|sass|scss)$': 'jest-css-modules-transform'
  },
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  moduleFileExtensions: ['js', 'ts', 'svelte'],
  moduleDirectories: ['node_modules', '<rootDir>/src'],
  coverageProvider: 'babel', // v8 is still experimental and outputs incorrect coverage stats for us
  collectCoverageFrom: ['<rootDir>/src/**/*.(svelte|ts)', '!<rootDir>/src/**/*.stories.svelte'],
  coveragePathIgnorePatterns: ['\\.svg$', '/node_modules/'],
};

export default config;

Once I add the untested file to a test case, the error for it disappears => @rvisharma thanks for your workaround!

@raurir
Copy link

raurir commented Jun 22, 2022

This issue remains, replicated and documented here: https://github.com/raurir/svelte-code-linting#testing

@cabreraalex
Copy link

+1 having same issue

@cooperwalbrun
Copy link
Contributor

cooperwalbrun commented Aug 21, 2022

I am also experiencing the same issue that @tobiasbueschel and @raurir mentioned, though I am not using Babel in my setup.

Here is my jest.config.cjs:

module.exports = {
  moduleFileExtensions: ['js', 'ts', 'svelte'],
  transform: {
    '^.+\\.svelte$': [
      'svelte-jester',
      {
        preprocess: true
      }
    ],
    '^.+\\.ts$': 'ts-jest'
  },
  coverageReporters: ['json', 'lcov', 'text', 'clover', 'html'],
  collectCoverageFrom: ['src/**/*.{ts,svelte}', '!src/*.d.ts']
};

I get the same error message:

STACK: Error: Cannot find module 'file:///path/to/my/project/svelte.config.js'

Edit: I added coverageProvider: "v8" to my jest.config.cjs, and it appears to be working fine now. I am using v2.3.2 of svelte-jester if it helps anyone else.

@ritchieanesco
Copy link

ritchieanesco commented Mar 3, 2023

I've bumped the jest version from 27.5.1 to 29.4.3 and now also getting the error

STACK: Error: Cannot find module 'file:///path/to/my/project/svelte.config.js'

when trying to include --coverage argument.

I get the above error message when using babel for coverageProvider

However when using v8 i get

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at /Users/<user>/w/xplore-ui/node_modules/@jest/reporters/build/CoverageReporter.js:507:37

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

No branches or pull requests

8 participants