Skip to content

unit test of missing CLI args using mocks #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mocked cli-program.js shows help in case of missing args 1`] = `
Array [
Object {
"notify": Object {},
},
Object {
"version": "x",
},
Object {
"usage": "[options] <name>",
},
Object {
"description": "creates a React Native library module for one or more platforms",
},
Object {
"action": Object {},
},
Object {
"option": Object {
"args": Array [
"--prefix [prefix]",
"The prefix for the library module",
[Function],
"",
],
},
},
Object {
"option": Object {
"args": Array [
"--module-name [moduleName]",
"The module library package name to be used in package.json. Default: react-native-(name in param-case)",
[Function],
undefined,
],
},
},
Object {
"option": Object {
"args": Array [
"--module-prefix [modulePrefix]",
"The module prefix for the library module, ignored if --module-name is specified",
[Function],
"react-native",
],
},
},
Object {
"option": Object {
"args": Array [
"--package-identifier [packageIdentifier]",
"(Android only!) The package name for the Android module",
[Function],
"com.reactlibrary",
],
},
},
Object {
"option": Object {
"args": Array [
"--platforms <platforms>",
"Platforms the library module will be created for - comma separated",
[Function],
"ios,android",
],
},
},
Object {
"option": Object {
"args": Array [
"--github-account [githubAccount]",
"The github account where the library module is hosted",
[Function],
"github_account",
],
},
},
Object {
"option": Object {
"args": Array [
"--author-name [authorName]",
"The author's name",
[Function],
"Your Name",
],
},
},
Object {
"option": Object {
"args": Array [
"--author-email [authorEmail]",
"The author's email",
[Function],
"[email protected]",
],
},
},
Object {
"option": Object {
"args": Array [
"--license [license]",
"The license type",
[Function],
"MIT",
],
},
},
Object {
"option": Object {
"args": Array [
"--view",
"Generate the module as a very simple native view component",
[Function],
undefined,
],
},
},
Object {
"option": Object {
"args": Array [
"--use-cocoapods",
"Generate a library with a sample podspec and third party pod usage example",
[Function],
undefined,
],
},
},
Object {
"option": Object {
"args": Array [
"--generate-example",
"Generate an example project and links the library module to it, requires both react-native-cli and yarn to be installed globally",
[Function],
undefined,
],
},
},
Object {
"option": Object {
"args": Array [
"--example-name [exampleName]",
"Name for the example project",
[Function],
"example",
],
},
},
Object {
"option": Object {
"args": Array [
"--example-react-native-version [exampleReactNativeVersion]",
"React Native version for the generated example project",
[Function],
"[email protected]",
],
},
},
Object {
"parse": Object {
"argv": Array [
"node",
"./bin/cli.js",
"--help",
],
},
},
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// special compact mocks for this test:
const mysnap = [];
const mockpushit = x => mysnap.push(x);
jest.mock('update-notifier', () => ({ pkg }) => {
// only check a limited number of fields in pkg
expect(pkg.name).toBeDefined();
expect(pkg.version).toBeDefined();
return { notify: () => mockpushit({ notify: {} }) };
});
const mockCommander = {
args: ['test-package'],
version: (version) => {
// actual value of version not expected to be stable
expect(version).toBeDefined();
mockpushit({ version: 'x' });
return mockCommander;
},
usage: (usage) => {
mockpushit({ usage });
return mockCommander;
},
description: (description) => {
mockpushit({ description });
return mockCommander;
},
action: (_) => {
mockpushit({ action: {} });
return mockCommander;
},
option: (...args) => {
mockpushit({ option: { args } });
return mockCommander;
},
parse: (argv) => {
// ensure that cli-program.js adds the `--help` option:
expect(argv.length).toBe(3);
expect(argv[2]).toBe('--help');
mockpushit({ parse: { argv } });
},
};
jest.mock('commander', () => mockCommander);

// TBD hackish mock(s) - testing with missing CLI arguments:
process.argv = ['node', './bin/cli.js'];

test('mocked cli-program.js shows help in case of missing args', () => {
// FUTURE TBD define this kind of a relative path near the beginning
// of the test script
require('../../../../../lib/cli-program.js');

// Using a 1 ms timer to wait for the
// CLI program func to finish.
// FUTURE TBD this looks like a bad smell
// that should be resolved someday.
return new Promise((resolve) => setTimeout(resolve, 1))
.then(() => { expect(mysnap).toMatchSnapshot(); });
});