salesforce / sfdx-lwc-jest

Run Jest against LWC components in SFDX workspace environment
MIT License
165 stars 82 forks source link

Add support for `jest.config.mjs` and `jest.config.cjs` #313

Closed lukethacoder closed 1 year ago

lukethacoder commented 1 year ago

Description

Currently sfdx-lwc-jest checks only for a jest.config.js file, but jest supports jest.config.js|ts|mjs|cjs|json (reference).

Steps to Reproduce

  1. Create a jest.config.cjs file
  2. Add your custom config (don't forget to extend the
  3. Run sfdx-lwc-jest
  4. Custom jest config file is not picked up, no custom config is used

jest.config.cjs

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

/** @type {import('jest').Config} */
const config = {
  ...jestConfig,
  modulePathIgnorePatterns: ['__docs__', '__example__'],
}

module.exports = config

or

jest.config.mjs

import { jestConfig } from '@salesforce/sfdx-lwc-jest/config'

/** @type {import('jest').Config} */
const config = {
  ...jestConfig,
  modulePathIgnorePatterns: ['__docs__', '__example__'],
}

export default config

Expected Results

Allow sfdx-lwc-jest to use .cjs and .mjs jest config extensions.

Actual Results

jest.config.cjs and jest.config.mjs config files are ignored

Version

Possible Solution

Check for .mjs and .cjs jest config files here

https://github.com/salesforce/sfdx-lwc-jest/blob/bbdcfb1e5a5625108b9fb31550fad3f818ed4fc7/src/utils/test-runner.js#L62

// Provide default configuration when none is present at the project root.
const hasCustomConfig = fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.js'))
    || fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.mjs'))
    || fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.cjs'));
nolanlawson commented 1 year ago

Resolved by https://github.com/salesforce/sfdx-lwc-jest/pull/314