-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(ci): add missing unit tests for CLI helpers
- Loading branch information
1 parent
db3fcce
commit e735bf4
Showing
2 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { DEFAULT_SETTINGS } from '../constants'; | ||
import { type CommandContext, createCommandContext } from './context'; | ||
|
||
describe('createCommandContext', () => { | ||
it('should pick CLI-related settings in standalone mode', () => { | ||
expect( | ||
createCommandContext( | ||
{ | ||
bin: 'npx --no-install code-pushup', | ||
config: null, | ||
debug: false, | ||
detectNewIssues: true, | ||
directory: '/test', | ||
logger: console, | ||
monorepo: false, | ||
output: '.code-pushup', | ||
projects: null, | ||
silent: false, | ||
task: 'code-pushup', | ||
}, | ||
null, | ||
), | ||
).toStrictEqual<CommandContext>({ | ||
project: undefined, | ||
bin: 'npx --no-install code-pushup', | ||
directory: '/test', | ||
config: null, | ||
silent: false, | ||
output: '.code-pushup', | ||
}); | ||
}); | ||
|
||
it('should override some settings when given monorepo project config', () => { | ||
expect( | ||
createCommandContext( | ||
{ | ||
bin: 'npx --no-install code-pushup', | ||
config: null, | ||
debug: false, | ||
detectNewIssues: true, | ||
directory: '/test', | ||
logger: console, | ||
monorepo: false, | ||
output: '.code-pushup', | ||
projects: null, | ||
silent: false, | ||
task: 'code-pushup', | ||
}, | ||
{ | ||
name: 'ui', | ||
directory: '/test/ui', | ||
bin: 'yarn code-pushup', | ||
}, | ||
), | ||
).toStrictEqual<CommandContext>({ | ||
project: 'ui', | ||
bin: 'yarn code-pushup', | ||
directory: '/test/ui', | ||
config: null, | ||
silent: false, | ||
output: '.code-pushup', | ||
}); | ||
}); | ||
|
||
it('should interpolate project name in output path for monorepo project', () => { | ||
expect( | ||
createCommandContext( | ||
{ | ||
...DEFAULT_SETTINGS, | ||
output: '.code-pushup/{project}', | ||
}, | ||
{ | ||
name: 'website', | ||
bin: 'npx nx run website:code-pushup --', | ||
}, | ||
), | ||
).toEqual( | ||
expect.objectContaining<Partial<CommandContext>>({ | ||
project: 'website', | ||
bin: 'npx nx run website:code-pushup --', | ||
output: '.code-pushup/website', | ||
}), | ||
); | ||
}); | ||
|
||
it('should omit {project} placeholder in output path when in standalone mode', () => { | ||
expect( | ||
createCommandContext( | ||
{ | ||
...DEFAULT_SETTINGS, | ||
output: '.code-pushup/{project}', | ||
}, | ||
undefined, | ||
), | ||
).toEqual( | ||
expect.objectContaining<Partial<CommandContext>>({ | ||
output: '.code-pushup/', | ||
}), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { join } from 'node:path'; | ||
import { | ||
type PersistedCliFiles, | ||
findPersistedFiles, | ||
persistCliOptions, | ||
persistedCliFiles, | ||
} from './persist'; | ||
|
||
describe('persistCliOptions', () => { | ||
it('should create CLI arguments for standalone project', () => { | ||
expect( | ||
persistCliOptions({ | ||
directory: process.cwd(), | ||
output: '.code-pushup', | ||
}), | ||
).toEqual([ | ||
`--persist.outputDir=${join(process.cwd(), '.code-pushup')}`, | ||
'--persist.filename=report', | ||
'--persist.format=json', | ||
'--persist.format=md', | ||
]); | ||
}); | ||
|
||
it('should create CLI arguments for monorepo project', () => { | ||
expect( | ||
persistCliOptions({ | ||
project: 'utils', | ||
directory: process.cwd(), | ||
output: '.code-pushup', | ||
}), | ||
).toEqual([ | ||
`--persist.outputDir=${join(process.cwd(), '.code-pushup')}`, | ||
'--persist.filename=utils-report', | ||
'--persist.format=json', | ||
'--persist.format=md', | ||
]); | ||
}); | ||
}); | ||
|
||
describe('persistedCliFiles', () => { | ||
it('should determine persisted files for standalone report', () => { | ||
expect( | ||
persistedCliFiles({ | ||
directory: process.cwd(), | ||
output: '.code-pushup', | ||
}), | ||
).toEqual<PersistedCliFiles>({ | ||
jsonFilePath: join(process.cwd(), '.code-pushup/report.json'), | ||
mdFilePath: join(process.cwd(), '.code-pushup/report.md'), | ||
artifactData: { | ||
rootDir: join(process.cwd(), '.code-pushup'), | ||
files: [ | ||
join(process.cwd(), '.code-pushup/report.json'), | ||
join(process.cwd(), '.code-pushup/report.md'), | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should determine persisted files for monorepo report', () => { | ||
expect( | ||
persistedCliFiles({ | ||
directory: process.cwd(), | ||
output: '.code-pushup/auth', | ||
project: 'auth', | ||
}), | ||
).toEqual<PersistedCliFiles>({ | ||
jsonFilePath: join(process.cwd(), '.code-pushup/auth/auth-report.json'), | ||
mdFilePath: join(process.cwd(), '.code-pushup/auth/auth-report.md'), | ||
artifactData: { | ||
rootDir: join(process.cwd(), '.code-pushup/auth'), | ||
files: [ | ||
join(process.cwd(), '.code-pushup/auth/auth-report.json'), | ||
join(process.cwd(), '.code-pushup/auth/auth-report.md'), | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should determine persisted files for diff in Markdown format only', () => { | ||
expect( | ||
persistedCliFiles({ | ||
directory: process.cwd(), | ||
output: '.code-pushup', | ||
isDiff: true, | ||
formats: ['md'], | ||
}), | ||
).toEqual<PersistedCliFiles<'md'>>({ | ||
mdFilePath: join(process.cwd(), '.code-pushup/report-diff.md'), | ||
artifactData: { | ||
rootDir: join(process.cwd(), '.code-pushup'), | ||
files: [join(process.cwd(), '.code-pushup/report-diff.md')], | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('findPersistedFiles', () => { | ||
it('should find report files in artifact data for standalone project', () => { | ||
expect( | ||
findPersistedFiles({ | ||
rootDir: join(process.cwd(), '.code-pushup'), | ||
files: [ | ||
'report-diff.json', | ||
'report-diff.md', | ||
'report.json', | ||
'report.md', | ||
], | ||
}), | ||
).toEqual<PersistedCliFiles>({ | ||
jsonFilePath: join(process.cwd(), '.code-pushup/report.json'), | ||
mdFilePath: join(process.cwd(), '.code-pushup/report.md'), | ||
artifactData: { | ||
rootDir: join(process.cwd(), '.code-pushup'), | ||
files: [ | ||
join(process.cwd(), '.code-pushup/report.json'), | ||
join(process.cwd(), '.code-pushup/report.md'), | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should find report files in artifact data for monorepo project', () => { | ||
expect( | ||
findPersistedFiles({ | ||
rootDir: join(process.cwd(), '.code-pushup'), | ||
files: [ | ||
'backend-report-diff.json', | ||
'backend-report-diff.md', | ||
'backend-report.json', | ||
'backend-report.md', | ||
'frontend-report-diff.json', | ||
'frontend-report-diff.md', | ||
'frontend-report.json', | ||
'frontend-report.md', | ||
'report-diff.md', | ||
], | ||
project: 'frontend', | ||
}), | ||
).toEqual<PersistedCliFiles>({ | ||
jsonFilePath: join(process.cwd(), '.code-pushup/frontend-report.json'), | ||
mdFilePath: join(process.cwd(), '.code-pushup/frontend-report.md'), | ||
artifactData: { | ||
rootDir: join(process.cwd(), '.code-pushup'), | ||
files: [ | ||
join(process.cwd(), '.code-pushup/frontend-report.json'), | ||
join(process.cwd(), '.code-pushup/frontend-report.md'), | ||
], | ||
}, | ||
}); | ||
}); | ||
}); |