Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get all tests passing
  • Loading branch information
mcmire committed Feb 26, 2025
commit 1e90ba3b55f8f28c6caead98a66dc1bb8e051918
35 changes: 15 additions & 20 deletions src/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ describe('editor', () => {
vitest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: 'editor' });
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
.calledWith('editor')
.thenResolve(null);
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
const resolveExecutableSpy = vitest.spyOn(miscUtils, 'resolveExecutable');
when(resolveExecutableSpy).calledWith('editor').thenResolve(null);
when(resolveExecutableSpy)
.calledWith('code')
.thenResolve('/path/to/code');

Expand All @@ -43,12 +42,9 @@ describe('editor', () => {
vitest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: 'editor' });
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
.calledWith('editor')
.thenResolve(null);
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
.calledWith('code')
.thenResolve(null);
const resolveExecutableSpy = vitest.spyOn(miscUtils, 'resolveExecutable');
when(resolveExecutableSpy).calledWith('editor').thenResolve(null);
when(resolveExecutableSpy).calledWith('code').thenResolve(null);

expect(await determineEditor()).toBeNull();
});
Expand All @@ -57,10 +53,9 @@ describe('editor', () => {
vitest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: 'editor' });
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
.calledWith('editor')
.thenResolve(null);
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
const resolveExecutableSpy = vitest.spyOn(miscUtils, 'resolveExecutable');
when(resolveExecutableSpy).calledWith('editor').thenResolve(null);
when(resolveExecutableSpy)
.calledWith('code')
.thenReject(new Error('some error'));

Expand All @@ -71,12 +66,11 @@ describe('editor', () => {
vitest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: 'editor' });
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
const resolveExecutableSpy = vitest.spyOn(miscUtils, 'resolveExecutable');
when(resolveExecutableSpy)
.calledWith('editor')
.thenReject(new Error('some error'));
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
.calledWith('code')
.thenResolve(null);
when(resolveExecutableSpy).calledWith('code').thenResolve(null);

expect(await determineEditor()).toBeNull();
});
Expand All @@ -85,10 +79,11 @@ describe('editor', () => {
vitest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: 'editor' });
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
const resolveExecutableSpy = vitest.spyOn(miscUtils, 'resolveExecutable');
when(resolveExecutableSpy)
.calledWith('editor')
.thenReject(new Error('some error'));
when(vitest.spyOn(miscUtils, 'resolveExecutable'))
when(resolveExecutableSpy)
.calledWith('code')
.thenReject(new Error('some error'));

Expand Down
8 changes: 4 additions & 4 deletions src/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ describe('fs', () => {

await expect(
fs.promises.readdir(path.join(sandbox.directoryPath, 'foo')),
).toResolve();
).toBeDefined();
await expect(
fs.promises.readdir(path.join(sandbox.directoryPath, 'foo', 'bar')),
).toResolve();
).toBeDefined();
await expect(
fs.promises.readdir(
path.join(sandbox.directoryPath, 'foo', 'bar', 'baz'),
),
).toResolve();
).toBeDefined();
});
});

Expand All @@ -228,7 +228,7 @@ describe('fs', () => {
path.join(sandbox.directoryPath, 'foo', 'bar', 'baz'),
);

await expect(ensureDirectoryPathExists(directoryPath)).toResolve();
await expect(ensureDirectoryPathExists(directoryPath)).toBeDefined();
});
});

Expand Down
15 changes: 11 additions & 4 deletions src/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ describe('project', () => {
vitest
.spyOn(actionUtils, 'getWorkspaceLocations')
.mockResolvedValue(['packages/a', 'packages/subpackages/b']);
when(vitest.spyOn(packageModule, 'readMonorepoWorkspacePackage'))
const readMonorepoWorkspacePackageSpy = vitest.spyOn(
packageModule,
'readMonorepoWorkspacePackage',
);
when(readMonorepoWorkspacePackageSpy)
.calledWith({
packageDirectoryPath: path.join(
projectDirectoryPath,
Expand All @@ -89,7 +93,7 @@ describe('project', () => {
stderr,
})
.thenResolve(workspacePackages.a);
when(vitest.spyOn(packageModule, 'readMonorepoWorkspacePackage'))
when(readMonorepoWorkspacePackageSpy)
.calledWith({
packageDirectoryPath: path.join(
projectDirectoryPath,
Expand Down Expand Up @@ -127,6 +131,7 @@ describe('project', () => {
});
});
});

describe('restoreChangelogsForSkippedPackages', () => {
it('should reset changelog for packages with changes not included in release', async () => {
const project = buildMockProject({
Expand All @@ -146,11 +151,13 @@ describe('project', () => {

const restoreFilesSpy = vitest.spyOn(repoModule, 'restoreFiles');

when(vitest.spyOn(fs, 'fileExists'))
const fileExistsSpy = vitest.spyOn(fs, 'fileExists');

when(fileExistsSpy)
.calledWith(project.workspacePackages.b.changelogPath)
.thenResolve(true);

when(vitest.spyOn(fs, 'fileExists'))
when(fileExistsSpy)
.calledWith(project.workspacePackages.c.changelogPath)
.thenResolve(true);

Expand Down
60 changes: 23 additions & 37 deletions src/repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,29 @@ describe('repo', () => {
);
});

it('throws if the URL of the "origin" remote is in an invalid format', async () => {
const repositoryDirectoryPath = '/path/to/project';
when(vitest.spyOn(miscUtils, 'getStdoutFromCommand'))
.calledWith('git', ['config', '--get', 'remote.origin.url'], {
cwd: repositoryDirectoryPath,
})
// TODO: `vitest-when` doesn't have a `thenResolveOnce` method.
.mockResolvedValueOnce('foo')
.mockResolvedValueOnce('http://github.com/Foo/Bar')
.mockResolvedValueOnce('https://gitbar.foo/Foo/Bar')
.mockResolvedValueOnce('[email protected]:Foo/Bar.git')
.mockResolvedValueOnce('[email protected]:Foo/Bar.foo');

await expect(
getRepositoryHttpsUrl(repositoryDirectoryPath),
).rejects.toThrow('Unrecognized URL for git remote "origin": foo');
await expect(
getRepositoryHttpsUrl(repositoryDirectoryPath),
).rejects.toThrow(
'Unrecognized URL for git remote "origin": http://github.com/Foo/Bar',
);
await expect(
getRepositoryHttpsUrl(repositoryDirectoryPath),
).rejects.toThrow(
'Unrecognized URL for git remote "origin": https://gitbar.foo/Foo/Bar',
);
await expect(
getRepositoryHttpsUrl(repositoryDirectoryPath),
).rejects.toThrow(
'Unrecognized URL for git remote "origin": [email protected]:Foo/Bar.git',
);
await expect(
getRepositoryHttpsUrl(repositoryDirectoryPath),
).rejects.toThrow(
'Unrecognized URL for git remote "origin": [email protected]:Foo/Bar.foo',
);
});
it.each([
'foo',
'http://github.com/Foo/Bar',
'https://gitbar.foo/Foo/Bar',
'[email protected]:Foo/Bar.git',
'[email protected]:Foo/Bar.foo',
])(
'throws if the URL of the "origin" remote is in the invalid format "%s"',
async (originUrl) => {
const repositoryDirectoryPath = '/path/to/project';
when(vitest.spyOn(miscUtils, 'getStdoutFromCommand'))
.calledWith('git', ['config', '--get', 'remote.origin.url'], {
cwd: repositoryDirectoryPath,
})
.thenResolve(originUrl);

await expect(
getRepositoryHttpsUrl(repositoryDirectoryPath),
).rejects.toThrow(
`Unrecognized URL for git remote "origin": ${originUrl}`,
);
},
);
});

describe('commitAllChanges', () => {
Expand Down
33 changes: 23 additions & 10 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
watch: false,

// Vitest doesn't inject the globals by default. We enable this option to
// inject the globals like `describe`, `it`, `expect`, etc.
globals: true,

coverage: {
enabled: true,

// Configure the coverage provider. We use `istanbul` here, because it
// is more stable than `v8`.
provider: 'istanbul',

// Only include source files in the `src` directory.
include: ['src/**/*.test.ts'],

// Exclude certain files from the coverage.
exclude: ['node_modules/', 'src/cli.ts', 'src/command-line-arguments.ts'],

// Configure the coverage provider. We use `istanbul` here, because it
// is more stable than `v8`.
provider: 'istanbul',

// Hide files with 100% coverage.
skipFull: true,

Expand All @@ -30,11 +24,30 @@ export default defineConfig({
// Auto-update the coverage thresholds.
autoUpdate: true,

// These should be set to 100 at all times.
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},

// Vitest doesn't inject the globals by default. We enable this option to
// inject the globals like `describe`, `it`, `expect`, etc.
globals: true,

// Calls .mockReset on all spies before each test.
mockReset: true,

// Calls .mockRestore on all spies before each test.
restoreMocks: true,

// Calls vi.unstubAllEnvs before each test.
unstubEnvs: true,

// Calls vi.unstubGlobals before each test.
unstubGlobals: true,

watch: false,
},
});