From 31daf779dfba6320c2ec065535f1314e6f0c8da2 Mon Sep 17 00:00:00 2001 From: Samy Ouyahia Date: Tue, 3 Sep 2024 09:37:24 +0200 Subject: [PATCH] Included setupTests.js in the jest-config package --- configs/jest-config/README.md | 38 +++++++++++++++++++++++++++++--- configs/jest-config/package.json | 5 +---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/configs/jest-config/README.md b/configs/jest-config/README.md index 87734030b..8eaea6487 100644 --- a/configs/jest-config/README.md +++ b/configs/jest-config/README.md @@ -28,9 +28,9 @@ And replace the `base` keyword with one of the following Jest configuration avai | `base` | Base configuration for any Node.Js project . | | `react` | Base configuration used for React-based projects. | -# Tests Setup -If you are using the [@monkvision/test-utils](https://github.com/monkvision/monkjs/tree/main/configs/test-utils) -package, and you wish to enjoy Jest automocking with the provided mocks, you can use the following test set up file : +# External Developments +If you are using MonkJs packages in an app outside the MonkJs monorepository, you might want to use our predefined +tests setup file to avoid ECMAScript Modules conflicts and to set up automocks for @monkvision packages : ```js const { base } = require('@monkvision/jest-config'); @@ -40,3 +40,35 @@ module.exports = { setupFilesAfterEnv: ['/node_modules/@monkvision/jest-config/setupTests.js'] }; ``` + +This will prevent errors like this to show up when running your tests : + +``` +Jest encountered an unexpected token + + Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax. + + Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. + + By default "node_modules" folder is ignored by transformers. + + Here's what you can do: + • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it. + • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript + • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. + • If you need a custom transformation specify a "transform" option in your config. + • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. + + You'll find more details and examples of these config options in the docs: + https://jestjs.io/docs/configuration + For information about custom transformations, see: + https://jestjs.io/docs/code-transformation + + Details: + + /Users/souyahia/Projects/Monk/monkjs_private/apps/drive/node_modules/ky/distribution/index.js:2 + import { Ky } from './core/Ky.js'; + ^^^^^^ + + SyntaxError: Cannot use import statement outside a module +``` diff --git a/configs/jest-config/package.json b/configs/jest-config/package.json index 141df051e..e71cd2dfd 100644 --- a/configs/jest-config/package.json +++ b/configs/jest-config/package.json @@ -7,10 +7,7 @@ "main": "index.js", "license": "BSD-3-Clause-Clear", "files": [ - "index.js", - "base.js", - "react.js", - "__mocks__", + "*.js", "LICENSE", "README.md" ],