1. Front background

Path aliases have been configured in Webpack

    alias: {
      The '@': path.resolve(__dirname, 'src')}Copy the code

Attempt to use it directly in test cases

import { BaseControl } from '@/prototype/combination.js';
Copy the code

However, an error was reported. The reason is that Jest cannot recognize aliases defined in Webpack. This must also be defined in the jest configuration file.

2. Configure the code

  moduleNameMapper: {
    '^ @ / (. *) $': '<rootDir>/src/$1'.'components/(.*)$': '<rootDir>/src/components/$1',},rootDir: path.resolve(__dirname, '. '),
Copy the code

3. Configure resolution

  • The concept of grouping, which sets a portion of content to a group (.*) that reads $1 in a special way.
  • @ maps to./ SRC
  • Components maps to./ SRC /components