Skip to content

Commit ebd52d5

Browse files
author
Chris Brody
authored
unit test of missing CLI args using mocks (#111)
1 parent d6d57fb commit ebd52d5

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`mocked cli-program.js shows help in case of missing args 1`] = `
4+
Array [
5+
Object {
6+
"notify": Object {},
7+
},
8+
Object {
9+
"version": "x",
10+
},
11+
Object {
12+
"usage": "[options] <name>",
13+
},
14+
Object {
15+
"description": "creates a React Native library module for one or more platforms",
16+
},
17+
Object {
18+
"action": Object {},
19+
},
20+
Object {
21+
"option": Object {
22+
"args": Array [
23+
"--prefix [prefix]",
24+
"The prefix for the library module",
25+
[Function],
26+
"",
27+
],
28+
},
29+
},
30+
Object {
31+
"option": Object {
32+
"args": Array [
33+
"--module-name [moduleName]",
34+
"The module library package name to be used in package.json. Default: react-native-(name in param-case)",
35+
[Function],
36+
undefined,
37+
],
38+
},
39+
},
40+
Object {
41+
"option": Object {
42+
"args": Array [
43+
"--module-prefix [modulePrefix]",
44+
"The module prefix for the library module, ignored if --module-name is specified",
45+
[Function],
46+
"react-native",
47+
],
48+
},
49+
},
50+
Object {
51+
"option": Object {
52+
"args": Array [
53+
"--package-identifier [packageIdentifier]",
54+
"(Android only!) The package name for the Android module",
55+
[Function],
56+
"com.reactlibrary",
57+
],
58+
},
59+
},
60+
Object {
61+
"option": Object {
62+
"args": Array [
63+
"--platforms <platforms>",
64+
"Platforms the library module will be created for - comma separated",
65+
[Function],
66+
"ios,android",
67+
],
68+
},
69+
},
70+
Object {
71+
"option": Object {
72+
"args": Array [
73+
"--github-account [githubAccount]",
74+
"The github account where the library module is hosted",
75+
[Function],
76+
"github_account",
77+
],
78+
},
79+
},
80+
Object {
81+
"option": Object {
82+
"args": Array [
83+
"--author-name [authorName]",
84+
"The author's name",
85+
[Function],
86+
"Your Name",
87+
],
88+
},
89+
},
90+
Object {
91+
"option": Object {
92+
"args": Array [
93+
"--author-email [authorEmail]",
94+
"The author's email",
95+
[Function],
96+
97+
],
98+
},
99+
},
100+
Object {
101+
"option": Object {
102+
"args": Array [
103+
"--license [license]",
104+
"The license type",
105+
[Function],
106+
"MIT",
107+
],
108+
},
109+
},
110+
Object {
111+
"option": Object {
112+
"args": Array [
113+
"--view",
114+
"Generate the module as a very simple native view component",
115+
[Function],
116+
undefined,
117+
],
118+
},
119+
},
120+
Object {
121+
"option": Object {
122+
"args": Array [
123+
"--use-cocoapods",
124+
"Generate a library with a sample podspec and third party pod usage example",
125+
[Function],
126+
undefined,
127+
],
128+
},
129+
},
130+
Object {
131+
"option": Object {
132+
"args": Array [
133+
"--generate-example",
134+
"Generate an example project and links the library module to it, requires both react-native-cli and yarn to be installed globally",
135+
[Function],
136+
undefined,
137+
],
138+
},
139+
},
140+
Object {
141+
"option": Object {
142+
"args": Array [
143+
"--example-name [exampleName]",
144+
"Name for the example project",
145+
[Function],
146+
"example",
147+
],
148+
},
149+
},
150+
Object {
151+
"option": Object {
152+
"args": Array [
153+
"--example-react-native-version [exampleReactNativeVersion]",
154+
"React Native version for the generated example project",
155+
[Function],
156+
157+
],
158+
},
159+
},
160+
Object {
161+
"parse": Object {
162+
"argv": Array [
163+
"node",
164+
"./bin/cli.js",
165+
"--help",
166+
],
167+
},
168+
},
169+
]
170+
`;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// special compact mocks for this test:
2+
const mysnap = [];
3+
const mockpushit = x => mysnap.push(x);
4+
jest.mock('update-notifier', () => ({ pkg }) => {
5+
// only check a limited number of fields in pkg
6+
expect(pkg.name).toBeDefined();
7+
expect(pkg.version).toBeDefined();
8+
return { notify: () => mockpushit({ notify: {} }) };
9+
});
10+
const mockCommander = {
11+
args: ['test-package'],
12+
version: (version) => {
13+
// actual value of version not expected to be stable
14+
expect(version).toBeDefined();
15+
mockpushit({ version: 'x' });
16+
return mockCommander;
17+
},
18+
usage: (usage) => {
19+
mockpushit({ usage });
20+
return mockCommander;
21+
},
22+
description: (description) => {
23+
mockpushit({ description });
24+
return mockCommander;
25+
},
26+
action: (_) => {
27+
mockpushit({ action: {} });
28+
return mockCommander;
29+
},
30+
option: (...args) => {
31+
mockpushit({ option: { args } });
32+
return mockCommander;
33+
},
34+
parse: (argv) => {
35+
// ensure that cli-program.js adds the `--help` option:
36+
expect(argv.length).toBe(3);
37+
expect(argv[2]).toBe('--help');
38+
mockpushit({ parse: { argv } });
39+
},
40+
};
41+
jest.mock('commander', () => mockCommander);
42+
43+
// TBD hackish mock(s) - testing with missing CLI arguments:
44+
process.argv = ['node', './bin/cli.js'];
45+
46+
test('mocked cli-program.js shows help in case of missing args', () => {
47+
// FUTURE TBD define this kind of a relative path near the beginning
48+
// of the test script
49+
require('../../../../../lib/cli-program.js');
50+
51+
// Using a 1 ms timer to wait for the
52+
// CLI program func to finish.
53+
// FUTURE TBD this looks like a bad smell
54+
// that should be resolved someday.
55+
return new Promise((resolve) => setTimeout(resolve, 1))
56+
.then(() => { expect(mysnap).toMatchSnapshot(); });
57+
});

0 commit comments

Comments
 (0)