|
1 |
| -import { describe, expect, it, beforeAll, afterEach, afterAll } from '@jest/globals'; |
| 1 | +import { describe, expect, it, beforeAll, afterAll, jest } from '@jest/globals'; |
2 | 2 | import fs from 'fs-extra';
|
3 |
| -import type { Repository } from '../../__fixtures__/repository'; |
4 | 3 | import { RepositoryFactory } from '../../__fixtures__/repositoryFactory';
|
5 | 4 | import { getOptions } from '../../options/getOptions';
|
| 5 | +import type { RepoOptions } from '../../types/BeachballOptions'; |
6 | 6 |
|
7 |
| -const baseArgv = ['node.exe', 'bin.js']; |
| 7 | +// Return a new object each time since getRepoOptions caches the result based on object identity. |
| 8 | +const baseArgv = () => ['node.exe', 'bin.js']; |
8 | 9 |
|
9 | 10 | describe('getOptions', () => {
|
10 | 11 | let repositoryFactory: RepositoryFactory;
|
11 |
| - let repo: Repository; |
| 12 | + // Don't reuse a repo in these tests! If multiple tests load beachball.config.js from the same path, |
| 13 | + // it will use the version from the require cache, which will have outdated contents. |
12 | 14 |
|
13 | 15 | beforeAll(() => {
|
14 | 16 | repositoryFactory = new RepositoryFactory('single');
|
15 |
| - repo = repositoryFactory.cloneRepository(); |
16 |
| - }); |
17 |
| - |
18 |
| - afterEach(() => { |
19 |
| - repo.git(['clean', '-fdx']); |
| 17 | + jest.spyOn(console, 'log').mockImplementation(() => {}); |
20 | 18 | });
|
21 | 19 |
|
22 | 20 | afterAll(() => {
|
23 | 21 | repositoryFactory.cleanUp();
|
| 22 | + jest.restoreAllMocks(); |
24 | 23 | });
|
25 | 24 |
|
26 | 25 | it('uses the branch name defined in beachball.config.js', () => {
|
| 26 | + const repo = repositoryFactory.cloneRepository(); |
27 | 27 | const config = inDirectory(repo.rootPath, () => {
|
28 | 28 | fs.writeFileSync('beachball.config.js', 'module.exports = { branch: "origin/foo" };');
|
29 |
| - return getOptions(baseArgv); |
| 29 | + return getOptions(baseArgv()); |
| 30 | + }); |
| 31 | + expect(config.branch).toEqual('origin/foo'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('reads config from package.json', () => { |
| 35 | + const repo = repositoryFactory.cloneRepository(); |
| 36 | + const config = inDirectory(repo.rootPath, () => { |
| 37 | + fs.writeJsonSync('package.json', { beachball: { branch: 'origin/foo' } }); |
| 38 | + return getOptions(baseArgv()); |
| 39 | + }); |
| 40 | + expect(config.branch).toEqual('origin/foo'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('finds a .beachballrc.json file', () => { |
| 44 | + const repo = repositoryFactory.cloneRepository(); |
| 45 | + const config = inDirectory(repo.rootPath, () => { |
| 46 | + fs.writeJsonSync('.beachballrc.json', { branch: 'origin/foo' }); |
| 47 | + return getOptions(baseArgv()); |
30 | 48 | });
|
31 | 49 | expect(config.branch).toEqual('origin/foo');
|
32 | 50 | });
|
33 | 51 |
|
34 | 52 | it('--config overrides configuration path', () => {
|
| 53 | + const repo = repositoryFactory.cloneRepository(); |
35 | 54 | const config = inDirectory(repo.rootPath, () => {
|
36 | 55 | fs.writeFileSync('beachball.config.js', 'module.exports = { branch: "origin/main" };');
|
37 | 56 | fs.writeFileSync('alternate.config.js', 'module.exports = { branch: "origin/foo" };');
|
38 |
| - return getOptions([...baseArgv, '--config', 'alternate.config.js']); |
| 57 | + return getOptions([...baseArgv(), '--config', 'alternate.config.js']); |
39 | 58 | });
|
40 | 59 | expect(config.branch).toEqual('origin/foo');
|
41 | 60 | });
|
| 61 | + |
| 62 | + it('merges options including objects', () => { |
| 63 | + const repo = repositoryFactory.cloneRepository(); |
| 64 | + // Ideally this test should include nested objects from multiple sources, but as of writing, |
| 65 | + // the only place that can have nested objects is the repo options. |
| 66 | + const repoOptions: Partial<RepoOptions> = { |
| 67 | + access: 'public', |
| 68 | + publish: false, |
| 69 | + disallowedChangeTypes: null, |
| 70 | + changelog: { |
| 71 | + groups: [{ masterPackageName: 'foo', include: ['foo'], changelogPath: '.' }], |
| 72 | + }, |
| 73 | + }; |
| 74 | + const config = inDirectory(repo.rootPath, () => { |
| 75 | + fs.writeFileSync('beachball.config.js', `module.exports = ${JSON.stringify(repoOptions)};`); |
| 76 | + return getOptions([...baseArgv(), '--disallowed-change-types', 'patch']); |
| 77 | + }); |
| 78 | + expect(config).toMatchObject({ |
| 79 | + access: 'public', |
| 80 | + publish: false, |
| 81 | + disallowedChangeTypes: ['patch'], |
| 82 | + }); |
| 83 | + expect(config.changelog).toEqual(repoOptions.changelog); |
| 84 | + }); |
42 | 85 | });
|
43 | 86 |
|
44 | 87 | const inDirectory = <T>(directory: string, cb: () => T): T => {
|
|
0 commit comments