Skip to content

Commit

Permalink
fix: Repair tests and compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Jul 12, 2024
1 parent 4e4e8b7 commit 36eeb35
Show file tree
Hide file tree
Showing 38 changed files with 6,545 additions and 9,576 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ jobs:
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: "8.8.0"
version: "9.5.0"

- name: Use Node.js '18.19.0'
- name: Use Node.js '20.15.0'
uses: actions/setup-node@v2
with:
node-version: "18.19.0"
node-version: "20.15.0"
registry-url: "https://registry.npmjs.org"
scope: "@lukso"
cache: "pnpm"
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 18.19.0
nodejs 20
66 changes: 33 additions & 33 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "pnpm test",
"request": "launch",
"runtimeArgs": ["test"],
"runtimeExecutable": "pnpm",
"skipFiles": ["<node_internals>/**"],
"console": "integratedTerminal",
"type": "node"
},
{
"name": "Pinata upload",
"program": "${workspaceFolder}/examples/upload-pinata.mjs",
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "node"
},
{
"name": "LocalNode upload",
"program": "${workspaceFolder}/examples/upload-local-ipfs.mjs",
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "node"
},
{
"command": "./node_modules/.bin/astro dev --force",
"name": "Astro example server",
"cwd": "${workspaceFolder}/examples/astro-example",
"request": "launch",
"type": "node-terminal"
}
]
"version": "0.2.0",
"configurations": [
{
"name": "pnpm test",
"request": "launch",
"runtimeArgs": ["test"],
"runtimeExecutable": "/opt/homebrew/bin/pnpm",
"skipFiles": ["<node_internals>/**"],
"console": "integratedTerminal",
"type": "node"
},
{
"name": "Pinata upload",
"program": "${workspaceFolder}/examples/upload-pinata.mjs",
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "node"
},
{
"name": "LocalNode upload",
"program": "${workspaceFolder}/examples/upload-local-ipfs.mjs",
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "node"
},
{
"command": "./node_modules/.bin/astro dev --force",
"name": "Astro example server",
"cwd": "${workspaceFolder}/examples/astro-example",
"request": "launch",
"type": "node-terminal"
}
]
}
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ const provider = new PinataUploader({
//
const provider = new IPFSHttpClientUploader(import.meta.env.INFURA_GATEWAY, {
headers: {
authorization: `Basic ${Buffer.from(
authorization: `Basic ${compatibility.encodeBase64(
`${import.meta.env.INFURA_API_KEY_NAME}:${import.meta.env.INFURA_API_KEY}`
).toString("base64")}`,
)}`,
},
});
```
Expand Down Expand Up @@ -185,11 +185,11 @@ This is how you would use this component within a page to talk to infura. (the c
gateway={import.meta.env.TEST_INFURA_GATEWAY}
options={{
headers: {
authorization: `Basic ${Buffer.from(
authorization: `Basic ${compatibility.encodeBase64(
`${import.meta.env.TEST_INFURA_API_KEY_NAME}:${
import.meta.env.TEST_INFURA_API_KEY
}`
).toString("base64")}`,
)}`,
},
}}
/>
Expand All @@ -206,6 +206,8 @@ This would connect to this kind of endpoint
```ts
import type { APIContext } from "astro";
import { IPFSHttpClientUploader } from "@lukso/data-provider-ipfs-http-client";
import { compatibility } from "@lukso/data-provider-base";
// import "@lukso/data-provider-base/compatibility-node"; when on backend.
export async function POST({ request }: APIContext) {
const formData = await request.formData();
Expand All @@ -215,11 +217,11 @@ export async function POST({ request }: APIContext) {
import.meta.env.TEST_INFURA_GATEWAY,
{
headers: {
authorization: `Basic ${Buffer.from(
authorization: `Basic ${compatibility.encodeBase64(
`${import.meta.env.TEST_INFURA_API_KEY_NAME}:${
import.meta.env.TEST_INFURA_API_KEY
}`
).toString("base64")}`,
)}`,
},
}
);
Expand Down
84 changes: 42 additions & 42 deletions configs/jest-presets/jest/browser/jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
const esModules = [
"is-ip",
"jest-tests-browser",
"@lukso/data-provider-base",
"@lukso/data-provider-ipfs-http-client",
"@lukso/data-provider-urlresolver",
"data-provider-ipfs-http-client",
"jest-tests-browser",
"is-ip",
"jest-tests-browser",
"@lukso/data-provider-base",
"@lukso/data-provider-ipfs-http-client",
"@lukso/data-provider-urlresolver",
"data-provider-ipfs-http-client",
"jest-tests-browser",
].join("|");

module.exports = {
roots: ["<rootDir>"],
testMatch: ["**/*.spec.mts", "**/*.spec.ts"],
testEnvironment: "jsdom", // jest-presets/jest/browser/jest-environment-jsdom.ts",
transform: {
"^.+\\.(ts|tsx|js|mjs)?$": [
"ts-jest",
{
useESM: true,
},
],
},
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"json",
"node",
"mjs",
"cjs",
],
extensionsToTreatAsEsm: [".ts"],
modulePathIgnorePatterns: [
"<rootDir>/test/__fixtures__",
"<rootDir>/node_modules",
"<rootDir>/dist",
],
setupFilesAfterEnv: ["jest-presets/jest/browser/jest-setup.js"],
preset: "ts-jest",
transformIgnorePatterns: [
`/node_modules/(?!${esModules})`,
"lukso-data-providers",
],
silent: false,
verbose: true,
roots: ["<rootDir>"],
testMatch: ["**/*.spec.mts", "**/*.spec.ts"],
testEnvironment: "@happy-dom/jest-environment", // jest-presets/jest/browser/jest-environment-jsdom.ts",
transform: {
"^.+\\.(ts|tsx|js|mjs)?$": [
"ts-jest",
{
useESM: true,
},
],
},
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"json",
"node",
"mjs",
"cjs",
],
extensionsToTreatAsEsm: [".ts"],
modulePathIgnorePatterns: [
"<rootDir>/test/__fixtures__",
"<rootDir>/node_modules",
"<rootDir>/dist",
],
setupFilesAfterEnv: ["jest-presets/jest/browser/jest-setup.js"],
preset: "ts-jest",
transformIgnorePatterns: [
`/node_modules/(?!${esModules})`,
"lukso-data-providers",
],
silent: false,
verbose: true,
};
55 changes: 28 additions & 27 deletions configs/jest-presets/jest/node/jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
const crypto = require("node:crypto");
const { TextEncoder, TextDecoder } = require("node:util");
const ResizeObserver = require("resize-observer-polyfill");
// const crypto = require("node:crypto");
// const { TextEncoder, TextDecoder } = require("node:util");
// const ResizeObserver = require("resize-observer-polyfill");
const { config } = require("dotenv");

config({ path: "../../.env.test" });

Object.defineProperty(globalThis, "crypto", {
value: crypto,
});
Object.defineProperty(globalThis, "Uint32Array", {
value: Uint32Array,
});
Object.defineProperty(globalThis, "Uint8Array", {
value: Uint8Array,
});
Object.defineProperty(globalThis, "ArrayBuffer", {
value: ArrayBuffer,
});
// Object.defineProperty(globalThis, "crypto", {
// value: crypto,
// });
// Object.defineProperty(globalThis, "Uint32Array", {
// value: Uint32Array,
// });
// Object.defineProperty(globalThis, "Uint8Array", {
// value: Uint8Array,
// });
// Object.defineProperty(globalThis, "ArrayBuffer", {
// value: ArrayBuffer,
// });

Object.defineProperty(globalThis, "ArrayBuffer", {
value: ArrayBuffer,
});
Object.defineProperty(globalThis, "TextDecoder", {
value: TextDecoder,
});
Object.defineProperty(globalThis, "TextEncoder", {
value: TextEncoder,
});
Object.defineProperty(globalThis, "ResizeObserver", {
value: ResizeObserver,
});
// Object.defineProperty(globalThis, "ArrayBuffer", {
// value: ArrayBuffer,
// });
// Object.defineProperty(globalThis, "TextDecoder", {
// value: TextDecoder,
// });
// Object.defineProperty(globalThis, "TextEncoder", {
// value: TextEncoder,
// });
// Object.defineProperty(globalThis, "ResizeObserver", {
// value: ResizeObserver,
// });
1 change: 1 addition & 0 deletions configs/jest-presets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"license": "MIT",
"dependencies": {
"@happy-dom/jest-environment": "^14.12.3",
"dotenv": "^16.4.5",
"ts-jest": "^29.1.2"
},
Expand Down
12 changes: 7 additions & 5 deletions configs/jest-tests-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"type": "module",
"sideEffects": false,
"license": "MIT",
"files": ["dist/**"],
"files": [
"dist/**"
],
"devDependencies": {
"@jest/globals": "^29.7.0",
"@lukso/tsconfig": "workspace:*",
"cross-blob": "^3.0.2",
"jest-presets": "workspace:*",
"typescript": "^4.9.5"
"typescript": "^5.5.3"
},
"scripts": {
"test": "cross-env NODE_OPTIONS='${NODE_OPTIONS} --experimental-vm-modules' jest --verbose --detectOpenHandles --runInBand"
Expand All @@ -26,9 +28,9 @@
"@lukso/data-provider-ipfs-http-client": "workspace:*",
"@lukso/data-provider-pinata": "workspace:*",
"@lukso/data-provider-urlresolver": "workspace:*",
"@types/chrome": "^0.0.248",
"@types/chrome": "^0.0.268",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.2",
"web-streams-polyfill": "^3.3.3"
"@types/node": "^20.14.10",
"web-streams-polyfill": "^4.0.0"
}
}
13 changes: 5 additions & 8 deletions configs/jest-tests-browser/test/infura-real.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { resolve } from "node:path";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { jest } from "@jest/globals";
import Blob from "cross-blob";
import "@lukso/data-provider-base/compatibility-node"; // jest is running node and jsdom doesn't support fetch.

const __dirname = dirname(fileURLToPath(import.meta.url));

import { compatibility } from "@lukso/data-provider-base";
import IPFSHttpClientUploader from "@lukso/data-provider-ipfs-http-client";

beforeEach(() => {
Expand All @@ -25,24 +26,20 @@ it("should pin images (web, infura)", async () => {
}, 20000);

async function mockDependencies() {
const file = new Blob(
const file = new compatibility.Blob(
// This is only for jest so it's no big deal.
// eslint-disable-next-line unicorn/prefer-module
[readFileSync(resolve(__dirname, "./test-image.png"))],
{
type: "image/png",
},
);
// TODO: fix "is not assignable to type IDE error"
// file.arrayBuffer = async () => {
// return Buffer.from('123123');
// };

const config = {
headers: {
authorization: `Basic ${Buffer.from(
authorization: `Basic ${compatibility.encodeBase64(
`${process.env.TEST_INFURA_API_KEY_NAME}:${process.env.TEST_INFURA_API_KEY}`,
).toString("base64")}`,
)}`,
},
};
const uploader = new IPFSHttpClientUploader(
Expand Down
7 changes: 3 additions & 4 deletions configs/jest-tests-browser/test/ipfs-http-client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jest } from "@jest/globals";
import { getFile } from "@lukso/data-provider-base";
import { compatibility } from "@lukso/data-provider-base";
import { IPFSHttpClientUploader } from "@lukso/data-provider-ipfs-http-client";

it("should pin images (mocked)", async () => {
Expand All @@ -14,12 +14,11 @@ it("should pin images (mocked)", async () => {
});

async function mockDependencies(gateway = "https://api.2eff.lukso.dev") {
const File = await getFile();
const file = new File(["123123"], "test-image.jpg", {
const file = new compatibility.File(["123123"], "test-image.jpg", {
type: "image/jpg",
});
// TODO: fix "is not assignable to type IDE error"
file.arrayBuffer = async () => Buffer.from("");
file.arrayBuffer = async () => new ArrayBuffer(0);

const uploader = new IPFSHttpClientUploader(gateway);

Expand Down
1 change: 1 addition & 0 deletions configs/jest-tests-browser/test/pinata-proxy-real.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { jest } from "@jest/globals";
import Blob from "cross-blob";
import "@lukso/data-provider-base/compatibility-node"; // jest is running node and jsdom doesn't support fetch.

const __dirname = dirname(fileURLToPath(import.meta.url));

Expand Down
Loading

0 comments on commit 36eeb35

Please sign in to comment.