Skip to content

Commit 6f294db

Browse files
Merge pull request SharePoint#4727 from baywet/bugfix/ci-cd-reporter
fixes SharePoint#4217 : cicd updated junit configuration and sample unit test
2 parents a8519b7 + 1da48b2 commit 6f294db

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

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

-2.6 KB
Loading

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

-2.42 KB
Loading

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ The SharePoint Framework does not provide a testing framework by default (since
6060
By default SharePoint Framework projects does not include a testing Framework. We will leverage Jest in this sample.
6161

6262
```shell
63-
npm i [email protected] jest jest-junit @voitanos/jest-preset-spfx-react16 -D
63+
npm i jest jest-junit @voitanos/jest-preset-spfx-react16 -D
6464
```
6565

6666
> [!NOTE]
@@ -81,7 +81,11 @@ You also need to configure Jest, to do so create a file `config/jest.config.json
8181
],
8282
"reporters": [
8383
"default",
84-
"jest-junit"
84+
["jest-junit", {
85+
"suiteName": "jest tests",
86+
"outputDirectory": "temp/test/junit",
87+
"outputName": "junit.xml"
88+
}]
8589
]
8690
}
8791
```
@@ -90,40 +94,28 @@ You also need to configure your project to leverage jest when typing commands. T
9094
"test": "./node_modules/.bin/jest --config ./config/jest.config.json",
9195
"test:watch": "./node_modules/.bin/jest --config ./config/jest.config.json --watchAll"
9296
```
93-
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.
94-
95-
```JSON
96-
"jest-junit": {
97-
"output": "temp/test/junit/junit.xml",
98-
"usePathForSuiteName": "true"
99-
}
100-
```
10197
#### Writing a unit test
10298
To write your first unit test, create a new file `src/webparts/webPartName/tests/webPartName.spec.ts` and add the following content:
10399
```TS
104-
/// <reference types="mocha" />
105-
import {assert, expect} from 'chai';
100+
import 'jest'
106101

107102
describe("webPartName", () => {
108-
it("should do something", () => {
109-
assert.ok(true, 'should be true');
110-
});
111-
it("should add numbers Sync fluent", () => {
112-
const result:number = 1 + 3;
113-
expect(result).to.eq(4); // fluent API
103+
test("should add numbers Sync fluent", () => {
104+
const result = 1 + 3;
105+
expect(result).toBe(4); // fluent API
114106
});
115107
});
116108
```
117109
> [!NOTE]
118-
> You can learn more about writing unit tests using Jest and Chai [here](https://jestjs.io/docs/en/getting-started.html).
110+
> You can learn more about writing unit tests using Jest [here](https://jestjs.io/docs/en/getting-started.html). You can learn more about testing react applications with Jest and Enzyme [here](https://jestjs.io/docs/en/tutorial-react) (you can ignore the setup part).
119111
120112
### Importing test results
121-
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)`.
113+
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 `**/junit.xml` and the `Search folder` to `$(Build.SourcesDirectory)`.
122114
![importing test results](../../images/azure-devops-spfx-04b.png)
123115

124116
### Importing code coverage information
125117

126-
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`.
118+
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)/**/*-coverage.xml`.
127119

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

0 commit comments

Comments
 (0)