Skip to content

Commit 57da2bd

Browse files
committed
test: add test for the init command
1 parent bb5bece commit 57da2bd

File tree

7 files changed

+695
-461
lines changed

7 files changed

+695
-461
lines changed

packages/react-native-builder-bob/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"@types/yargs": "^17.0.10",
8686
"concurrently": "^7.2.2",
8787
"jest": "^29.7.0",
88-
"mock-fs": "^5.2.0"
88+
"mock-fs": "^5.2.0",
89+
"mock-stdin": "^1.0.0"
8990
}
9091
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`initializes the configuration 1`] = `
4+
"{
5+
"name": "library",
6+
"version": "1.0.0",
7+
"devDependencies": {
8+
"react-native-builder-bob": "^0.38.1"
9+
},
10+
"source": "./src/index.ts",
11+
"main": "./lib/commonjs/index.js",
12+
"module": "./lib/module/index.js",
13+
"types": "./lib/typescript/commonjs/src/index.d.ts",
14+
"exports": {
15+
".": {
16+
"import": {
17+
"types": "./lib/typescript/module/src/index.d.ts",
18+
"default": "./lib/module/index.js"
19+
},
20+
"require": {
21+
"types": "./lib/typescript/commonjs/src/index.d.ts",
22+
"default": "./lib/commonjs/index.js"
23+
}
24+
}
25+
},
26+
"scripts": {
27+
"prepare": "bob build"
28+
},
29+
"files": [
30+
"src",
31+
"lib",
32+
"!**/__tests__",
33+
"!**/__fixtures__",
34+
"!**/__mocks__"
35+
],
36+
"react-native-builder-bob": {
37+
"source": "src",
38+
"output": "lib",
39+
"targets": [
40+
[
41+
"module",
42+
{
43+
"esm": true
44+
}
45+
],
46+
[
47+
"commonjs",
48+
{
49+
"esm": true
50+
}
51+
],
52+
[
53+
"typescript",
54+
{
55+
"esm": true
56+
}
57+
]
58+
]
59+
},
60+
"eslintIgnore": [
61+
"node_modules/",
62+
"lib/"
63+
]
64+
}
65+
"
66+
`;
67+
68+
exports[`initializes the configuration 2`] = `
69+
"{
70+
"compilerOptions": {
71+
"rootDir": ".",
72+
"allowUnreachableCode": false,
73+
"allowUnusedLabels": false,
74+
"esModuleInterop": true,
75+
"forceConsistentCasingInFileNames": true,
76+
"jsx": "react-jsx",
77+
"lib": [
78+
"ESNext"
79+
],
80+
"module": "ESNext",
81+
"moduleResolution": "bundler",
82+
"noFallthroughCasesInSwitch": true,
83+
"noImplicitReturns": true,
84+
"noImplicitUseStrict": false,
85+
"noStrictGenericChecks": false,
86+
"noUncheckedIndexedAccess": true,
87+
"noUnusedLocals": true,
88+
"noUnusedParameters": true,
89+
"resolveJsonModule": true,
90+
"skipLibCheck": true,
91+
"strict": true,
92+
"target": "ESNext",
93+
"verbatimModuleSyntax": true
94+
}
95+
}
96+
"
97+
`;

packages/react-native-builder-bob/src/__tests__/index.test.ts renamed to packages/react-native-builder-bob/src/__tests__/babel.test.ts

File renamed without changes.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { afterEach, beforeEach, expect, it, jest } from '@jest/globals';
2+
import { readFile } from 'fs-extra';
3+
import mockFs from 'mock-fs';
4+
import { stdin } from 'mock-stdin';
5+
import { join } from 'path';
6+
import { init } from '../init';
7+
8+
let io: ReturnType<typeof stdin> | undefined;
9+
10+
const root = '/path/to/library';
11+
12+
const enter = '\x0D';
13+
14+
const waitFor = async (callback: () => void) => {
15+
const interval = 10;
16+
17+
let timeout = 50;
18+
19+
return new Promise((resolve, reject) => {
20+
const intervalId = setInterval(() => {
21+
try {
22+
callback();
23+
clearInterval(intervalId);
24+
resolve(undefined);
25+
} catch (error) {
26+
if (timeout <= 0) {
27+
clearInterval(intervalId);
28+
reject(error);
29+
}
30+
31+
timeout -= interval;
32+
}
33+
}, interval);
34+
});
35+
};
36+
37+
beforeEach(() => {
38+
io = stdin();
39+
40+
mockFs({
41+
[root]: {
42+
'package.json': JSON.stringify({
43+
name: 'library',
44+
version: '1.0.0',
45+
}),
46+
'src': {
47+
'index.ts': "export default 'hello world';",
48+
},
49+
},
50+
});
51+
});
52+
53+
afterEach(() => {
54+
io?.restore();
55+
mockFs.restore();
56+
});
57+
58+
it('initializes the configuration', async () => {
59+
jest.spyOn(process.stdout, 'write').mockImplementation(() => true);
60+
61+
process.chdir(root);
62+
63+
const run = async () => {
64+
await waitFor(() =>
65+
expect(process.stdout.write).toHaveBeenLastCalledWith(
66+
expect.stringMatching('The working directory is not clean')
67+
)
68+
);
69+
70+
io?.send('y');
71+
72+
await waitFor(() =>
73+
expect(process.stdout.write).toHaveBeenLastCalledWith(
74+
expect.stringMatching('Where are your source files?')
75+
)
76+
);
77+
78+
io?.send(enter);
79+
80+
await waitFor(() =>
81+
expect(process.stdout.write).toHaveBeenLastCalledWith(
82+
expect.stringMatching('Where do you want to generate the output files?')
83+
)
84+
);
85+
86+
io?.send(enter);
87+
88+
await waitFor(() =>
89+
expect(process.stdout.write).toHaveBeenLastCalledWith(
90+
expect.stringMatching('Which targets do you want to build?')
91+
)
92+
);
93+
94+
io?.send(enter);
95+
96+
await waitFor(() =>
97+
expect(process.stdout.write).toHaveBeenLastCalledWith(
98+
expect.stringMatching(
99+
"You have enabled 'typescript' compilation, but we couldn't find a 'tsconfig.json' in project root"
100+
)
101+
)
102+
);
103+
104+
io?.send(enter);
105+
};
106+
107+
await Promise.all([run(), init()]);
108+
109+
expect(process.stdout.write).toHaveBeenLastCalledWith(
110+
expect.stringMatching('configured successfully!')
111+
);
112+
113+
expect(await readFile(join(root, 'package.json'), 'utf8')).toMatchSnapshot();
114+
115+
expect(await readFile(join(root, 'tsconfig.json'), 'utf8')).toMatchSnapshot();
116+
});

0 commit comments

Comments
 (0)