1 | const coffee = require('coffee');
|
2 | const open = require('open');
|
3 | const chalk = require('chalk');
|
4 |
|
5 | const { onHome, onTest } = require('.././actions.js');
|
6 |
|
7 | const isWin = process.platform === 'win32';
|
8 |
|
9 | jest.setTimeout(20000);
|
10 | jest.mock('open', () => {
|
11 | return jest.fn(() => {
|
12 | console.log('browser opened');
|
13 | });
|
14 | });
|
15 |
|
16 | jest.mock('node-fetch', () => {
|
17 | return jest.fn(url => {
|
18 | return new Promise(resolve => {
|
19 | setTimeout(
|
20 | () => resolve({ ok: !url.includes('error.com') }),
|
21 | (Math.random() + 1)*1000,
|
22 | );
|
23 | });
|
24 | });
|
25 | });
|
26 |
|
27 | beforeAll(async () => {
|
28 | const { stdout } = await coffee.spawn('nrm', ['-V'], { shell: isWin }).end();
|
29 | __NRM_VERSION__ = stdout ? stdout : null;
|
30 | await coffee.spawn('npm', ['link'], { shell: isWin }).end();
|
31 | });
|
32 |
|
33 | afterAll(async () => {
|
34 | await coffee.spawn('npm', ['unlink', 'nrm', '-g'], { shell: isWin }).end();
|
35 | if(__NRM_VERSION__ !== null) {
|
36 | await coffee.spawn('npm', [`install -g nrm@${__NRM_VERSION__}`], { shell: isWin }).end();
|
37 | }
|
38 | });
|
39 |
|
40 | it('nrm ls', async () => {
|
41 | await coffee.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
|
42 | .expect('stdout', /The registry has been changed to 'cnpm'/g)
|
43 | .expect('code', 0)
|
44 | .end();
|
45 |
|
46 | const { stdout, code } = await coffee.spawn('nrm', ['ls'], { shell: isWin }).end();
|
47 |
|
48 | const match = chalk.green.bold('* ') + 'cnpm';
|
49 | expect(stdout.includes(match)).toBe(true);
|
50 | expect(code).toBe(0);
|
51 | });
|
52 |
|
53 | it('nrm use <registry>', async () => {
|
54 | await coffee.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
|
55 | .expect('stdout', /The registry has been changed to 'cnpm'/g)
|
56 | .expect('code', 0)
|
57 | .end();
|
58 | });
|
59 |
|
60 | it('nrm current', async () => {
|
61 | await coffee.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
|
62 | .expect('stdout', /The registry has been changed to 'cnpm'/g)
|
63 | .expect('code', 0)
|
64 | .end();
|
65 |
|
66 | await coffee.spawn('nrm', ['current'], { shell: isWin })
|
67 | .expect('stdout', /cnpm/g)
|
68 | .expect('code', 0)
|
69 | .end();
|
70 | });
|
71 |
|
72 | describe('nrm command which needs to add a custom registry', () => {
|
73 | const customName = 'customName';
|
74 | const url = 'https://registry.error.com/';
|
75 |
|
76 | beforeEach(async () => {
|
77 |
|
78 | __REGISTRY__ = customName;
|
79 |
|
80 | await coffee.spawn('nrm', ['add', `${__REGISTRY__}`, `${url}`], { shell: isWin })
|
81 | .expect('stdout', /success/g)
|
82 | .expect('code', 0)
|
83 | .end();
|
84 | });
|
85 |
|
86 | afterEach(async () => {
|
87 | await coffee.spawn('nrm', ['del',`${__REGISTRY__}`], { shell: isWin })
|
88 | .expect('stdout', /has been deleted successfully/g)
|
89 | .expect('code', 0)
|
90 | .end();
|
91 | });
|
92 |
|
93 | it('nrm rename', async () => {
|
94 | const newName = 'newName';
|
95 | __REGISTRY__ = newName;
|
96 | const match = new RegExp(`The registry '${customName}' has been renamed to '${newName}'`, 'g');
|
97 |
|
98 | await coffee.spawn('nrm', ['rename',`${customName}`, `${newName}`], { shell: isWin })
|
99 | .expect('stdout', match)
|
100 | .expect('code', 0)
|
101 | .end();
|
102 | });
|
103 |
|
104 | it('nrm set <name>', async () => {
|
105 | const attr = 'attr';
|
106 | const value = 'value';
|
107 |
|
108 | await coffee.spawn('nrm', ['set', `${__REGISTRY__}`, '-a', `${attr}`, '-v', `${value}`], { shell: isWin })
|
109 | .expect('stdout', /successfully/g)
|
110 | .expect('code', 0)
|
111 | .end();
|
112 | });
|
113 |
|
114 | it('nrm test [registry]', async () => {
|
115 | const results = await onTest();
|
116 | expect(results.every(ele => /\d+\sms/.test(ele))).toBe(true);
|
117 | expect(results.some(ele => ele.includes('*'))).toBe(true);
|
118 | expect(results.some(ele => ele.includes('please ignore'))).toBe(true);
|
119 | });
|
120 |
|
121 | it('nrm set-scope <scopeName> <url>, del-scope <scopeName>', async () => {
|
122 | const scopeName = 'nrm';
|
123 | const url = 'https://scope.example.org';
|
124 |
|
125 | await coffee.spawn('nrm', ['set-scope',`${scopeName}`, `${url}`], { shell: isWin })
|
126 | .expect('stdout', /success/g)
|
127 | .expect('code', 0)
|
128 | .end();
|
129 |
|
130 | await coffee.spawn('nrm', ['del-scope',`${scopeName}`], { shell: isWin })
|
131 | .expect('stdout', /success/g)
|
132 | .expect('code', 0)
|
133 | .end();
|
134 | });
|
135 |
|
136 | it('nrm set-hosted-repo <name> <repo>', async () => {
|
137 | const repo = 'repo';
|
138 | const match = new RegExp(`Set the repository of registry '${__REGISTRY__}' successfully`, 'g');
|
139 |
|
140 | await coffee.spawn('nrm', ['set-hosted-repo',`${__REGISTRY__}`, `${repo}`], { shell: isWin })
|
141 | .expect('stdout', match)
|
142 | .expect('code', 0)
|
143 | .end();
|
144 | });
|
145 |
|
146 | it('login <name> [base64]', async () => {
|
147 | const username = 'username';
|
148 | const password = 'password';
|
149 |
|
150 | await coffee.spawn('nrm', ['login',`${__REGISTRY__}`,'-u', `${username}`, '-p', `${password}`], { shell: isWin })
|
151 | .expect('stdout', /success/g)
|
152 | .expect('code', 0)
|
153 | .end();
|
154 |
|
155 | await coffee.spawn('nrm', ['login',`${__REGISTRY__}`], { shell: isWin })
|
156 | .expect('stderr', /Authorization information in base64 format or username & password is required/g)
|
157 | .end();
|
158 | });
|
159 | });
|
160 |
|
161 | it('nrm home <registry> [browser]', async () => {
|
162 | await onHome('cnpm');
|
163 | expect(open).toHaveBeenCalled();
|
164 | });
|