@voitanos/jest-preset-spfx v1.4.1
jest-preset-spfx
A Jest preset configuration for SharePoint Framework (SPFx) projects.
NOTE: This preset does not contain any support for SPFx projects that utilize React.
See the related packages jest-preset-spfx-react15 & jest-preset-spfx-react16 if you are leveraging React in your SPFx projects.
Installation
Install Jest & this preset using your package manager of choice:
npm install jest @voitanos/jest-preset-spfx --save-devThis will install
@types/jest,ts-jest&identity-obj-proxyas dependencies in your project.The postinstall script will verify you have a
./config/jest.config.jsonfile and update yourpackage.jsonscripts 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-jestis used to support the SPFx supported version of TypeScript as more current versions ofts-jestrequire 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
- Copy the folder examples from the installed package (also found here in the source repo) into the project's src folder.
Execute Jest to run the tests:
npm testObserve four (4) passing tests.
Option 2: Create your own test
Add a new file
SampleTests.spec.tsto the./src/webpartsfolder with the following code:import 'jest'; test('1+1 should equal 2', () => { const result = 1+1; expect(result).toBe(2); });Execute Jest to run the tests:
npm testObserve 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 to @voitanos/jest-preset-spfx 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:
Explanation of select configuration properties above: jest-preset.json
- collectCoverage, coverageDirectory, collectCoverageFrom & coverageReporters: collects code coverage statistics and generates associated reports in the
./temp/testfolder - moduleNameMapper:
- when Jest sees a request for a CSS/SCSS file in the source files, it effectively ignores it using the
identity-obj-proxypackage - when jest sees a request for
en-us.json, it is provided a helper path to find the file
- when Jest sees a request for a CSS/SCSS file in the source files, it effectively ignores it using the
- testMatch: all tests found either in a special
__tests__folder or within thesrcfolder 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.
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
