npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@voitanos/jest-preset-spfx-react15

v1.3.2

Published

Jest preset configuration for SharePoint Framework projects that leverage React v15 (SPFx <=1.6.0).

Downloads

37

Readme

jest-preset-spfx-react15

A Jest preset configuration for SharePoint Framework (SPFx) projects that leverage React v15. React v15 is used by SPFx projects created with the SPFx Yeoman generator v1.6.0 or earlier. It includes the popular Enzyme React rendering library from Airbnb

See the related packages jest-preset-spfx for SPFx projects without React & jest-preset-spfx-react16 if you are leveraging React v16.

Installation

Install Jest & this preset using your package manager of choice:

npm install [email protected] @voitanos/jest-preset-spfx-react15 --save-dev --save-exact

This will install @types/enzyme-adapter-react-15, @types/enzyme-to-json, @types/jest, @types/react-test-renderer, enzyme, enzyme-adapter-react-15, enzyme-to-json, identity-obj-proxy, raf, react-test-renderer & ts-jest as dependencies in your project. The specific versions needed for React v15 are used

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 & test:watch.

If the configuration file is not present, it will create it. If it is present, it will verify the minimal properties.

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.

Validating Installation

To validate a successful install, do one of the following two things:

Option 1: Add example tests

  1. Copy the folder examples from the installed package (also found here in the source repo) into the project's src folder.

  2. Execute Jest to run the tests:

    npm test
  3. Observe five (5) passing tests: one for React rendering, four for non-React Typescript).

Option 2: Create your own test

  1. Add a new file SampleTests.spec.ts to the ./src/webparts folder with the following code:

    import 'jest';
    
    test('1+1 should equal 2', () => {
      const result: number = 1 + 1;
        expect(result).toBe(2);
    });
  2. Execute Jest to run the tests:

    npm test
  3. Observe a single (1) passing test.

How it works

This package contains a base Jest configuration that your project will inherit. It does this by using the preset property in the jest.config.json file.

References

Package.json NPM scripts

Two scripts are added to the package.json scripts section:

  • test: Run Jest and specify the configuration file to use: npm test.
  • 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.

Jest preset configuration for SPFx

The following preset is used for SPFx projects:

{
  "collectCoverage": true,
  "coverageDirectory": "<rootDir>/../temp/test",
  "coverageReporters": [
    "json",
    "lcov",
    "text-summary"
  ],
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "json"
  ],
  "moduleNameMapper": {
    "\\.(css|scss)$": "identity-obj-proxy",
    "^resx-strings/en-us.json": "<rootDir>/node_modules/@microsoft/sp-core-library/lib/resx-strings/en-us.json"
  },
  "setupFiles": [
    "raf/polyfill",
    "@voitanos/jest-preset-spfx-react15/jest.enzyme.js"
  ],
  "snapshotSerializers": [
    "enzyme-to-json/serializer"
  ],
  "testMatch": [
    "**/src/**/*.(spec|test).+(ts|js)?(x)",
    "**/__tests__/**/*.(spec|test).+(ts|js)?(x)"
  ],
  "transform": {
    "^.+\\.(ts|tsx)$": "ts-jest"
  }
}

Explanation of select configuration properties above:

  • collectCoverage: collects code coverage statistics and generates associated reports in the ./temp/test folder
  • moduleNameMapper:
    • when Jest sees a request for a CSS/SCSS file in the source files, it effectively ignores it using the identity-obj-proxy package
    • when jest sees a request for en-us.json, it is provided a helper path to find the file
  • setupFules:
    • installs the requestAnimationFrames polyfill needed for headless browser testing
    • configures Enzyme to use the React v15 adapter
  • testMatch: all tests found either in a special __tests__ folder or within the src folder with the following names will be found:
    • *.spec.ts
    • *.spec.tsx
    • *.spec.js
    • *.spec.jsx
    • *.test.ts
    • *.test.tsx
    • *.test.js
    • *.test.jsx
  • transform: the Jest preprocessor will transpile all TypeScript files to JavaScript before running the tests