|
| 1 | +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; |
| 2 | +import { Tree, readProjectConfiguration, getProjects } from '@nrwl/devkit'; |
| 3 | + |
| 4 | +import generator from './generator'; |
| 5 | +import * as utils from '@nx-dotnet/utils'; |
| 6 | +import * as fs from 'fs'; |
| 7 | + |
| 8 | +jest.mock('@nx-dotnet/utils', () => ({ |
| 9 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 10 | + ...(jest.requireActual('@nx-dotnet/utils') as any), |
| 11 | + glob: jest.fn(), |
| 12 | + findProjectFileInPath: jest.fn(), |
| 13 | + resolve: (m: string) => m, |
| 14 | +})); |
| 15 | + |
| 16 | +const MOCK_API_PROJECT = ` |
| 17 | +<Project Sdk="Microsoft.NET.Sdk.Web"> |
| 18 | + <PropertyGroup> |
| 19 | + <TargetFramework>net5.0</TargetFramework> |
| 20 | + <RootNamespace>MyTestApi</RootNamespace> |
| 21 | + </PropertyGroup> |
| 22 | +</Project>`; |
| 23 | + |
| 24 | +const MOCK_TEST_PROJECT = ` |
| 25 | +<Project Sdk="Microsoft.NET.Sdk.Web"> |
| 26 | + <PropertyGroup> |
| 27 | + <TargetFramework>net5.0</TargetFramework> |
| 28 | + <RootNamespace>MyTestApi.Test</RootNamespace> |
| 29 | + </PropertyGroup> |
| 30 | + <ItemGroup> |
| 31 | + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> |
| 32 | + </ItemGroup> |
| 33 | +</Project>`; |
| 34 | + |
| 35 | +describe('import-projects generator', () => { |
| 36 | + let appTree: Tree; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + appTree = createTreeWithEmptyWorkspace(); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + jest.resetAllMocks(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should run successfully if no new projects are found', async () => { |
| 47 | + jest.spyOn(utils, 'glob').mockResolvedValue([]); |
| 48 | + const promise = generator(appTree); |
| 49 | + const oldProjects = getProjects(appTree); |
| 50 | + await expect(promise).resolves.not.toThrow(); |
| 51 | + const newProjects = getProjects(appTree); |
| 52 | + expect(oldProjects).toEqual(newProjects); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should run successfully if new projects are found', async () => { |
| 56 | + jest |
| 57 | + .spyOn(utils, 'glob') |
| 58 | + .mockImplementation((x) => |
| 59 | + Promise.resolve( |
| 60 | + x.startsWith('apps') ? ['apps/my-api/my-api.csproj'] : [], |
| 61 | + ), |
| 62 | + ); |
| 63 | + jest |
| 64 | + .spyOn(utils, 'findProjectFileInPath') |
| 65 | + .mockImplementation((x) => |
| 66 | + x.startsWith('apps') |
| 67 | + ? Promise.resolve('apps/my-api/my-api.csproj') |
| 68 | + : Promise.reject(), |
| 69 | + ); |
| 70 | + jest.spyOn(fs, 'readFileSync').mockReturnValue(MOCK_TEST_PROJECT); |
| 71 | + jest.spyOn(fs, 'writeFileSync').mockImplementation(() => null); |
| 72 | + appTree.write('apps/my-api/my-api.csproj', MOCK_API_PROJECT); |
| 73 | + const promise = generator(appTree); |
| 74 | + await expect(promise).resolves.not.toThrow(); |
| 75 | + expect(readProjectConfiguration(appTree, 'my-test-api')).toBeDefined(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should run add test target if test projects are found', async () => { |
| 79 | + jest |
| 80 | + .spyOn(utils, 'glob') |
| 81 | + .mockImplementation((x) => |
| 82 | + Promise.resolve( |
| 83 | + x.startsWith('apps') ? ['apps/my-api-test/my-api-test.csproj'] : [], |
| 84 | + ), |
| 85 | + ); |
| 86 | + jest |
| 87 | + .spyOn(utils, 'findProjectFileInPath') |
| 88 | + .mockImplementation((x) => |
| 89 | + x.startsWith('apps') |
| 90 | + ? Promise.resolve('apps/my-api/my-api-test.csproj') |
| 91 | + : Promise.reject(), |
| 92 | + ); |
| 93 | + jest.spyOn(fs, 'readFileSync').mockReturnValue(MOCK_TEST_PROJECT); |
| 94 | + jest.spyOn(fs, 'writeFileSync').mockImplementation(() => null); |
| 95 | + appTree.write('apps/my-api-test/my-api-test.csproj', MOCK_TEST_PROJECT); |
| 96 | + const promise = generator(appTree); |
| 97 | + await expect(promise).resolves.not.toThrow(); |
| 98 | + expect(readProjectConfiguration(appTree, 'my-test-api-test')).toBeDefined(); |
| 99 | + expect( |
| 100 | + readProjectConfiguration(appTree, 'my-test-api-test').targets.test, |
| 101 | + ).toBeDefined(); |
| 102 | + expect( |
| 103 | + readProjectConfiguration(appTree, 'my-test-api-test').targets.serve, |
| 104 | + ).not.toBeDefined(); |
| 105 | + }); |
| 106 | +}); |
0 commit comments