|
| 1 | +import deepmerge from 'deepmerge'; |
| 2 | +import {projectTypes} from '@form8ion/javascript-core'; |
| 3 | + |
| 4 | +import any from '@travi/any'; |
| 5 | +import {it, vi, describe, expect, beforeEach} from 'vitest'; |
| 6 | +import {when} from 'jest-when'; |
| 7 | + |
| 8 | +import {scaffold as scaffoldCommitlint} from './commitlint/index.js'; |
| 9 | +import {scaffold as scaffoldCommitizen} from './commitizen/index.js'; |
| 10 | +import {scaffold as scaffoldSemanticRelease} from './semantic-release/index.js'; |
| 11 | +import scaffoldCommitConvention from './scaffolder.js'; |
| 12 | + |
| 13 | +vi.mock('deepmerge'); |
| 14 | +vi.mock('./semantic-release/scaffolder.js'); |
| 15 | +vi.mock('./commitizen/scaffolder.js'); |
| 16 | +vi.mock('./commitlint/index.js'); |
| 17 | + |
| 18 | +describe('commit-convention scaffolder', () => { |
| 19 | + const projectRoot = any.string(); |
| 20 | + const publishedProjectType = any.fromList([projectTypes.PACKAGE, projectTypes.CLI]); |
| 21 | + const commitizenResults = any.simpleObject(); |
| 22 | + const semanticReleaseResults = any.simpleObject(); |
| 23 | + const mergedResults = any.simpleObject(); |
| 24 | + const conventionalCommitsBadge = { |
| 25 | + img: 'https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg', |
| 26 | + text: 'Conventional Commits', |
| 27 | + link: 'https://conventionalcommits.org' |
| 28 | + }; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + when(scaffoldCommitizen).calledWith({projectRoot}).mockResolvedValue(commitizenResults); |
| 32 | + when(scaffoldSemanticRelease).calledWith({projectRoot}).mockResolvedValue(semanticReleaseResults); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should configure the commit convention', async () => { |
| 36 | + const commitlintConfig = any.simpleObject(); |
| 37 | + const commitlintResults = any.simpleObject(); |
| 38 | + when(scaffoldCommitlint).calledWith({projectRoot, config: commitlintConfig}).mockResolvedValue(commitlintResults); |
| 39 | + when(deepmerge.all) |
| 40 | + .calledWith([ |
| 41 | + commitizenResults, |
| 42 | + commitlintResults, |
| 43 | + { |
| 44 | + vcsIgnore: {files: [], directories: []}, |
| 45 | + badges: {contribution: {'commit-convention': conventionalCommitsBadge}} |
| 46 | + }, |
| 47 | + {} |
| 48 | + ]) |
| 49 | + .mockReturnValue(mergedResults); |
| 50 | + |
| 51 | + expect(await scaffoldCommitConvention({projectRoot, configs: {commitlint: commitlintConfig}})) |
| 52 | + .toEqual(mergedResults); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should not configure commitlint if no config is provided', async () => { |
| 56 | + when(deepmerge.all).calledWith([ |
| 57 | + commitizenResults, |
| 58 | + { |
| 59 | + vcsIgnore: {files: [], directories: []}, |
| 60 | + badges: {contribution: {'commit-convention': conventionalCommitsBadge}} |
| 61 | + }, |
| 62 | + {} |
| 63 | + ]).mockReturnValue(mergedResults); |
| 64 | + |
| 65 | + expect(await scaffoldCommitConvention({projectRoot, configs: {}})).toEqual(mergedResults); |
| 66 | + expect(scaffoldCommitlint).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should configure semantic-release for publishable project types', async () => { |
| 70 | + when(deepmerge.all).calledWith([ |
| 71 | + commitizenResults, |
| 72 | + { |
| 73 | + vcsIgnore: {files: [], directories: []}, |
| 74 | + badges: {contribution: {'commit-convention': conventionalCommitsBadge}} |
| 75 | + }, |
| 76 | + semanticReleaseResults |
| 77 | + ]).mockReturnValue(mergedResults); |
| 78 | + |
| 79 | + expect(await scaffoldCommitConvention({projectRoot, projectType: publishedProjectType, configs: {}})) |
| 80 | + .toEqual(mergedResults); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should not configure tools for the commit-convention for a sub-project', async () => { |
| 84 | + expect(await scaffoldCommitConvention({pathWithinParent: any.string()})).toEqual({}); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should only configure semantic-release for a sub-project when the project is publishable', async () => { |
| 88 | + expect( |
| 89 | + await scaffoldCommitConvention({ |
| 90 | + projectRoot, |
| 91 | + pathWithinParent: any.string(), |
| 92 | + projectType: publishedProjectType |
| 93 | + }) |
| 94 | + ).toEqual(semanticReleaseResults); |
| 95 | + }); |
| 96 | +}); |
0 commit comments