|
1 |
| -import {describe, expect, it} from 'vitest'; |
| 1 | +import {promises as fs} from 'node:fs'; |
| 2 | +import {fileExists} from '@form8ion/core'; |
2 | 3 |
|
| 4 | +import {describe, expect, it, vi} from 'vitest'; |
| 5 | +import any from '@travi/any'; |
| 6 | +import {when} from 'jest-when'; |
| 7 | + |
| 8 | +import scaffoldCommitlint from './scaffolder.js'; |
3 | 9 | import liftCommitlint from './lifter.js';
|
4 | 10 |
|
| 11 | +vi.mock('node:fs'); |
| 12 | +vi.mock('@form8ion/core'); |
| 13 | +vi.mock('./scaffolder.js'); |
| 14 | + |
5 | 15 | describe('commitlint lifter', () => {
|
| 16 | + const projectRoot = any.string(); |
| 17 | + const commitlintConfig = any.simpleObject(); |
| 18 | + const configs = {commitlint: commitlintConfig}; |
| 19 | + |
6 | 20 | it('should return an empty result when config does not need the format to be changed', async () => {
|
7 |
| - expect(await liftCommitlint()).toEqual({}); |
| 21 | + when(fileExists).calledWith(`${projectRoot}/.commitlintrc.js`).mockResolvedValue(false); |
| 22 | + when(fileExists).calledWith(`${projectRoot}/.commitlintrc.cjs`).mockResolvedValue(false); |
| 23 | + |
| 24 | + expect(await liftCommitlint({projectRoot, configs})).toEqual({}); |
| 25 | + |
| 26 | + expect(scaffoldCommitlint).not.toHaveBeenCalled(); |
| 27 | + expect(fs.unlink).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should migrate the config to json format if currently using js format', async () => { |
| 31 | + const scaffoldResult = any.simpleObject(); |
| 32 | + when(fileExists).calledWith(`${projectRoot}/.commitlintrc.js`).mockResolvedValue(true); |
| 33 | + when(fileExists).calledWith(`${projectRoot}/.commitlintrc.cjs`).mockResolvedValue(false); |
| 34 | + when(scaffoldCommitlint).calledWith({projectRoot, config: commitlintConfig}).mockResolvedValue(scaffoldResult); |
| 35 | + |
| 36 | + expect(await liftCommitlint({projectRoot, configs})).toEqual(scaffoldResult); |
| 37 | + expect(fs.unlink).toHaveBeenCalledWith(`${projectRoot}/.commitlintrc.js`); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should migrate the config to json format if currently using cjs format', async () => { |
| 41 | + const scaffoldResult = any.simpleObject(); |
| 42 | + when(fileExists).calledWith(`${projectRoot}/.commitlintrc.js`).mockResolvedValue(false); |
| 43 | + when(fileExists).calledWith(`${projectRoot}/.commitlintrc.cjs`).mockResolvedValue(true); |
| 44 | + when(scaffoldCommitlint).calledWith({projectRoot, config: commitlintConfig}).mockResolvedValue(scaffoldResult); |
| 45 | + |
| 46 | + expect(await liftCommitlint({projectRoot, configs})).toEqual(scaffoldResult); |
| 47 | + expect(fs.unlink).toHaveBeenCalledWith(`${projectRoot}/.commitlintrc.cjs`); |
8 | 48 | });
|
9 | 49 | });
|
0 commit comments