Skip to content

Commit c70dc1a

Browse files
baywetVesaJuvonen
authored andcommitted
azure devops: updated documentation to incorporate jest change (SharePoint#3699)
1 parent 5638e3e commit c70dc1a

8 files changed

+71
-63
lines changed

docs/images/azure-devops-spfx-04.png

-9 KB
Loading

docs/images/azure-devops-spfx-04b.png

31.9 KB
Loading

docs/images/azure-devops-spfx-05.png

-548 Bytes
Loading

docs/images/azure-devops-spfx-06.png

6.88 KB
Loading

docs/images/azure-devops-spfx-07.png

7.14 KB
Loading

docs/images/azure-devops-spfx-08.png

17.2 KB
Loading

docs/images/azure-devops-spfx-09.png

18 KB
Loading

docs/spfx/toolchain/implement-ci-cd-with-azure-devops.md

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ Continuous Integration (CI) helps developers integrate code into a shared reposi
1919
Setting up Azure DevOps for Continuous Integration with a SharePoint Framework solution requires the following steps:
2020

2121
1. Creating the Build Definition
22-
2. Installing NodeJS
23-
3. Restoring dependencies
24-
4. Executing Unit Tests
25-
5. Importing code coverage information
26-
6. Bundling the solution
27-
7. Packaging the solution
28-
8. Preparing the artifacts
29-
9. Publishing the artifacts
22+
1. Installing NodeJS
23+
1. Restoring dependencies
24+
1. Executing Unit Tests
25+
1. Importing test results
26+
1. Importing code coverage information
27+
1. Bundling the solution
28+
1. Packaging the solution
29+
1. Preparing the artifacts
30+
1. Publishing the artifacts
3031

3132
### Creating the Build Definition
3233
The Build Definition, as its name suggests, includes all the definitions and their configurations for the build. Start setting up your Continuous Integration by creating a new build definition and link it to your repository.
@@ -49,70 +50,77 @@ Because third party dependencies are not stored in the source control, you need
4950

5051
### Executing Unit Tests
5152

52-
The SharePoint Framework supports writing unit tests using KarmaJS, Mocha, Chai and Sinon. These modules are already referenced for you and it is highly recommended at a minimum to test the business logic of your code to get feedback on any potential issues or regressions as soon as possible. To have Azure DevOps execute your unit tests, add a `gulp` task. Set the path to the `gulpfile` file and set the `Gulp Tasks` option to `test`.
53+
The SharePoint Framework does not provide a testing framework by default (since 1.8.0), we will leverage Jest with this sample. These modules will be installed in a later step and it is highly recommended at a minimum to test the business logic of your code to get feedback on any potential issues or regressions as soon as possible. To have Azure DevOps execute your unit tests, add a `npm` task. Set the `command` to `custom` and `custom command` field, enter `test`. Then set the `Working Directory` option to `$(Build.SourcesDirectory)`.
5354

5455
![executing unit tests](../../images/azure-devops-spfx-04.png)
5556

56-
> [!NOTE]
57-
> Make sure you check `Publish to TFS/Team Services` under the `JUnit Test Results` section and set the `Test Result Files` to `**/test-*.xml`. This will instruct the task to report results with the build status in Azure DevOps.
5857

59-
#### Configuring KarmaJS
58+
#### Configuring Jest
6059

61-
By default SharePoint Framework projects do not include a reporter for JUnit. Reporters are plugins for KarmaJS that export the test results in a certain format. To install the necessary reporters, run the following commands in your project.
60+
By default SharePoint Framework projects does not include a testing Framework. We will leverage Jest in this sample.
6261

6362
```shell
64-
63+
npm i [email protected] jest jest-junit @voitanos/jest-preset-spfx-react16 -D
6564
```
6665

67-
You also need to configure KarmaJS to load and use the reporter, to do so create a file `config/karma.config.js` and add the following content.
68-
69-
```JS
70-
"use strict";
71-
var existingKarmaConfig = require('@microsoft/sp-build-web/lib/karma/karma.config');
72-
var junitReporter = require('karma-junit-reporter');
73-
74-
module.exports = function (config) {
75-
existingKarmaConfig(config);
76-
config.reporters.push('junit');
66+
You also need to configure Jest, to do so create a file `config/jest.config.json` and add the following content.
67+
68+
```JSON
69+
{
70+
"preset": "@voitanos/jest-preset-spfx-react16",
71+
"rootDir": "../src",
72+
"coverageReporters": [
73+
"text",
74+
"json",
75+
"lcov",
76+
"text-summary",
77+
"cobertura"
78+
],
79+
"reporters": [
80+
"default",
81+
"jest-junit"
82+
]
83+
}
84+
```
85+
You also need to configure your project to leverage jest when typing commands. To do so edit the `package.json` file and add/replace these two `scripts` with the following values:
86+
```JSON
87+
"test": "./node_modules/.bin/jest --config ./config/jest.config.json",
88+
"test:watch": "./node_modules/.bin/jest --config ./config/jest.config.json --watchAll"
89+
```
90+
Finally you need to modify the `package.json` to configure the reporter. Reporters are plugins that provide new export formats for test results to test runners. To do so edit `package.json` and add these lines after the `scripts` property.
7791

78-
config.set({
79-
basePath: './..',
92+
```JSON
93+
"jest-junit": {
94+
"output": "temp/test/junit/junit.xml",
95+
"usePathForSuiteName": "true"
96+
}
97+
```
98+
#### Writing a unit test
99+
To write your first unit test, create a new file `src/webparts/webPartName/tests/webPartName.spec.ts` and add the following content:
100+
```TS
101+
/// <reference types="mocha" />
102+
import {assert, expect} from 'chai';
103+
104+
describe("webPartName", () => {
105+
it("should do something", () => {
106+
assert.ok(true, 'should be true');
80107
});
81-
82-
config.junitReporter = {
83-
outputDir: 'temp/', // results will be saved as $outputDir/$browserName.xml
84-
outputFile: 'test-results.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
85-
suite: 'karma', // suite will become the package name attribute in xml testsuite element
86-
useBrowserName: true, // add browser name to report and classes names
87-
};
88-
var coberturaSubDir = 'cobertura';
89-
var coverageSubDir = 'lcov';
90-
var coberturaFileName = 'cobertura.xml';
91-
config.coverageReporter.reporters.push({type: 'cobertura', subdir: './' + coberturaSubDir, file: coberturaFileName});
92-
config.coverageReporter.reporters.push({
93-
type: 'lcov',
94-
subdir: './' + coverageSubDir + '/',
95-
file: 'lcov.info'
108+
it("should add numbers Sync fluent", () => {
109+
const result:number = 1 + 3;
110+
expect(result).to.eq(4); // fluent API
96111
});
97-
config.browserNoActivityTimeout = 60000;
98-
config.plugins.push(junitReporter);
99-
};
112+
});
100113
```
114+
> [!NOTE]
115+
> You can learn more about writing unit tests using Jest and Chai [here](https://jestjs.io/docs/en/getting-started.html).
101116
102-
Finally you need to modify the gulpfile to instruct it to leverage this new configuration. To do so edit `gulpfile.js` and add these lines after `build.initialize(gulp);`.
103-
104-
```JS
105-
var buildConfig = build.getConfig();
106-
var karmaTaskCandidates = buildConfig.uniqueTasks.filter((t) => t.name === 'karma');
107-
if(karmaTaskCandidates && karmaTaskCandidates.length > 0) {
108-
var karmaTask = karmaTaskCandidates[0];
109-
karmaTask.taskConfig.configPath = './config/karma.config.js';
110-
}
111-
```
117+
### Importing test results
118+
In order to get test results information attached with the build results, you need to import these test results from the test runner into Azure DevOps. To do so, add a new `Publish Test Results` task. Set the `Test results files` field to `temp/test/junit/junit.xml` and the `Search folder` to `$(Build.SourcesDirectory)`.
119+
![importing test results](../../images/azure-devops-spfx-04b.png)
112120

113121
### Importing code coverage information
114122

115-
In order to get code coverage reported with the build status you need to add a task to import that information. To configure the code coverage information, add the `publish code coverage results` tasks. Make sure you set the tool to `Cobertura`, `Summary files` to `$(Build.SourcesDirectory)/temp/coverage/cobertura/cobertura.xml` and `Report Directory` to `$(Build.SourcesDirectory)/temp/coverage/cobertura`.
123+
In order to get code coverage reported with the build status you need to add a task to import that information. To configure the code coverage information, add the `publish code coverage results` tasks. Make sure you set the tool to `Cobertura`, `Summary files` to `$(Build.SourcesDirectory)/temp/test/cobertura-coverage.xml` and `Report Directory` to `$(Build.SourcesDirectory)/temp/test`.
116124

117125
![importing coverage information](../../images/azure-devops-spfx-05.png)
118126

@@ -147,14 +155,14 @@ Continuous Deployment (CD) takes validated code packages from build process and
147155
Setting up Azure DevOps for Continuous Deployments with a SharePoint Framework solution requires the following steps:
148156

149157
1. Creating the Release Definition
150-
2. Linking the Build Artifact
151-
3. Creating the Environment
152-
4. Installing NodeJS
153-
5. Installing the Office 365 CLI
154-
6. Connecting to the App Catalog
155-
7. Adding the Solution Package to the App Catalog
156-
8. Deploying the Application
157-
9. Setting the Variables for the Environment
158+
1. Linking the Build Artifact
159+
1. Creating the Environment
160+
1. Installing NodeJS
161+
1. Installing the Office 365 CLI
162+
1. Connecting to the App Catalog
163+
1. Adding the Solution Package to the App Catalog
164+
1. Deploying the Application
165+
1. Setting the Variables for the Environment
158166

159167
### Creating the Release Definition
160168

0 commit comments

Comments
 (0)