Skip to content

Commit 0c2f8c9

Browse files
committed
refactor: Fix and improve tests/impl
1 parent e07b881 commit 0c2f8c9

3 files changed

Lines changed: 13 additions & 19 deletions

File tree

packages/cli/bin/ui5.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ const ui5 = {
100100
await profile.start();
101101
}
102102
const {default: cli} = await import("../lib/cli/cli.js");
103-
const {command} = await cli(pkg);
103+
const argv = await cli(pkg);
104104

105105
// Stop profiling after CLI finished execution
106106
// Except for "serve" command, which continues running and only stops on sigint (see profile.js)
107-
if (profile && command !== "serve") {
107+
if (profile && argv._[0] !== "serve") {
108108
await profile.stop();
109109
}
110110
},

packages/cli/lib/cli/cli.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,5 @@ export default async (pkg) => {
6868
// Format terminal output to full available width
6969
cli.wrap(cli.terminalWidth());
7070

71-
const {_} = await cli.argv;
72-
return {
73-
command: _[0]
74-
};
71+
return cli.parse();
7572
};

packages/cli/test/lib/cli/cli.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ test.beforeEach(async (t) => {
1111
notify: t.context.updateNotifierNotify
1212
}).named("updateNotifier");
1313

14-
t.context.argvGetter = sinon.stub();
14+
t.context.yargsHideBin = sinon.stub().named("hideBin").returns([]);
15+
1516
t.context.yargsInstance = {
1617
parserConfiguration: sinon.stub(),
1718
version: sinon.stub(),
1819
scriptName: sinon.stub(),
1920
command: sinon.stub(),
2021
terminalWidth: sinon.stub().returns(123),
2122
wrap: sinon.stub(),
22-
get argv() {
23-
t.context.argvGetter();
24-
return undefined;
25-
}
23+
parse: sinon.stub().resolves({_: []})
2624
};
2725

2826
t.context.yargs = sinon.stub().returns(t.context.yargsInstance).named("yargs");
@@ -54,10 +52,9 @@ test.beforeEach(async (t) => {
5452
t.context.cli = await esmock.p("../../../lib/cli/cli.js", {
5553
"update-notifier": t.context.updateNotifier,
5654
"yargs": t.context.yargs,
57-
// TODO: Somehow esmock is unable to mock this import
58-
// "yargs/helpers": {
59-
// hideBin: t.context.yargsHideBin
60-
// },
55+
"yargs/helpers": {
56+
hideBin: t.context.yargsHideBin
57+
},
6158
"../../../lib/cli/version.js": {
6259
setVersion: t.context.setVersion
6360
},
@@ -76,7 +73,7 @@ test.afterEach.always((t) => {
7673

7774
test.serial("CLI", async (t) => {
7875
const {
79-
cli, updateNotifier, updateNotifierNotify, argvGetter, yargsInstance, yargs,
76+
cli, updateNotifier, updateNotifierNotify, yargsInstance, yargs,
8077
setVersion, cliBase
8178
} = t.context;
8279

@@ -131,8 +128,8 @@ test.serial("CLI", async (t) => {
131128
t.is(yargsInstance.wrap.callCount, 1);
132129
t.deepEqual(yargsInstance.wrap.getCall(0).args, [123]);
133130

134-
t.is(argvGetter.callCount, 1);
135-
t.deepEqual(argvGetter.getCall(0).args, []);
131+
t.is(yargsInstance.parse.callCount, 1);
132+
t.deepEqual(yargsInstance.parse.getCall(0).args, []);
136133

137134
sinon.assert.callOrder(
138135
updateNotifier,
@@ -146,7 +143,7 @@ test.serial("CLI", async (t) => {
146143
yargsInstance.command,
147144
yargsInstance.terminalWidth,
148145
yargsInstance.wrap,
149-
argvGetter
146+
yargsInstance.parse
150147
);
151148
});
152149

0 commit comments

Comments
 (0)