Skip to content

Commit a7ee439

Browse files
committed
add tests and refine dependency checks
1 parent c00b44d commit a7ee439

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import fs from 'fs';
2+
import dependencies from '../dependencies';
3+
import {EnvironmentInfo} from '../../../types';
4+
5+
describe('dependencies', () => {
6+
let environmentInfo: EnvironmentInfo;
7+
let dependenciesJSON: string;
8+
9+
beforeEach(() => {
10+
jest.spyOn(fs, 'readFileSync').mockImplementation(() => dependenciesJSON);
11+
});
12+
13+
it('returns false if dependencies are correct', async () => {
14+
dependenciesJSON = JSON.stringify({
15+
name: 'AwesomeProject',
16+
dependencies: {
17+
'react-native': '0.72.1',
18+
},
19+
});
20+
21+
const diagnostics = await dependencies.getDiagnostics(environmentInfo);
22+
expect(diagnostics.needsToBeFixed).toBe(false);
23+
});
24+
25+
it('returns true if dependencies contains an incompatible version react native package', async () => {
26+
dependenciesJSON = JSON.stringify({
27+
name: 'AwesomeProject',
28+
dependencies: {
29+
'react-native': '0.72.1',
30+
'@react-native/codegen': '1.72.3',
31+
'@react-native/gradle-plugin': '0.69.10',
32+
},
33+
});
34+
35+
const diagnostics = await dependencies.getDiagnostics(environmentInfo);
36+
expect(diagnostics.needsToBeFixed).toBe(true);
37+
});
38+
39+
it('returns true if dependencies contains an compatible version react native package', async () => {
40+
dependenciesJSON = JSON.stringify({
41+
name: 'AwesomeProject',
42+
dependencies: {
43+
'react-native': '0.72.1',
44+
'@react-native/codegen': '0.72.1',
45+
},
46+
});
47+
48+
const diagnostics = await dependencies.getDiagnostics(environmentInfo);
49+
expect(diagnostics.needsToBeFixed).toBe(true);
50+
});
51+
});

packages/cli-doctor/src/tools/healthchecks/dependencies.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import chalk from 'chalk';
23
import path from 'path';
34
import semver from 'semver';
45
import {HealthCheckInterface} from '../../types';
@@ -50,21 +51,31 @@ export default {
5051
const root = config?.root || findProjectRoot();
5152
const dependencies = findDependencies(root);
5253
const reactNativeVersion = dependencies['react-native'];
53-
const reactNativeMinorVersion = semver.coerce(reactNativeVersion)?.minor;
54+
const reactNativeCoercedVersion = semver.coerce(reactNativeVersion);
5455
const issues: string[] = [];
5556

5657
RNPackages.forEach((pkg) => {
5758
if (dependencies[pkg]) {
58-
issues.push(
59-
` - ${pkg} is part of React Native and should not be a dependency in your package.json`,
60-
);
6159
const packageVersion = dependencies[pkg];
62-
const packageMinorVersion = semver.coerce(packageVersion)?.minor;
63-
64-
if (reactNativeMinorVersion !== packageMinorVersion) {
65-
issues.push(
66-
` - ${pkg} "${packageVersion}" is not compatible with react-native: "${reactNativeVersion}"`,
60+
const packageCoercedVersion = semver.coerce(packageVersion);
61+
if (reactNativeCoercedVersion && packageCoercedVersion) {
62+
const verisonDiff = semver.diff(
63+
packageCoercedVersion,
64+
reactNativeCoercedVersion,
6765
);
66+
if (verisonDiff === 'major' || verisonDiff === 'minor') {
67+
issues.push(
68+
` - ${chalk.red.bold(
69+
'error',
70+
)} ${pkg}: "${packageVersion}" is not compatible with react-native: "${reactNativeVersion}"`,
71+
);
72+
} else {
73+
issues.push(
74+
` - ${chalk.yellow.bold(
75+
'warn',
76+
)} ${pkg} is part of React Native and should not be a dependency in your package.json`,
77+
);
78+
}
6879
}
6980
}
7081
});

0 commit comments

Comments
 (0)