jest-community / vscode-jest

The optimal flow for Jest based testing in VS Code
MIT License
2.84k stars 295 forks source link

vscode jest configuration error #457

Closed JFGHT closed 5 years ago

JFGHT commented 5 years ago

Environment

  1. node -v: v10.13.0

  2. npm -v: 6.4.1 && yarn 1.16.0

  3. npm ls jest or npm ls react-scripts (if you haven’t ejected): @vue/cli-plugin-unit-jest@3.7.0 └── jest@23.6.0

  4. Operating system: Ubuntu 18.

Prerequisite

Steps to Reproduce

Create Vue application with Vue-cli (last version).

vue.ts file, the mainfile to run Vue's instance (affected code):

const i18n = new VueI18n({
  locale, // set locale
  fallbackLocale: 'en',
  messages: { }, // set locale messages
});

// Setting default language
import(/* webpackChunkName: "lang-[request]" */ `@/lang/${locale}.ts`)
  .then((langFile): void => {
    i18n.setLocaleMessage(locale, langFile.default[locale]);
  })
  .catch((e): void => {
    throw new Error(e);
  });

jest.config.js:

process.env.VUE_CLI_BABEL_TARGET_NODE = true;
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true;

module.exports = {
  moduleFileExtensions: [
    'ts',
    'tsx',
    'js',
    'jsx',
    'json',
    'vue',
  ],
  transform: {
    '^.+\\.vue$': 'vue-jest',
    '.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
    '^.+\\.tsx?$': 'ts-jest',
  },
  moduleNameMapper: {
    '^.+\\.svg': '<rootDir>/tests/transformers/svgTransform.js',
    '^@/(.*)$': '<rootDir>/src/$1',
    '\\.(css|less|sass|scss)$': 'identity-obj-proxy',
  },
  snapshotSerializers: [
    'jest-serializer-vue',
  ],
  testMatch: [
    // Each component's test
    '<rootDir>/(src/**/*.spec.(ts|tsx|js)|**/__tests__/*.(ts|tsx|js))',
  ],
  testURL: 'http://localhost',
  collectCoverage: true,
  coverageDirectory: 'artefacts/tests/jest/coverage',
  collectCoverageFrom: [
    'src/**/*.{ts,vue}',
    '!src/**/*shims*.ts',
    '!src/**/custom.d.ts',
    '!**/node_modules/**',
  ],
  coverageReporters: [
    'html',
    'json',
    'text-summary',
  ],
};

Relevant Debug Info

Configuration error:

Could not locate module @/lang/${locale}.ts mapped as: /home/xxx/src/lang/${locale}.ts.

Please check your configuration for these entries: { "moduleNameMapper": { "/^@\/(.*)$/": "/home/xxx/src/$1" }, "resolver": null }

Expected Behavior

Normal initialization, run test and start watch mode.

Actual Behavior

Show that error msg in output and get stuck at Start watching mode.

Worth to mention

I do have another jest.config.js file inside root/functions because I'm testing Google Cloud Functions as well.

The code:

// this config includes typescript specific settings
// and if you're not using typescript, you should remove `transform` property
module.exports = {
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
  testPathIgnorePatterns: ['lib/', 'node_modules/'],
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  testEnvironment: 'node',
  coveragePathIgnorePatterns: ['./tests/lib', './src/lib', './src/logging'],
  // rootDir: 'tests',
};
connectdotz commented 5 years ago

do you get a chance to set jest.pathToJest to yarn test per troubleshooting?

{
  "jest.pathToJest": "yarn test"
}
usulpro commented 5 years ago

I faced the similar issue with moduleNameMapper and after adding "jest.pathToJest": "yarn test" to config it was fixed!

Thanks @connectdotz

JFGHT commented 5 years ago

Seems to fix my issue. Thanks!