svelte-jester icon indicating copy to clipboard operation
svelte-jester copied to clipboard

Coverage of untested files not collected

Open marekdedic opened this issue 2 years ago • 8 comments

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 avatar Dec 14 '21 08:12 marekdedic

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 - https://github.com/facebook/jest/issues/1211#issuecomment-237100888

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));

rvisharma avatar Dec 18 '21 20:12 rvisharma

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?

sebastianrothe avatar Feb 01 '22 23:02 sebastianrothe

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

marekdedic avatar Feb 02 '22 09:02 marekdedic

+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!

tobiasbueschel avatar Apr 25 '22 07:04 tobiasbueschel

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

raurir avatar Jun 22 '22 00:06 raurir

+1 having same issue

cabreraalex avatar Jul 26 '22 17:07 cabreraalex

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.

cooperwalbrun avatar Aug 21 '22 21:08 cooperwalbrun

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

ritchieanesco avatar Mar 03 '23 02:03 ritchieanesco