Skip to content

Commit 86a7f26

Browse files
committed
Fix: Use empty prerelease default for pre-major bump
Fixes #786
1 parent 2677fcc commit 86a7f26

3 files changed

Lines changed: 67 additions & 9 deletions

File tree

source/ui.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,7 @@ const ui = async ({packageManager, ...options}, {package_, rootDirectory}) => {
414414
prereleasePrefix: {
415415
type: 'input',
416416
message: 'Prerelease identifier',
417-
// Use || not ?? to treat empty string as falsy (show 'rc' instead of empty default)
418-
default: defaultPrereleasePrefix || 'rc',
417+
default: defaultPrereleasePrefix,
419418
when(answers) {
420419
// Only ask when a prerelease increment was selected from the menu
421420
if (!answers.version) {

test/_helpers/mock-inquirer.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ Logs for debugging are outputted on test failure.
2424
@param {Answers} o.inputAnswers Test input
2525
@param {Record<string, Prompt> | Prompt[]} o.prompts Actual prompts
2626
*/
27-
const mockPrompt = async ({t, inputAnswers, prompts}) => {
28-
const answers = {};
29-
30-
// Ensure `prompts` is an object
27+
const getPromptsObject = prompts => {
3128
if (Array.isArray(prompts)) {
3229
const promptsObject = {};
3330

3431
for (const prompt of prompts) {
3532
promptsObject[prompt.name] = prompt;
3633
}
3734

38-
prompts = promptsObject;
35+
return promptsObject;
3936
}
4037

38+
return prompts;
39+
};
40+
41+
const mockPrompt = async ({t, inputAnswers, prompts}) => {
42+
const answers = {};
43+
prompts = getPromptsObject(prompts);
44+
4145
t.log('prompts:', Object.keys(prompts));
4246

4347
/* eslint-disable no-await-in-loop */
@@ -193,19 +197,23 @@ Mocks `inquirer` for testing `source/ui.js`.
193197
@param {ExecutionContext} o.t
194198
@param {Answers} o.answers Test input
195199
@param {import('esmock').MockMap} [o.mocks] Optional global mocks
200+
@param {(prompts: Record<string, Prompt>) => void} [o.onPrompt] Optional hook to inspect prompts
196201
*/
197-
export const mockInquirer = async ({t, answers, mocks = {}}) => {
202+
export const mockInquirer = async ({t, answers, mocks = {}, onPrompt = () => {}}) => {
198203
/** @type {string[]} */
199204
const logs = [];
200205

201206
/** @type {import('../../source/ui.js')} */
202207
const ui = await esmock('../../source/ui.js', import.meta.url, {
203208
inquirer: {
204209
async prompt(prompts) {
210+
const promptDescriptors = getPromptsObject(prompts);
211+
onPrompt(promptDescriptors);
212+
205213
let uiAnswers = {};
206214

207215
const assertions = await t.try(async tt => {
208-
uiAnswers = await mockPrompt({t: tt, inputAnswers: answers, prompts});
216+
uiAnswers = await mockPrompt({t: tt, inputAnswers: answers, prompts: promptDescriptors});
209217
});
210218

211219
assertions.commit({retainLogs: !assertions.passed});

test/ui/prompts/version.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,54 @@ test('releaseDraftOnly does not throw on current version', async t => {
233233
t.true(results.confirm);
234234
t.is(results.version, '1.0.0');
235235
});
236+
237+
test('uses empty prerelease identifier by default when no prerelease identifier is configured', async t => {
238+
let capturedPrereleasePrefixPrompt;
239+
240+
const {ui} = await mockInquirer({
241+
t,
242+
answers: {
243+
version: 'premajor',
244+
prereleasePrefix: '',
245+
},
246+
onPrompt(prompts) {
247+
capturedPrereleasePrefixPrompt = prompts.prereleasePrefix;
248+
},
249+
mocks: {
250+
'./npm/util.js': {
251+
checkIgnoreStrategy: sinon.stub().resolves(),
252+
},
253+
'./util.js': {
254+
getNewFiles: sinon.stub().resolves({unpublished: [], firstTime: []}),
255+
getNewDependencies: sinon.stub().resolves([]),
256+
getPreReleasePrefix: sinon.stub().resolves(''),
257+
},
258+
'./git-util.js': {
259+
latestTagOrFirstCommit: sinon.stub().resolves('v1.0.0'),
260+
commitLogFromRevision: sinon.stub().resolves('abc 123'),
261+
},
262+
execa: {
263+
execa: sinon.stub().resolves({stdout: 'https://registry.npmjs.org/'}),
264+
},
265+
},
266+
});
267+
268+
const {version} = await ui({
269+
packageManager,
270+
runPublish: false,
271+
availability: {},
272+
}, {
273+
package_: {
274+
name: 'foo',
275+
version: '1.0.0',
276+
files: ['*'],
277+
repository: {
278+
url: 'https://github.com/foo/bar',
279+
},
280+
},
281+
});
282+
283+
t.truthy(capturedPrereleasePrefixPrompt);
284+
t.is(capturedPrereleasePrefixPrompt.default, '', 'the prerelease identifier default should remain empty');
285+
t.is(version.toString(), '2.0.0-0');
286+
});

0 commit comments

Comments
 (0)