How to use validateExtensionsToTreatAsEsm method in Jest

Best JavaScript code snippet using jest


Source:normalize.js Github


Full Screen

...499 'Running all tests instead.'500 )501 );502};503function validateExtensionsToTreatAsEsm(extensionsToTreatAsEsm) {504 if (!extensionsToTreatAsEsm || extensionsToTreatAsEsm.length === 0) {505 return;506 }507 function printConfig(opts) {508 const string = => `'${ext}'`).join(', ');509 return _chalk().default.bold(`extensionsToTreatAsEsm: [${string}]`);510 }511 const extensionWithoutDot = extensionsToTreatAsEsm.some(512 ext => !ext.startsWith('.')513 );514 if (extensionWithoutDot) {515 throw createConfigError(` Option: ${printConfig(516 extensionsToTreatAsEsm517 )} includes a string that does not start with a period (${_chalk().default.bold(518 '.'519 )}).520 Please change your configuration to ${printConfig(521 => (ext.startsWith('.') ? ext : `.${ext}`))522 )}.`);523 }524 if (extensionsToTreatAsEsm.includes('.js')) {525 throw createConfigError(526 ` Option: ${printConfig(527 extensionsToTreatAsEsm528 )} includes ${_chalk().default.bold(529 "'.js'"530 )} which is always inferred based on ${_chalk().default.bold(531 'type'532 )} in its nearest ${_chalk().default.bold('package.json')}.`533 );534 }535 if (extensionsToTreatAsEsm.includes('.cjs')) {536 throw createConfigError(537 ` Option: ${printConfig(538 extensionsToTreatAsEsm539 )} includes ${_chalk().default.bold(540 "'.cjs'"541 )} which is always treated as CommonJS.`542 );543 }544 if (extensionsToTreatAsEsm.includes('.mjs')) {545 throw createConfigError(546 ` Option: ${printConfig(547 extensionsToTreatAsEsm548 )} includes ${_chalk().default.bold(549 "'.mjs'"550 )} which is always treated as an ECMAScript Module.`551 );552 }553}554async function normalize(555 initialOptions,556 argv,557 configPath,558 projectIndex = Infinity559) {560 var _options$haste, _argv$_;561 const {hasDeprecationWarnings} = (0, _jestValidate().validate)(562 initialOptions,563 {564 comment: _utils.DOCUMENTATION_NOTE,565 deprecatedConfig: _Deprecated.default,566 exampleConfig: _ValidConfig.default,567 recursiveDenylist: [568 'collectCoverageOnlyFrom', // 'coverageThreshold' allows to use 'global' and glob strings on the same569 // level, there's currently no way we can deal with such config570 'coverageThreshold',571 'globals',572 'moduleNameMapper',573 'testEnvironmentOptions',574 'transform'575 ]576 }577 );578 let options = normalizePreprocessor(579 normalizeReporters(580 normalizeMissingOptions(581 normalizeRootDir((0, _setFromArgv.default)(initialOptions, argv)),582 configPath,583 projectIndex584 )585 )586 );587 if (options.preset) {588 options = await setupPreset(options, options.preset);589 }590 if (!options.setupFilesAfterEnv) {591 options.setupFilesAfterEnv = [];592 }593 if (594 options.setupTestFrameworkScriptFile &&595 options.setupFilesAfterEnv.length > 0596 ) {597 throw createConfigError(` Options: ${_chalk().default.bold(598 'setupTestFrameworkScriptFile'599 )} and ${_chalk().default.bold(600 'setupFilesAfterEnv'601 )} cannot be used together.602 Please change your configuration to only use ${_chalk().default.bold(603 'setupFilesAfterEnv'604 )}.`);605 }606 if (options.setupTestFrameworkScriptFile) {607 options.setupFilesAfterEnv.push(options.setupTestFrameworkScriptFile);608 }609 options.testEnvironment = (0, _jestResolve().resolveTestEnvironment)({610 requireResolveFunction: require.resolve,611 rootDir: options.rootDir,612 testEnvironment:613 options.testEnvironment ||614 require.resolve(_Defaults.default.testEnvironment)615 });616 if (!options.roots && options.testPathDirs) {617 options.roots = options.testPathDirs;618 delete options.testPathDirs;619 }620 if (!options.roots) {621 options.roots = [options.rootDir];622 }623 if (624 !options.testRunner ||625 options.testRunner === 'circus' ||626 options.testRunner === 'jest-circus'627 ) {628 options.testRunner = require.resolve('jest-circus/runner');629 } else if (options.testRunner === 'jasmine2') {630 options.testRunner = require.resolve('jest-jasmine2');631 }632 if (!options.coverageDirectory) {633 options.coverageDirectory = path().resolve(options.rootDir, 'coverage');634 }635 setupBabelJest(options); // TODO: Type this properly636 const newOptions = {..._Defaults.default};637 if (options.resolver) {638 newOptions.resolver = (0, _utils.resolve)(null, {639 filePath: options.resolver,640 key: 'resolver',641 rootDir: options.rootDir642 });643 }644 validateExtensionsToTreatAsEsm(options.extensionsToTreatAsEsm);645 if (options.watchman == null) {646 options.watchman = _Defaults.default.watchman;647 }648 const optionKeys = Object.keys(options);649 optionKeys.reduce((newOptions, key) => {650 // The resolver has been resolved separately; skip it651 if (key === 'resolver') {652 return newOptions;653 } // This is cheating, because it claims that all keys of InitialOptions are Required.654 // We only really know it's Required for oldOptions[key], not for oldOptions.someOtherKey,655 // so oldOptions[key] is the only way it should be used.656 const oldOptions = options;657 let value;658 switch (key) {...

Full Screen

Full Screen

Jest Testing Tutorial

LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.


  1. What is Jest Framework
  2. Advantages of Jest - Jest has 3,898,000 GitHub repositories, as mentioned on its official website. Learn what makes Jest special and why Jest has gained popularity among the testing and developer community.
  3. Jest Installation - All the prerequisites and set up steps needed to help you start Jest automation testing.
  4. Using Jest with NodeJS Project - Learn how to leverage Jest framework to automate testing using a NodeJS Project.
  5. Writing First Test for Jest Framework - Get started with code-based tutorial to help you write and execute your first Jest framework testing script.
  6. Jest Vocabulary - Learn the industry renowned and official jargons of the Jest framework by digging deep into the Jest vocabulary.
  7. Unit Testing with Jest - Step-by-step tutorial to help you execute unit testing with Jest framework.
  8. Jest Basics - Learn about the most pivotal and basic features which makes Jest special.
  9. Jest Parameterized Tests - Avoid code duplication and fasten automation testing with Jest using parameterized tests. Parameterization allows you to trigger the same test scenario over different test configurations by incorporating parameters.
  10. Jest Matchers - Enforce assertions better with the help of matchers. Matchers help you compare the actual output with the expected one. Here is an example to see if the object is acquired from the correct class or not. -

|<p>it('check_object_of_Car', () => {</p><p> expect(newCar()).toBeInstanceOf(Car);</p><p> });</p>| | :- |

  1. Jest Hooks: Setup and Teardown - Learn how to set up conditions which needs to be followed by the test execution and incorporate a tear down function to free resources after the execution is complete.
  2. Jest Code Coverage - Unsure there is no code left unchecked in your application. Jest gives a specific flag called --coverage to help you generate code coverage.
  3. HTML Report Generation - Learn how to create a comprehensive HTML report based on your Jest test execution.
  4. Testing React app using Jest Framework - Learn how to test your react web-application with Jest framework in this detailed Jest tutorial.
  5. Test using LambdaTest cloud Selenium Grid - Run your Jest testing script over LambdaTest cloud-based platform and leverage parallel testing to help trim down your test execution time.
  6. Snapshot Testing for React Front Ends - Capture screenshots of your react based web-application and compare them automatically for visual anomalies with the help of Jest tutorial.
  7. Bonus: Import ES modules with Jest - ES modules are also known as ECMAScript modules. Learn how to best use them by importing in your Jest testing scripts.
  8. Jest vs Mocha vs Jasmine - Learn the key differences between the most popular JavaScript-based testing frameworks i.e. Jest, Mocha, and Jasmine.
  9. Jest FAQs(Frequently Asked Questions) - Explore the most commonly asked questions around Jest framework, with their answers.

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?