Skip to content

Commit de87f99

Browse files
author
Chris Brody
authored
test CLI command func error with logging (#162)
for coverage of some error logging code in `lib/cli-command.js`
1 parent 2773429 commit de87f99

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`create alice-bobbi module with logging, with fs error (with defaults for Android & iOS) 1`] = `
4+
Array [
5+
Object {
6+
"warn": Array [
7+
"While \`{DEFAULT_PACKAGE_IDENTIFIER}\` is the default package
8+
identifier, it is recommended to customize the package identifier.",
9+
],
10+
},
11+
Object {
12+
"info": Array [
13+
"CREATE new React Native module with the following options:
14+
15+
root moduleName: react-native-alice-bobbi
16+
name: alice-bobbi
17+
prefix:
18+
modulePrefix: react-native
19+
packageIdentifier: com.reactlibrary
20+
platforms: android,ios
21+
githubAccount: github_account
22+
authorName: Your Name
23+
authorEmail: yourname@email.com
24+
license: MIT
25+
view: false
26+
useCocoapods: false
27+
generateExample: false
28+
exampleName: example
29+
",
30+
],
31+
},
32+
Object {
33+
"info": Array [
34+
"CREATE: Generating the React Native library module",
35+
],
36+
},
37+
Object {
38+
"ensureDir": "react-native-alice-bobbi",
39+
},
40+
Object {
41+
"error": Array [
42+
"Error while creating library module react-native-alice-bobbi",
43+
],
44+
},
45+
Object {
46+
"error": Array [
47+
"Error: ENOPERM not permitted
48+
at Object.ensureDir (.../tests/with-mocks/cli/command/func/with-logging/with-error/cli-command-with-logging-with-error.test.js:9:27)
49+
at ensureDir (.../lib/lib.js:147:15)
50+
at generateLibraryModule (.../lib/lib.js:240:10)
51+
at generateWithNormalizedOptions (.../lib/lib.js:257:10)
52+
at createLibraryModule (.../lib/cli-command.js:...
53+
at Object.func (.../tests/with-mocks/cli/command/func/with-logging/with-error/cli-command-with-logging-with-error.test.js:62:9)
54+
",
55+
],
56+
},
57+
]
58+
`;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const func = require('../../../../../../../lib/cli-command.js').func;
2+
3+
// special compact mocks for this test:
4+
const mysnap = [];
5+
const mockpushit = x => mysnap.push(x);
6+
jest.mock('fs-extra', () => ({
7+
ensureDir: (dir) => {
8+
mockpushit({ ensureDir: dir });
9+
return Promise.reject(new Error(`ENOPERM not permitted`));
10+
},
11+
outputFile: (outputFileName, theContent) => {
12+
// NOT EXPECTED:
13+
mockpushit({ outputFileName, theContent });
14+
return Promise.reject(new Error(`ENOPERM not permitted`));
15+
},
16+
}));
17+
18+
// TBD hackish mock:
19+
global.console = {
20+
info: (...args) => {
21+
mockpushit({ info: [].concat(args) });
22+
},
23+
log: (...args) => {
24+
mockpushit({
25+
// TBD EXTRA WORKAROUND HACK for non-deterministic elapsed time in log
26+
log: args.map(line => line.replace(/It took.*s/g, 'It took XXX'))
27+
});
28+
},
29+
warn: (...args) => {
30+
mockpushit({ warn: [].concat(args) });
31+
},
32+
error: (first, ...rest) => {
33+
mockpushit({
34+
error: [].concat(
35+
[].concat(
36+
first
37+
// Check trace with relative path
38+
// THANKS for guidance:
39+
// * https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string/1145525#1145525
40+
// * https://github.com/tunnckoCore/clean-stacktrace-relative-paths/blob/v1.0.4/index.js#L59
41+
.split(process.cwd()).join('...')
42+
// IGNORE test-dependant trace info
43+
.split(/at.*JestTest/)[0]
44+
// IGNORE line number in cli-command.js
45+
// in order to avoid sensitivity to mutation testing
46+
// (miss potentially surviving mutants)
47+
.replace(/cli-command.js:.*/, 'cli-command.js:...')
48+
// WORKAROUND for Windows:
49+
.replace(/\\/g, '/')
50+
),
51+
rest
52+
)
53+
});
54+
},
55+
};
56+
57+
test('create alice-bobbi module with logging, with fs error (with defaults for Android & iOS)', async () => {
58+
const args = ['alice-bobbi'];
59+
60+
const config = 'bogus';
61+
62+
await func(args, config, {});
63+
64+
expect(mysnap).toMatchSnapshot();
65+
});

0 commit comments

Comments
 (0)