|
9 | 9 | runCLI,
|
10 | 10 | uniq,
|
11 | 11 | updateFile,
|
| 12 | + updateJson, |
12 | 13 | } from '@nx/e2e/utils';
|
13 | 14 | import * as ts from 'typescript';
|
14 | 15 |
|
@@ -435,6 +436,92 @@ describe('Linter', () => {
|
435 | 436 | );
|
436 | 437 | });
|
437 | 438 | });
|
| 439 | + |
| 440 | + describe('dependency checks', () => { |
| 441 | + beforeAll(() => { |
| 442 | + updateJson(`libs/${mylib}/.eslintrc.json`, (json) => { |
| 443 | + json.overrides = [ |
| 444 | + ...json.overrides, |
| 445 | + { |
| 446 | + files: ['*.json'], |
| 447 | + parser: 'jsonc-eslint-parser', |
| 448 | + rules: { |
| 449 | + '@nx/dependency-checks': 'error', |
| 450 | + }, |
| 451 | + }, |
| 452 | + ]; |
| 453 | + return json; |
| 454 | + }); |
| 455 | + updateJson(`libs/${mylib}/project.json`, (json) => { |
| 456 | + json.targets.lint.options.lintFilePatterns = [ |
| 457 | + `libs/${mylib}/**/*.ts`, |
| 458 | + `libs/${mylib}/project.json`, |
| 459 | + `libs/${mylib}/package.json`, |
| 460 | + ]; |
| 461 | + return json; |
| 462 | + }); |
| 463 | + }); |
| 464 | + |
| 465 | + it('should report dependency check issues', () => { |
| 466 | + const rootPackageJson = readJson('package.json'); |
| 467 | + const nxVersion = rootPackageJson.devDependencies.nx; |
| 468 | + const swcCoreVersion = rootPackageJson.devDependencies['@swc/core']; |
| 469 | + const swcHelpersVersion = rootPackageJson.dependencies['@swc/helpers']; |
| 470 | + |
| 471 | + let out = runCLI(`lint ${mylib}`, { silenceError: true }); |
| 472 | + expect(out).toContain('All files pass linting'); |
| 473 | + |
| 474 | + // make an explict dependency to nx |
| 475 | + updateFile( |
| 476 | + `libs/${mylib}/src/lib/${mylib}.ts`, |
| 477 | + (content) => |
| 478 | + `import { names } from '@nx/devkit';\n\n` + |
| 479 | + content.replace(/return .*;/, `return names(${mylib}).className;`) |
| 480 | + ); |
| 481 | + |
| 482 | + // output should now report missing dependencies section |
| 483 | + out = runCLI(`lint ${mylib}`, { silenceError: true }); |
| 484 | + expect(out).toContain( |
| 485 | + 'Dependency sections are missing from the "package.json"' |
| 486 | + ); |
| 487 | + |
| 488 | + // should fix the missing section issue |
| 489 | + out = runCLI(`lint ${mylib} --fix`, { silenceError: true }); |
| 490 | + expect(out).toContain( |
| 491 | + `Successfully ran target lint for project ${mylib}` |
| 492 | + ); |
| 493 | + const packageJson = readJson(`libs/${mylib}/package.json`); |
| 494 | + expect(packageJson).toMatchInlineSnapshot(` |
| 495 | + { |
| 496 | + "dependencies": { |
| 497 | + "@nx/devkit": "${nxVersion}", |
| 498 | + "@swc/core": "${swcCoreVersion}", |
| 499 | + "@swc/helpers": "${swcHelpersVersion}", |
| 500 | + "nx": "${nxVersion}", |
| 501 | + }, |
| 502 | + "name": "@proj/${mylib}", |
| 503 | + "type": "commonjs", |
| 504 | + "version": "0.0.1", |
| 505 | + } |
| 506 | + `); |
| 507 | + |
| 508 | + // intentionally set the invalid version |
| 509 | + updateJson(`libs/${mylib}/package.json`, (json) => { |
| 510 | + json.dependencies['@nx/devkit'] = '100.0.0'; |
| 511 | + return json; |
| 512 | + }); |
| 513 | + out = runCLI(`lint ${mylib}`, { silenceError: true }); |
| 514 | + expect(out).toContain( |
| 515 | + `The version specifier does not contain the installed version of "@nx/devkit" package: ${nxVersion}` |
| 516 | + ); |
| 517 | + |
| 518 | + // should fix the version mismatch issue |
| 519 | + out = runCLI(`lint ${mylib} --fix`, { silenceError: true }); |
| 520 | + expect(out).toContain( |
| 521 | + `Successfully ran target lint for project ${mylib}` |
| 522 | + ); |
| 523 | + }); |
| 524 | + }); |
438 | 525 | });
|
439 | 526 |
|
440 | 527 | describe('Root projects migration', () => {
|
|
0 commit comments