Skip to content

Commit 666b4a7

Browse files
move tests to typescript
1 parent 7db31ff commit 666b4a7

File tree

21 files changed

+2122
-3347
lines changed

21 files changed

+2122
-3347
lines changed

.husky/pre-commit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npx --no-install lint-staged
4+
npx pretty-quick --staged
5+
npm test

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
rootDir: 'packages',
6+
coverageDirectory: './coverage/',
7+
collectCoverage: true
8+
};

package.json

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
{
22
"license": "MIT",
33
"scripts": {
4+
"prepare": "husky install",
45
"postinstall": "lerna bootstrap",
56
"coverage": "codecov",
67
"clean": "lerna clean",
78
"dev": "jest --watch",
89
"updated": "lerna updated",
910
"test": "lerna run tsc && jest",
10-
"publish": "lerna run tsc && lerna publish --exact",
11-
"precommit": "lint-staged",
12-
"prepare": "husky install"
13-
},
14-
"lint-staged": {
15-
"*.js": [
16-
"prettier --single-quote --write",
17-
"jest --findRelatedTests"
18-
]
19-
},
20-
"jest": {
21-
"rootDir": "packages",
22-
"coverageDirectory": "./coverage/",
23-
"collectCoverage": true
11+
"publish": "lerna run tsc && lerna publish --exact"
2412
},
2513
"devDependencies": {
14+
"@types/jest": "29.1.2",
2615
"@types/node": "18.8.3",
2716
"@typescript-eslint/eslint-plugin": "5.0.0",
2817
"@typescript-eslint/parser": "5.0.0",
2918
"eslint": "8.0.1",
3019
"eslint-config-prettier": "8.3.0",
3120
"eslint-plugin-prettier": "4.0.0",
32-
"husky": "7.0.2",
33-
"jest": "25.1.0",
21+
"husky": "8.0.1",
22+
"jest": "29.1.2",
3423
"lerna": "4.0.0",
35-
"lint-staged": "10.0.7",
3624
"prettier": "2.4.1",
25+
"ts-jest": "29.0.3",
3726
"typescript": "4.8.4"
3827
}
39-
}
28+
}

packages/oc-azure-storage-adapter/__test__/azure.test.js renamed to packages/oc-azure-storage-adapter/__test__/azure.test.ts

Lines changed: 73 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
const Readable = require('stream').Readable;
2-
const azure = require('../lib');
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import stream from 'stream';
3+
import azure from '../src';
34

45
//Mock Date functions
56
const DATE_TO_USE = new Date('2017');
67
const _Date = Date;
7-
global.Date = jest.fn(() => DATE_TO_USE);
8+
global.Date = jest.fn(() => DATE_TO_USE) as any;
89
global.Date.UTC = _Date.UTC;
910
global.Date.parse = _Date.parse;
1011
global.Date.now = _Date.now;
1112

13+
const validOptions = {
14+
publicContainerName: 'pubcon',
15+
privateContainerName: 'privcon',
16+
accountName: 'name',
17+
accountKey: 'key',
18+
path: '/'
19+
};
20+
1221
test('should expose the correct methods', () => {
13-
const options = {
14-
publicContainerName: 'pubcon',
15-
privateContainerName: 'privcon'
16-
};
17-
const client = new azure(options);
22+
const client = azure(validOptions);
1823

1924
[
2025
{ method: 'adapterType', value: 'azure-blob-storage' },
@@ -28,46 +33,44 @@ test('should expose the correct methods', () => {
2833
{ method: 'putFileContent', type: Function }
2934
].forEach(api => {
3035
if (api.type === Function) {
31-
expect(client[api.method]).toBeInstanceOf(api.type);
36+
expect((client as any)[api.method]).toBeInstanceOf(api.type);
3237
} else {
33-
expect(client[api.method]).toBe(api.value);
38+
expect((client as any)[api.method]).toBe(api.value);
3439
}
3540
});
3641
});
3742

3843
test('validate valid conf without credentials', () => {
3944
const options = {
4045
publicContainerName: 'pubcon',
41-
privateContainerName: 'privcon'
46+
privateContainerName: 'privcon',
47+
path: 'path'
4248
};
43-
const client = new azure(options);
44-
expect(client.isValid()).toBe(true);
49+
// @ts-expect-error Bad config
50+
const client = azure(options);
51+
expect(client.isValid()).toBe(false);
4552
});
4653

4754
test('validate valid conf with credentials', () => {
48-
const options = {
49-
publicContainerName: 'pubcon',
50-
privateContainerName: 'privcon',
51-
accountName: 'acc',
52-
accountKey: 'accKey'
53-
};
54-
const client = new azure(options);
55+
const client = azure(validOptions);
5556
expect(client.isValid()).toBe(true);
5657
});
5758

5859
test('validate missing public container', () => {
5960
const options = {
6061
privateContainerName: 'privcon'
6162
};
62-
const client = new azure(options);
63+
// @ts-expect-error Bad config
64+
const client = azure(options);
6365
expect(client.isValid()).toBe(false);
6466
});
6567

6668
test('validate missing private container', () => {
6769
const options = {
6870
publicContainerName: 'pubcon'
6971
};
70-
const client = new azure(options);
72+
// @ts-expect-error Bad config
73+
const client = azure(options);
7174
expect(client.isValid()).toBe(false);
7275
});
7376

@@ -77,7 +80,8 @@ test('validate partial credentials, no key', () => {
7780
privateContainerName: 'privcon',
7881
accountName: 'acc'
7982
};
80-
const client = new azure(options);
83+
// @ts-expect-error Bad config
84+
const client = azure(options);
8185
expect(client.isValid()).toBe(false);
8286
});
8387

@@ -87,7 +91,8 @@ test('validate partial credentials, no name', () => {
8791
privateContainerName: 'privcon',
8892
accountKey: 'accKey'
8993
};
90-
const client = new azure(options);
94+
// @ts-expect-error Bad config
95+
const client = azure(options);
9196
expect(client.isValid()).toBe(false);
9297
});
9398

@@ -123,11 +128,7 @@ test('validate partial credentials, no name', () => {
123128
}
124129
].forEach(scenario => {
125130
test(`test getFile ${scenario.src}`, async () => {
126-
const options = {
127-
publicContainerName: 'pubcon',
128-
privateContainerName: 'privcon'
129-
};
130-
const client = new azure(options);
131+
const client = azure(validOptions);
131132
const operation = () =>
132133
client[scenario.src.match(/\.json$/) ? 'getJson' : 'getFile'](
133134
scenario.src,
@@ -143,11 +144,7 @@ test('validate partial credentials, no name', () => {
143144
});
144145

145146
test('test getFile force mode', async () => {
146-
const options = {
147-
publicContainerName: 'pubcon',
148-
privateContainerName: 'privcon'
149-
};
150-
const client = new azure(options);
147+
const client = azure(validOptions);
151148

152149
const data1 = await client.getFile('path/to-mutable.txt', false);
153150
const data2 = await client.getFile('path/to-mutable.txt');
@@ -158,15 +155,17 @@ test('test getFile force mode', async () => {
158155
});
159156

160157
test('test getJson force mode', async () => {
161-
const options = {
162-
publicContainerName: 'pubcon',
163-
privateContainerName: 'privcon'
164-
};
165-
const client = new azure(options);
158+
const client = azure(validOptions);
166159

167-
const data1 = await client.getJson('path/to-mutable.json', false);
168-
const data2 = await client.getJson('path/to-mutable.json');
169-
const data3 = await client.getJson('path/to-mutable.json', true);
160+
const data1: { value: string } = await client.getJson(
161+
'path/to-mutable.json',
162+
false
163+
);
164+
const data2: { value: string } = await client.getJson('path/to-mutable.json');
165+
const data3: { value: string } = await client.getJson(
166+
'path/to-mutable.json',
167+
true
168+
);
170169

171170
expect(data1.value).toBe(data2.value);
172171
expect(data3.value).not.toBe(data1.value);
@@ -179,10 +178,7 @@ test('test getJson force mode', async () => {
179178
{ path: 'components/image/1.0.0/', expected: [] }
180179
].forEach(scenario => {
181180
test(`test listObjects when bucket is not empty for folder ${scenario.path}`, async () => {
182-
const client = new azure({
183-
publicContainerName: 'pubcon',
184-
privateContainerName: 'privcon'
185-
});
181+
const client = azure(validOptions);
186182

187183
const data = await client.listSubDirectories(scenario.path);
188184

@@ -192,10 +188,7 @@ test('test getJson force mode', async () => {
192188

193189
['hello', 'path/'].forEach(scenario => {
194190
test(`test listObjects when bucket is empty for folder ${scenario}`, async () => {
195-
const client = new azure({
196-
publicContainerName: 'my-empty-container',
197-
privateContainerName: 'my-empty-container'
198-
});
191+
const client = azure(validOptions);
199192

200193
const data = await client.listSubDirectories(scenario);
201194

@@ -204,15 +197,12 @@ test('test getJson force mode', async () => {
204197
});
205198

206199
test('test getUrl ', () => {
207-
const client = new azure({ path: '/' });
200+
const client = azure(validOptions);
208201
expect(client.getUrl('test', '1.0.0', 'test.js')).toBe('/test/1.0.0/test.js');
209202
});
210203

211204
test('test put dir (failure)', () => {
212-
const client = new azure({
213-
publicContainerName: 'pubcon',
214-
privateContainerName: 'privcon'
215-
});
205+
const client = azure(validOptions);
216206

217207
return expect(
218208
client.putDir(
@@ -223,10 +213,7 @@ test('test put dir (failure)', () => {
223213
});
224214

225215
test('test put dir (stream failure throwing)', () => {
226-
const client = new azure({
227-
publicContainerName: 'pubcon',
228-
privateContainerName: 'privcon'
229-
});
216+
const client = azure(validOptions);
230217

231218
return expect(
232219
client.putDir(
@@ -237,28 +224,30 @@ test('test put dir (stream failure throwing)', () => {
237224
});
238225

239226
test('test private putFileContent', async () => {
240-
const client = new azure({
241-
publicContainerName: 'pubcon',
242-
privateContainerName: 'privcon'
243-
});
227+
const client = azure(validOptions);
244228

245-
const result = await client.putFileContent('words', 'filename.js', true);
229+
const result = (await client.putFileContent(
230+
'words',
231+
'filename.js',
232+
true
233+
)) as any;
246234

247235
expect(result.container).toBe('privcon');
248236
});
249237

250238
test('test private putFileContent stream', async () => {
251-
const client = new azure({
252-
publicContainerName: 'pubcon',
253-
privateContainerName: 'privcon'
254-
});
239+
const client = azure(validOptions);
255240

256241
const fileContent = 'words';
257-
const fileStream = new Readable();
242+
const fileStream = new stream.Readable();
258243
fileStream.push(fileContent);
259244
fileStream.push(null);
260245

261-
const result = await client.putFileContent(fileStream, 'filename.js', true);
246+
const result = (await client.putFileContent(
247+
fileStream,
248+
'filename.js',
249+
true
250+
)) as any;
262251

263252
expect(result.container).toBe('privcon');
264253
expect(result.lengthWritten).toBe(fileContent.length);
@@ -268,38 +257,37 @@ test('test private putFileContent stream', async () => {
268257
});
269258

270259
test('test public putFileContent', async () => {
271-
const client = new azure({
272-
publicContainerName: 'pubcon',
273-
privateContainerName: 'privcon'
274-
});
260+
const client = azure(validOptions);
275261

276-
const result = await client.putFileContent('words', 'filename.gz', false);
262+
const result = (await client.putFileContent(
263+
'words',
264+
'filename.gz',
265+
false
266+
)) as any;
277267

278268
expect(result.container).toBe('pubcon');
279269
});
280270

281271
test('test public putFileContent stream', async () => {
282-
const client = new azure({
283-
publicContainerName: 'pubcon',
284-
privateContainerName: 'privcon'
285-
});
272+
const client = azure(validOptions);
286273

287274
const fileContent = 'words';
288-
const fileStream = new Readable();
275+
const fileStream = new stream.Readable();
289276
fileStream.push(fileContent);
290277
fileStream.push(null);
291278

292-
const result = await client.putFileContent(fileStream, 'filename.js', false);
279+
const result = (await client.putFileContent(
280+
fileStream,
281+
'filename.js',
282+
false
283+
)) as any;
293284

294285
expect(result.container).toBe('pubcon');
295286
expect(result.lengthWritten).toBe(fileContent.length);
296287
});
297288

298289
test('put a js file ', async () => {
299-
const client = new azure({
300-
publicContainerName: 'pubcon',
301-
privateContainerName: 'privcon'
302-
});
290+
const client = azure(validOptions);
303291

304292
await expect(client.putFile('../path', 'hello.js', false)).resolves.toEqual({
305293
container: 'pubcon',

0 commit comments

Comments
 (0)