0

When i running Jest test for LWC in azure pipelines i got strange error


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)

This is my 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'
      }
    ]
  ]
}; 

In Azure Devops im running jest test on ubuntu-latest image Node.js is 19.9.0 and Npm is in 9.6.3 version

When i run same test and same config i got 100% coverage and all tests are passed What can be diff between this test run ?

Cezu
  • 82
  • 10

1 Answers1

0

You shouldn’t need at the modulePaths proprieties unless you have distinct path. Also it looks like you are missing the import in the config file.

const { jestConfig } = require('@salesforce/sfdx-lwc-jest/config');
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
CowboyCoder
  • 61
  • 2
  • 7