[Vuejs]-How to exclude files for unit test in vue?

0👍

Your Jest configuration should use relative paths from your jest.config.js file. Adjust your collectCoverageFrom and coveragePathIgnorePatterns accordingly:

module.exports = {
  preset: '@vue/cli-plugin-unit-jest',
  collectCoverage: true,
  collectCoverageFrom: [
    'src/**/*.{js,vue}',
    '!src/main.js',
    '!src/components/fileDownloader/downloaderFile.js',
  ],
  coveragePathIgnorePatterns: [
    '/node_modules/',
    'src/components/fileDownloader/downloaderFile.js',
  ]
};

and for sonar, adjust your paths in sonar.exclusions and sonar.coverage.exclusions based on the location of your sonar-project.properties file:

sonar.exclusions=**/node_modules/**,**/tests/unit/**,**/coverage/lcov-report/**,**/src/models/**,**/src/components/fileDownloader/downloaderFile.js
sonar.coverage.exclusions=**/src/components/fileDownloader/downloaderFile.js

Hope this helps 🌷

0👍

In your jest.config.js file, you can specify which files or directories you want to exclude from unit tests using the testPathIgnorePatterns configuration option.

module.exports = {
  // Other Jest configuration options...

  testPathIgnorePatterns: [
    '/node_modules/',  // Exclude files in the node_modules directory
    '/dist/',          // Exclude files in the dist directory
    '/some-directory/',// Exclude files in a specific directory
    'filenameToExclude\\.js', // Exclude a specific file by name
  ],
};

After configuring the exclusion patterns, when you run your unit tests using the npm test or yarn test command, Jest will exclude the specified files and directories from being considered for tests.

Leave a comment