Skip to content

Commit

Permalink
Enhances 'spfx doctor' command with 'json' output. Closes #4212
Browse files Browse the repository at this point in the history
  • Loading branch information
MathijsVerbeeck authored and Adam-it committed Apr 22, 2024
1 parent b4816dd commit 9cb6870
Show file tree
Hide file tree
Showing 3 changed files with 349 additions and 99 deletions.
110 changes: 108 additions & 2 deletions docs/docs/cmd/spfx/spfx-doctor.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ m365 spfx doctor [options]
: JMESPath query string. See [http://jmespath.org/](http://jmespath.org/) for more information and examples

`-o, --output [output]`
: Output type. `text`. Default `text`
: Output type. `text` or `json`. Default `text`

`--verbose`
: Runs command with verbose logging
Expand Down Expand Up @@ -56,7 +56,7 @@ Checks ran by this command are based on what is officially supported by Microsof

:::info

This command supports only text output.
This command supports only text or json output.

:::

Expand All @@ -80,11 +80,67 @@ Verify if your environment meets the requirements to work with SharePoint Framew
m365 spfx doctor --spfxVersion 1.11.0 --output text
```

Verify if your environment meets the requirements to work with SharePoint Framework v1.11.0 and outputs it in a JSON format.

```sh
m365 spfx doctor --spfxVersion 1.11.0 --output json
```

## Response

### Response with no issues

<Tabs>
<TabItem value="JSON">

```json
[
{
"check": "SharePoint Framework",
"passed": true,
"version": "1.18.2",
"message": "SharePoint Framework v1.18.2 valid."
},
{
"check": "SharePoint Framework",
"passed": true,
"version": "1.18.2",
"message": "SharePoint Framework v1.18.2"
},
{
"check": "env",
"passed": true,
"message": "Supported in SPO",
"version": "spo"
},
{
"check": "Node",
"passed": true,
"message": "Node v18.18.2",
"version": "18.18.2"
},
{
"check": "yo",
"passed": true,
"message": "yo v5.0.0",
"version": "5.0.0"
},
{
"check": "gulp-cli",
"passed": true,
"message": "gulp-cli v2.3.0",
"version": "2.3.0"
},
{
"check": "typescript",
"passed": true,
"message": "bundled typescript used"
}
]
```

</TabItem>

<TabItem value="Text">

```text
Expand All @@ -106,6 +162,56 @@ m365 spfx doctor --spfxVersion 1.11.0 --output text
When the installed version of Yeoman is lower than expected to run SharePoint Framework v1.15.0

<Tabs>
<TabItem value="JSON">

```json
[
{
"check": "SharePoint Framework",
"passed": true,
"version": "1.15.0",
"message": "SharePoint Framework v1.15.0 valid."
},
{
"check": "SharePoint Framework",
"passed": true,
"version": "1.15.0",
"message": "SharePoint Framework v1.15.0"
},
{
"check": "env",
"passed": true,
"message": "Supported in SPO",
"version": "spo"
},
{
"check": "Node",
"passed": true,
"message": "Node v16.18.0",
"version": "16.18.0"
},
{
"check": "yo",
"passed": false,
"message": "yo v3.1.1 found, v^4 required",
"version": "3.1.1",
"fix": "npm i -g yo@4"
},
{
"check": "gulp-cli",
"passed": true,
"message": "gulp-cli v2.3.0",
"version": "2.3.0"
},
{
"check": "typescript",
"passed": true,
"message": "bundled typescript used"
}
]
```

</TabItem>
<TabItem value="Text">

```text
Expand Down
33 changes: 30 additions & 3 deletions src/m365/spfx/commands/spfx-doctor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import { pid } from '../../../utils/pid.js';
import { session } from '../../../utils/session.js';
import { sinonUtil } from '../../../utils/sinonUtil.js';
import commands from '../commands.js';
import command from './spfx-doctor.js';
import command, { SpfxDoctorCheck } from './spfx-doctor.js';

describe(commands.DOCTOR, () => {
let log: string[];
let sandbox: SinonSandbox;
let logger: Logger;
let loggerLogSpy: sinon.SinonSpy;
let loggerLogSpyErr: sinon.SinonSpy;
let commandInfo: CommandInfo;

const packageVersionResponse = (name: string, version: string): string => {
Expand Down Expand Up @@ -55,7 +56,11 @@ describe(commands.DOCTOR, () => {
}
};
loggerLogSpy = sinon.spy(logger, 'log');
loggerLogSpyErr = sinon.spy(logger, 'logToStderr');
sinon.stub(process, 'platform').value('linux');
(command as any).resultsObject = [];
(command as any).output = '';
(command as any).logger = null;
});

afterEach(() => {
Expand Down Expand Up @@ -575,10 +580,10 @@ describe(commands.DOCTOR, () => {
assert(loggerLogSpy.calledWith(getStatus(0, 'Supported in SP2019')));
});

it('fails validation if output does not equal text.', async () => {
it('fails validation if output does not equal text or json.', async () => {
const actual = await command.validate({
options: {
output: 'json'
output: 'md'
}
}, commandInfo);

Expand Down Expand Up @@ -836,6 +841,28 @@ describe(commands.DOCTOR, () => {
assert(loggerLogSpy.calledWith('- npm i -g yo@3'), 'No fix provided');
});

it('fails yo check when yo not found and logs error object', async () => {
const sandbox = sinon.createSandbox();
sandbox.stub(process, 'version').value('v10.18.0');
sinon.stub(child_process, 'exec').callsFake((file, callback: any) => {
const packageName: string = file.split(' ')[2];
switch (packageName) {
case '@microsoft/sp-core-library':
callback(undefined, packageVersionResponse(packageName, '1.10.0'));
break;
default:
callback(new Error(`${file} ENOENT`));
}
return {} as child_process.ChildProcess;
});

await command.action(logger, { options: { output: 'json' } });
const checks: SpfxDoctorCheck[] = loggerLogSpy.lastCall.args[0];
assert.strictEqual(checks.filter(y => y.check === 'yo')[0].passed, false);
assert(loggerLogSpyErr.calledWith('- npm i -g yo@3'), 'No fix provided');
assert(loggerLogSpyErr.calledWith(getStatus(1, 'yo not found')));
});

it('passes gulp-cli check when gulp-cli found', async () => {
const sandbox = sinon.createSandbox();
sandbox.stub(process, 'version').value('v10.18.0');
Expand Down
Loading

0 comments on commit 9cb6870

Please sign in to comment.