{"id":20159438,"url":"https://github.com/voitanos/jest-preset-spfx-react16","last_synced_at":"2025-04-09T23:35:17.502Z","repository":{"id":33764551,"uuid":"161049433","full_name":"Voitanos/jest-preset-spfx-react16","owner":"Voitanos","description":"Jest preset configuration for SharePoint Framework projects that leverage React v16 (SPFx \u003e=1.7.0).","archived":false,"fork":false,"pushed_at":"2022-04-02T10:39:00.000Z","size":315,"stargazers_count":2,"open_issues_count":6,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T02:42:52.675Z","etag":null,"topics":["enzyme","jest","react","sharepoint-framework","spfx"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Voitanos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-09T14:46:39.000Z","updated_at":"2021-07-14T10:20:32.000Z","dependencies_parsed_at":"2022-07-28T22:49:17.192Z","dependency_job_id":null,"html_url":"https://github.com/Voitanos/jest-preset-spfx-react16","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voitanos%2Fjest-preset-spfx-react16","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voitanos%2Fjest-preset-spfx-react16/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voitanos%2Fjest-preset-spfx-react16/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Voitanos%2Fjest-preset-spfx-react16/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Voitanos","download_url":"https://codeload.github.com/Voitanos/jest-preset-spfx-react16/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248130510,"owners_count":21052757,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enzyme","jest","react","sharepoint-framework","spfx"],"created_at":"2024-11-14T00:08:05.581Z","updated_at":"2025-04-09T23:35:17.482Z","avatar_url":"https://github.com/Voitanos.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jest-preset-spfx-react16\n\n[![npm @latest](https://img.shields.io/npm/v/@voitanos/jest-preset-spfx-react16/latest?style=flat-square)](https://www.npmjs.com/package/@voitanos/jest-preset-spfx-react16)\n\n[![Voitanos on Twitter](https://img.shields.io/badge/Twitter-%40voitanos-blue?style=flat-square)](https://www.twitterl.com/voitanos)\n\nA [Jest](http://facebook.github.io/jest) preset configuration for [SharePoint Framework](https://docs.microsoft.com/sharepoint/dev/spfx/sharepoint-framework-overview) (SPFx) projects that leverage React v16. React v16 is used by SPFx projects created with the [SPFx Yeoman generator](https://www.npmjs.com/@microsoft/generator-sharepoint) v1.7.0 or later. It includes the popular [Enzyme](https://airbnb.io/enzyme/) React rendering library from Airbnb\n\n\u003e See the related packages [jest-preset-spfx](https://www.npmjs.com/package/@voitanos/jest-preset-spfx) for SPFx projects without React \u0026 [jest-preset-spfx-react15](https://www.npmjs.com/package/@voitanos/jest-preset-spfx-react15) if you are leveraging React v15.\n\n## Installation\n\n1. Install the desired Jest preset using your package manager of choice:\n\n    ```console\n    npm install jest @voitanos/jest-preset-spfx-react16 --save-dev\n    ```\n\n    This will install `@types/enzyme-adapter-react-16`, `@types/enzyme-to-json`, `@types/jest`, `@types/react-test-renderer`, `enzyme`, `enzyme-adapter-react-16`, `enzyme-to-json`, `identity-obj-proxy`, `jest-junit`, `raf`, `react-test-renderer` \u0026 `ts-jest` as dependencies in your project. The specific versions needed for React v16 are used\n\n    The postinstall script will verify you have a `./config/jest.config.json` file and update your `package.json` scripts with two scripts for running Jest tests with this configuration: `test` \u0026 `test:watch`.\n\n    If the configuration file is not present, it will create it. If it is present, it will verify the minimal properties.\n\n    \u003e **IMPORTANT**: Make sure you install the version of the preset that matches the version of TypeScript you're using in your project. See [Installing specific preset versions](#installing-specific-preset-versions) for details on how to determine the correct version.\n\n    \u003e **NOTE**: A specific version of `ts-jest` is used to support the SPFx supported version of TypeScript as more current versions of `ts-jest` require newer versions of TypeScript that is not yet supported by SPFx.\n\n1. Install the specified version of Jest for your preset version. See [Installing specific preset versions](#installing-specific-preset-versions) for guidance on picking the correct preset version.\n\n    When the you install the preset, it will output to the console the version of Jest you need to install that the installed version of the **ts-jest** transformer uses. Look for the following statement in the console:\n\n    ```console\n    ACTION REQUIRED: Install Jest v25.5.4 by executing the following command in the console:\n       npm install jest@25.5.4 --save-dev --save-exact\n    ```\n\n    Execute the command displayed in the console to install the required version of Jest in your project.\n\n### Installing specific preset versions\n\nFiguring out the correct matrix of package versions to install can be challenging. This mostly comes down to TypeScript.\n\nSPFx projects are written in TypeScript but Jest defaults to testing JavaScript files. We use the popular [ts-jest](https://www.npmjs.com/package/ts-jest) transformer that converts TypeScript files to JavaScript as Jest executes it's tests. Each version of **ts-jest** is written to match a specific version of TypeScript and Jest.\n\nBut no worries! We've done the hard part of figuring out what combinations of versions you need.\n\n#### Determine the TypeScript version of your project\n\nFirst, determine the version of TypeScript your SPFx project uses:\n\n1. Open the **package.json** file in your SPFx project.\n1. Within the `devDependencies` object, look for the package that starts with the name `@microsoft/rush-stack-compiler-`.\n\n    This package is the bridge to a specific version of TypeScript. The TypeScript version is indicated by the last part of the name of the package.\n\n    For example, consider the following entry in the **package.json** file:\n\n    ```json\n    {\n      ...\n      \"devDependencies\": {\n        \"@microsoft/rush-stack-compiler-2.9\": \"0.6.8\",\n        ...\n      }\n    }\n    ```\n\n    This SPFx project is using **TypeScript v2.9**.\n\n#### Determine the matching Jest preset version\n\nThe next step is to determine the corresponding version of the Jest preset that is configured with the version of TypeScript in your project.\n\nThe Jest preset NPM package contains distribution tags that are used to alias specific published versions of the package. We use them to indicate which version is built for a specific version of TypeScript.\n\n1. Get a list of all distribution tags by executing the following command:\n\n    ```console\n    npm info @voitanos/jest-preset-spfx-react16\n    ```\n\n1. Locate the matching TypeScript version. The previous command will write the package information to the console as well as a list of the published distribution tags:\n\n    ```console\n    dist-tags:\n    latest: 1.2.2             next: 1.3.0-beta.b385582  ts2.9: 1.2.2\n    ```\n\n    From this output, you can see three tags:\n\n      - **latest**: the most current stable version of the NPM package that's been published\n      - **next**: the next version of the NPM package, usually a beta and used for testing\n      - **ts2.9**: the version built for TypeScript v2.9\n\n#### Install a specific Jest preset package version\n\nUsing the information above, install the specific Jest preset package:\n\n```console\nnpm install @voitanos/jest-preset-spfx-react16@2.9 --save-dev --save-exact\n```\n\n\u003e **NOTE**: Pay close attention to the console output as you'll likely have at least one additional step (*look for ACTION REQUIRED*). The console will also contain information about changes applied to your project to support tests.\n\n## Validating Installation\n\nTo validate a successful install, do one of the following two things:\n\n### Option 1: Add example tests\n\n1. Copy the folder **examples** from the installed package (*also found [here in the source repo](https://github.com/Voitanos/jest-preset-spfx-react16/tree/master/examples)*) into the project's **src** folder.\n1. Execute Jest to run the tests:\n\n    ```shell\n    npm test\n    ```\n\n1. Observe five (5) passing tests: one for React rendering, four for non-React Typescript).\n\n### Option 2: Create your own test\n\n1. Add a new file `SampleTests.spec.ts` to the `./src/webparts` folder with the following code:\n\n    ```ts\n    import 'jest';\n\n    test('1+1 should equal 2', () =\u003e {\n      const result = 1+1;\n      expect(result).toBe(2);\n    });\n    ```\n\n1. Execute Jest to run the tests:\n\n    ```shell\n    npm test\n    ```\n\n1. Observe a single (1) passing test.\n\n## How it works\n\nThis package contains a [base Jest configuration](https://github.com/Voitanos/jest-preset-spfx-react16/blob/master/jest-preset.json) that your project will inherit. It does this by using the `preset` property in the `jest.config.json` file.\n\n## References\n\n### Package.json NPM scripts\n\nTwo scripts are added to the `package.json` scripts section:\n\n- **test**: Run Jest and specify the configuration file to use: `npm test`.\n- **test:watch**: Run Jest and specify the configuration file to use, but run in watch mode so when files change, it will automatically re-run the tests: `npm run test:watch`.\n\n### Jest preset configuration for SPFx\n\nThe following preset is used for SPFx projects:\n\n```json\n{\n  \"collectCoverage\": true,\n  \"coverageDirectory\": \"\u003crootDir\u003e/../temp/test\",\n  \"coverageReporters\": [\n    \"json\",\n    \"lcov\",\n    \"text-summary\"\n  ],\n  \"moduleFileExtensions\": [\n    \"ts\",\n    \"tsx\",\n    \"js\",\n    \"json\"\n  ],\n  \"moduleNameMapper\": {\n    \"\\\\.(css|scss)$\": \"identity-obj-proxy\",\n    \"^resx-strings/en-us.json\": \"\u003crootDir\u003e/node_modules/@microsoft/sp-core-library/lib/resx-strings/en-us.json\"\n  },\n  \"setupFiles\": [\n    \"raf/polyfill\",\n    \"@voitanos/jest-preset-spfx-react16/jest.enzyme.js\"\n  ],\n  \"snapshotSerializers\": [\n    \"enzyme-to-json/serializer\"\n  ],\n  \"testMatch\": [\n    \"**/src/**/*.(spec|test).+(ts|js)?(x)\",\n    \"**/__tests__/**/*.(spec|test).+(ts|js)?(x)\"\n  ],\n  \"transform\": {\n    \"^.+\\\\.(ts|tsx)$\": \"ts-jest\"\n  }\n}\n```\n\nExplanation of select configuration properties above:\n\n- **collectCoverage**: collects code coverage statistics and generates associated reports in the `./temp/test` folder\n- **moduleNameMapper**:\n  - when Jest sees a request for a CSS/SCSS file in the source files, it effectively ignores it using the `identity-obj-proxy` package\n  - when jest sees a request for `en-us.json`, it is provided a helper path to find the file\n- **setupFiles**:\n  - installs the **requestAnimationFrames** polyfill needed for headless browser testing\n  - configures Enzyme to use the React v16 adapter\n- **testMatch**: all tests found either in a special `__tests__` folder or within the `src` folder with the following names will be found:\n  - `*.spec.ts`\n  - `*.spec.tsx`\n  - `*.spec.js`\n  - `*.spec.jsx`\n  - `*.test.ts`\n  - `*.test.tsx`\n  - `*.test.js`\n  - `*.test.jsx`\n- **transform**: the Jest preprocessor will transpile all TypeScript files to JavaScript before running the tests\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoitanos%2Fjest-preset-spfx-react16","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvoitanos%2Fjest-preset-spfx-react16","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoitanos%2Fjest-preset-spfx-react16/lists"}