Skip to content

Commit 82ec347

Browse files
committed
chore: more esm tweaks
1 parent 60172ce commit 82ec347

6 files changed

Lines changed: 111 additions & 70 deletions

File tree

jest.config.ts renamed to jest.config.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
* For a detailed explanation regarding each configuration property and type check, visit:
33
* https://jestjs.io/docs/en/configuration.html
44
*/
5-
import type { Config } from '@jest/types'
65

7-
export default async (): Promise<Config.InitialOptions> => {
6+
export default async () => {
87
return {
98
preset: 'ts-jest',
109
testEnvironment: 'node',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
"scripts": {
2525
"prepare": "npm run build",
26-
"build": "rimraf ./build && tsc && chmod +x dist/index.js",
26+
"build": "rimraf ./dist && tsc && chmod +x dist/index.js && babel --no-babelrc --plugins \"babel-plugin-add-import-extension\" --out-dir dist/ dist/",
2727
"start": "nodemon --config nodemon.json src/index.ts",
2828
"start:debug": "nodemon --config nodemon.json --inspect-brk src/index.ts",
2929
"lint": "eslint --fix \"src/**/*.ts\" \"test/**/*.ts\" && prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",

test/jest/postage-setup.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* eslint-disable no-console */
2+
import fetch from 'node-fetch'
3+
4+
const STAMPS_ENDPOINT = 'stamps'
5+
6+
async function http(url, config) {
7+
const response = await fetch(url, config)
8+
9+
return await response.json()
10+
}
11+
12+
async function getPostageBatch(url, postageBatchId) {
13+
return await http(`${url}/${STAMPS_ENDPOINT}/${postageBatchId}`, {
14+
method: 'get',
15+
})
16+
}
17+
18+
async function createPostageBatch(url, amount, depth) {
19+
const response = await http(`${url}/${STAMPS_ENDPOINT}/${amount}/${depth}`, {
20+
method: 'post',
21+
})
22+
23+
return response.batchID
24+
}
25+
26+
export default async function postageSetup() {
27+
const beeDebugUrl = process.env.BEE_DEBUG_API_URL || 'http://127.0.0.1:1635'
28+
const beeDebugPeerUrl = process.env.BEE_PEER_DEBUG_API_URL || 'http://127.0.0.1:11635'
29+
30+
try {
31+
if (process.env.BEE_POSTAGE) {
32+
try {
33+
if (!(await getPostageBatch(beeDebugUrl, process.env.BEE_POSTAGE)).usable) {
34+
delete process.env.BEE_POSTAGE
35+
console.log('BEE_POSTAGE stamp was found but is not usable')
36+
} else {
37+
console.log('Using configured BEE_POSTAGE stamp.')
38+
}
39+
} catch (e) {
40+
delete process.env.BEE_POSTAGE
41+
console.log('BEE_POSTAGE stamp was not found')
42+
}
43+
}
44+
45+
if (process.env.BEE_PEER_POSTAGE) {
46+
try {
47+
if (!(await getPostageBatch(beeDebugPeerUrl, process.env.BEE_PEER_POSTAGE)).usable) {
48+
delete process.env.BEE_PEER_POSTAGE
49+
console.log('BEE_PEER_POSTAGE stamp was found but is not usable')
50+
} else {
51+
console.log('Using configured BEE_PEER_POSTAGE stamp.')
52+
}
53+
} catch (e) {
54+
delete process.env.BEE_PEER_POSTAGE
55+
console.log('BEE_PEER_POSTAGE stamp was not found')
56+
}
57+
}
58+
59+
if (!process.env.BEE_POSTAGE || !process.env.BEE_PEER_POSTAGE) {
60+
console.log('Creating postage stamps...')
61+
62+
const stampsOrder = []
63+
64+
if (!process.env.BEE_POSTAGE) {
65+
stampsOrder.push({ url: beeDebugUrl, env: 'BEE_POSTAGE' })
66+
}
67+
68+
if (!process.env.BEE_PEER_POSTAGE) {
69+
stampsOrder.push({ url: beeDebugPeerUrl, env: 'BEE_PEER_POSTAGE' })
70+
}
71+
72+
const stamps = await Promise.all(stampsOrder.map(async order => createPostageBatch(order.url, '1', 20)))
73+
74+
for (let i = 0; i < stamps.length; i++) {
75+
process.env[stampsOrder[i].env] = stamps[i]
76+
console.log(`${stampsOrder[i].env}: ${stamps[i]}`)
77+
}
78+
79+
console.log('Waiting for the stamps to be usable')
80+
let allUsable = true
81+
do {
82+
for (let i = 0; i < stamps.length; i++) {
83+
// eslint-disable-next-line max-depth
84+
try {
85+
// eslint-disable-next-line max-depth
86+
if (!(await getPostageBatch(stampsOrder[i].url, stamps[i])).usable) {
87+
allUsable = false
88+
break
89+
} else {
90+
allUsable = true
91+
}
92+
} catch (e) {
93+
allUsable = false
94+
break
95+
}
96+
}
97+
98+
// eslint-disable-next-line no-loop-func
99+
await new Promise(resolve => setTimeout(() => resolve(), 1_000))
100+
} while (!allUsable)
101+
console.log('Usable, yey!')
102+
}
103+
} catch (e) {
104+
// It is possible that for unit tests the Bee nodes does not run
105+
// so we are only logging errors and not leaving them to propagate
106+
console.error(e)
107+
}
108+
}

test/tests-setup.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

tsconfig.test.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"extends": "./tsconfig.json",
33
"include": [
44
"src",
5-
"test",
6-
"jest.config.ts"
5+
"test"
76
],
87
"compilerOptions": {
98
"noEmit": true

0 commit comments

Comments
 (0)