LWC Jest测试在Azure Devops管道中显示错误

9avjhtql  于 2023-08-01  发布在  Jest
关注(0)|答案(1)|浏览(102)

当我在Azure管道中运行LWC的Jest测试时,我得到了奇怪的错误

Cannot find module 'c/myComponent' from 'force-app/main/default/lwc/myComponent/__tests__/myComponent.test.js'

  1 | import { createElement } from 'lwc';
> 2 | import myComponent from 'c/myComponent';
    | ^
  3 | import fetchTopics from '@salesforce/apex/MyComponentController.getAllContentTopics';
  4 |
  5 | const mockGetTopics = require('./data/getTopics.json');

  at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
  at Object.<anonymous> (force-app/main/default/lwc/myComponent/__tests__/myComponent.test.js:2:1)

字符串
这是我的jest.config.js

module.exports = {
  ...jestConfig,
  coverageReporters: ['clover', 'json', 'text', 'lcov', 'cobertura'],
  modulePathIgnorePatterns: ['/.localdevserver'],
  modulePaths: ["<rootDir>"],
  reporters: [
    'default',
    [
      'jest-junit',
      {
        outputDirectory: 'tests',
        outputName: 'test-results-lwc.xml'
      }
    ]
  ]
};


在Azure Devops中,我在ubuntu上运行jest测试-最新图像Node.js是19.9.0,Npm是9.6.3版本
当我运行相同的测试和相同的配置,我得到了100%的覆盖率,所有的测试都通过了什么可以是这个测试运行之间的差异?

xdnvmnnf

xdnvmnnf1#

除非你有明确的路径,否则你不需要查看modulePaths属性。另外,看起来你在配置文件中缺少导入。

const { jestConfig } = require('@salesforce/sfdx-lwc-jest/config');

字符串

相关问题