From f4ccc09d69b7b9c76483c74e9ee79508f72366aa Mon Sep 17 00:00:00 2001 From: Miguel Gomes Date: Thu, 21 Nov 2024 11:50:51 -0300 Subject: [PATCH] move stream-metadata to vitest (#1581) stacked on top of #1576 I'm breaking #1462 into small PRs Moving the stream-metadata package from jest to vitest. Some tests wasn't enabled: the test suite name wasn't being matched with the name pattern of test:integration script Those tests were outdated, did a few changes to match the expected current behavior. --- .vscode/launch.json | 29 +++++--------- packages/stream-metadata/.eslintignore | 6 +-- packages/stream-metadata/.eslintrc.json | 7 +--- packages/stream-metadata/jest.config.ts | 32 --------------- packages/stream-metadata/jest.setup.ts | 11 ------ packages/stream-metadata/package.json | 13 +++---- .../integration/spaceMemberMetadata.test.ts | 8 ++-- .../tests/integration/spaceMetadata.test.ts | 39 ++++++------------- packages/stream-metadata/tests/testUtils.ts | 6 +-- packages/stream-metadata/tsconfig.json | 3 +- packages/stream-metadata/vitest.config.ts | 12 ++++++ packages/stream-metadata/vitest.setup.ts | 5 +++ yarn.lock | 13 +------ 13 files changed, 57 insertions(+), 127 deletions(-) delete mode 100644 packages/stream-metadata/jest.config.ts delete mode 100644 packages/stream-metadata/jest.setup.ts create mode 100644 packages/stream-metadata/vitest.config.ts create mode 100644 packages/stream-metadata/vitest.setup.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 265529b17..2630e36b8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -90,29 +90,20 @@ "console": "integratedTerminal", }, { - "name": "Jest: current file in 'packages/stream-metadata (local_multi_ne)'", + "name": "Vitest: current file in 'packages/stream-metadata/' (local_multi_ne)", "type": "node", "request": "launch", - "cwd": "${workspaceFolder}/packages/stream-metadata", - "program": "${workspaceFolder}/node_modules/.bin/jest", - "runtimeArgs": [ - "--experimental-vm-modules" - ], - "args": [ - "${relativeFile}", - "--config", - "${workspaceFolder}/packages/stream-metadata/jest.config.ts", - "--runInBand", - "--no-cache", - "--forceExit" - ], - "console": "integratedTerminal", - "internalConsoleOptions": "neverOpen", "env": { - "NODE_TLS_REJECT_UNAUTHORIZED": "0", "NODE_EXTRA_CA_CERTS":"~/river-ca-cert.pem", "RIVER_ENV": "local_multi_ne", "PORT": "3003", - } - } + }, + "program": "${workspaceFolder}/node_modules/vitest/vitest.mjs", + "cwd": "${workspaceFolder}/packages/stream-metadata", + "autoAttachChildProcesses": true, + "skipFiles": ["/**", "**/node_modules/**"], + "smartStep": true, + "args": ["run", "${fileBasename}", "--config", "vitest.config.ts"], + "console": "integratedTerminal", + }, ] } diff --git a/packages/stream-metadata/.eslintignore b/packages/stream-metadata/.eslintignore index 5e0646af0..16b202e06 100644 --- a/packages/stream-metadata/.eslintignore +++ b/packages/stream-metadata/.eslintignore @@ -1,4 +1,4 @@ -jest.config.ts -jest.setup.ts +vitest.config.ts +vitest.setup.ts dist/ -node_modules/ +node_modules/ \ No newline at end of file diff --git a/packages/stream-metadata/.eslintrc.json b/packages/stream-metadata/.eslintrc.json index 5f814d805..9a8e21099 100644 --- a/packages/stream-metadata/.eslintrc.json +++ b/packages/stream-metadata/.eslintrc.json @@ -7,8 +7,7 @@ "plugin:import/typescript", "plugin:@typescript-eslint/recommended", "plugin:@typescript-eslint/recommended-requiring-type-checking", - "plugin:prettier/recommended", - "plugin:jest/recommended" + "plugin:prettier/recommended" ], "parser": "@typescript-eslint/parser", "parserOptions": { @@ -30,10 +29,6 @@ "project": "./tsconfig.test.json" }, "rules": { - "jest/no-standalone-expect": "off", - "jest/expect-expect": "off", - "jest/no-conditional-expect": "off", - "jest/no-disabled-tests": "off", "@typescript-eslint/no-unsafe-call": "off", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-unsafe-argument": "off", diff --git a/packages/stream-metadata/jest.config.ts b/packages/stream-metadata/jest.config.ts deleted file mode 100644 index 0958e51ce..000000000 --- a/packages/stream-metadata/jest.config.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { resolve } from 'path' -import type { Config } from '@jest/types' - -const config: Config.InitialOptions = { - preset: 'ts-jest/presets/default-esm', - extensionsToTreatAsEsm: ['.ts'], - modulePathIgnorePatterns: ['/dist/'], - moduleNameMapper: { - '^(\\.{1,2}/.*)\\.js$': '$1', - '@matrix-org/olm/olm.wasm': resolve(__dirname, './tests/__mocks__/mock-wasm-file.js'), - }, - setupFilesAfterEnv: [resolve(__dirname, './jest.setup.ts')], - setupFiles: ['fake-indexeddb/auto'], - testEnvironment: 'node', - testPathIgnorePatterns: ['/dist/', '/node_modules/'], - testTimeout: 30_000, - transform: { - '^.+\\.ts$': [ - 'ts-jest', - { - useESM: true, - tsconfig: './tsconfig.test.json', - }, - ], - '^.+\\.wasm$': 'jest-transform-stub', - }, - transformIgnorePatterns: ['/node_modules/(?!@river-build)'], - verbose: true, - testMatch: ['**/?(*.)+(test).ts'], -} - -export default config diff --git a/packages/stream-metadata/jest.setup.ts b/packages/stream-metadata/jest.setup.ts deleted file mode 100644 index bd2e1852b..000000000 --- a/packages/stream-metadata/jest.setup.ts +++ /dev/null @@ -1,11 +0,0 @@ -import dotenv from 'dotenv' -import { resolve } from 'path' -import { fileURLToPath } from 'url' -import { dirname } from 'path' - -// ES module equivalent of __dirname -const __filename = fileURLToPath(import.meta.url) -const __dirname = dirname(__filename) - -// Load .env.test environment variables -dotenv.config({ path: resolve(__dirname, '.env.test') }) diff --git a/packages/stream-metadata/package.json b/packages/stream-metadata/package.json index 1472d7560..306a62f31 100644 --- a/packages/stream-metadata/package.json +++ b/packages/stream-metadata/package.json @@ -19,8 +19,9 @@ "prettier": "prettier --config ../prettier-config/config.js --ignore-path .prettierignore .", "run-build": "yarn build && node ./dist/node_esbuild.cjs", "start": "node ./dist/node_esbuild.cjs", - "test": "NODE_TLS_REJECT_UNAUTHORIZED=0 node --experimental-vm-modules $(yarn bin jest) --forceExit", - "test:integration": "yarn test --testNamePattern='integration/stream-metadata'", + "test": "vitest run", + "test:integration": "vitest run --testNamePattern integration/stream-metadata", + "test:watch": "vitest --watch", "typecheck": "tsc --noEmit" }, "dependencies": { @@ -51,7 +52,6 @@ "@river-build/encryption": "workspace:^", "@river-build/eslint-config": "workspace:^", "@river-build/prettier-config": "workspace:^", - "@types/jest": "^29.5.3", "@types/node": "^20.5.0", "@types/uuid": "^10.0.0", "@typescript-eslint/parser": "^7.14.1", @@ -62,11 +62,8 @@ "eslint-import-resolver-typescript": "^3.5.5", "eslint-plugin-prettier": "^4.2.1", "fake-indexeddb": "^4.0.1", - "jest": "^29.6.2", - "jest-transform-stub": "^2.0.0", "prettier": "^2.8.8", - "ts-jest": "^29.1.1", - "ts-node": "^10.9.1", - "typescript": "^5.1.6" + "typescript": "^5.1.6", + "vitest": "^2.1.5" } } diff --git a/packages/stream-metadata/tests/integration/spaceMemberMetadata.test.ts b/packages/stream-metadata/tests/integration/spaceMemberMetadata.test.ts index 58d015b24..8ad191b49 100644 --- a/packages/stream-metadata/tests/integration/spaceMemberMetadata.test.ts +++ b/packages/stream-metadata/tests/integration/spaceMemberMetadata.test.ts @@ -18,7 +18,7 @@ const log = dlog('stream-metadata:test:spaceMemberMetadata', { defaultEnabled: true, }) -describe('integration/space/:spaceAddress/token/:tokenId', () => { +describe('integration/stream-metadata/:spaceAddress/token/:tokenId', () => { const baseURL = getTestServerUrl() log('baseURL', baseURL) @@ -69,7 +69,7 @@ describe('integration/space/:spaceAddress/token/:tokenId', () => { expect(response.headers['content-type']).toContain('application/json') expect(name).toEqual(`${spaceMetadata.name} - Member`) expect(description).toEqual(`Member of ${spaceMetadata.name}`) - expect(image).toBe(`${baseURL}/space/${spaceAddress}/image`) + expect(image).toContain(`${baseURL}/space/${spaceAddress}/image`) const renewalPrice = attributes.find((attr) => attr.trait_type === 'Renewal Price') expect(renewalPrice).toBeDefined() @@ -92,7 +92,7 @@ describe('integration/space/:spaceAddress/token/:tokenId', () => { await runTest(spaceAddress, 0, metadata) }) - it('should return 404 /space/:spaceAddress/token/42069', async () => { + it('should return 200 - any token id is valid for a space', async () => { const metadata = { name: 'Alice Space', uri: baseURL, @@ -103,6 +103,6 @@ describe('integration/space/:spaceAddress/token/:tokenId', () => { const response = await axios.get( `${baseURL}/space/${spaceAddress}/token/42069`, ) - expect(response.status).toBe(404) + expect(response.status).toBe(200) }) }) diff --git a/packages/stream-metadata/tests/integration/spaceMetadata.test.ts b/packages/stream-metadata/tests/integration/spaceMetadata.test.ts index b855c439e..979068f08 100644 --- a/packages/stream-metadata/tests/integration/spaceMetadata.test.ts +++ b/packages/stream-metadata/tests/integration/spaceMetadata.test.ts @@ -12,7 +12,6 @@ import { makeTestClient, SpaceMetadataParams, } from '../testUtils' -import { config } from '../../src/environment' import { spaceMetadataBaseUrl, SpaceMetadataResponse } from '../../src/routes/spaceMetadata' import { spaceDapp } from '../../src/contract-utils' @@ -21,7 +20,7 @@ const log = dlog('stream-metadata:test:spaceMetadata', { defaultEnabled: true, }) -describe('integration/space/:spaceAddress', () => { +describe('integration/stream-metadata/space/:spaceAddress', () => { const baseURL = getTestServerUrl() log('baseURL', baseURL) @@ -138,7 +137,7 @@ describe('integration/space/:spaceAddress', () => { imageData, ) - await bobsClient.setSpaceImage(spaceStreamId, chunkedMedia) + const { eventId } = await bobsClient.setSpaceImage(spaceStreamId, chunkedMedia) /* * 4. fetch the space metadata from the stream-metadata server. @@ -159,29 +158,15 @@ describe('integration/space/:spaceAddress', () => { } } - /* - * 5. verify the response. - */ - if (spaceUri.trim() === '' || spaceUri.toLowerCase() === spaceMetadataBaseUrl) { - // 200 response case - const { name, description, image: imageUrl } = response.data - expect(response.status).toBe(200) - expect(response.headers['content-type']).toContain('application/json') - expect(name).toEqual(expectedMetadata.name) - const expectedDescription = `${expectedMetadata.shortDescription}

${expectedMetadata.longDescription}` - expect(description).toEqual(expectedDescription) - - let expectedImageUrl = spaceUri - if (spaceUri.trim() === '' || spaceUri === config.streamMetadataBaseUrl) { - expectedImageUrl = `${spaceMetadataBaseUrl}/${spaceAddress}/image` - } - expect(imageUrl.toLowerCase()).toEqual(expectedImageUrl.toLowerCase()) - } else { - // 302 redirect case - expect(response.status).toBe(302) - expect(response.headers['location']).toBeDefined() - expect(response.headers['location']).toEqual(spaceUri.toLowerCase()) - } + const { name, description, image: imageUrl } = response.data + expect(response.status).toBe(200) + expect(response.headers['content-type']).toContain('application/json') + expect(name).toEqual(expectedMetadata.name) + const expectedDescription = `${expectedMetadata.shortDescription}

${expectedMetadata.longDescription}` + expect(description).toEqual(expectedDescription) + + const expectedImageUrl = `${spaceMetadataBaseUrl}/${spaceAddress}/image/${eventId}` + expect(imageUrl.toLowerCase()).toEqual(expectedImageUrl.toLowerCase()) } it('should return 404 /space', async () => { @@ -238,7 +223,7 @@ describe('integration/space/:spaceAddress', () => { await runSpaceImageTest(' ') }) - it('should return status 302 with spaceImage when uri is https://example.com', async () => { + it('should return status 200 even if spaceUri is https://example.com', async () => { await runSpaceImageTest('https://example.com') }) }) diff --git a/packages/stream-metadata/tests/testUtils.ts b/packages/stream-metadata/tests/testUtils.ts index 0c90da36c..acb57fa8f 100644 --- a/packages/stream-metadata/tests/testUtils.ts +++ b/packages/stream-metadata/tests/testUtils.ts @@ -42,10 +42,8 @@ export function makeUniqueSpaceStreamId(): string { export function getTestServerUrl() { // use the .env.test config to derive the baseURL of the server under test - const { host, port, riverEnv } = config - const protocol = riverEnv.startsWith('local') ? 'http' : 'https' - const baseURL = `${protocol}://${host}:${port}` - return baseURL + const { streamMetadataBaseUrl } = config + return streamMetadataBaseUrl } export async function getAnyNodeUrlFromRiverRegistry() { diff --git a/packages/stream-metadata/tsconfig.json b/packages/stream-metadata/tsconfig.json index d86cf0773..8aa3a672a 100644 --- a/packages/stream-metadata/tsconfig.json +++ b/packages/stream-metadata/tsconfig.json @@ -9,7 +9,8 @@ "strict": true, "outDir": "./dist", "rootDir": "./src", - "sourceMap": true + "sourceMap": true, + "types": ["vitest/globals"] }, "include": ["src/**/*.ts"], "exclude": ["node_modules", "dist", "tests"] diff --git a/packages/stream-metadata/vitest.config.ts b/packages/stream-metadata/vitest.config.ts new file mode 100644 index 000000000..0e01fa602 --- /dev/null +++ b/packages/stream-metadata/vitest.config.ts @@ -0,0 +1,12 @@ +import { defineConfig, mergeConfig } from 'vitest/config' +import { rootConfig } from '../../vitest.config.mjs' + +export default mergeConfig( + rootConfig, + defineConfig({ + test: { + include: ['tests/**/*.test.ts'], + setupFiles: './vitest.setup.ts', + }, + }), +) diff --git a/packages/stream-metadata/vitest.setup.ts b/packages/stream-metadata/vitest.setup.ts new file mode 100644 index 000000000..852ac04ec --- /dev/null +++ b/packages/stream-metadata/vitest.setup.ts @@ -0,0 +1,5 @@ +import dotenv from 'dotenv' +import { resolve } from 'path' +import 'fake-indexeddb/auto' + +dotenv.config({ path: resolve(__dirname, '.env.test') }) diff --git a/yarn.lock b/yarn.lock index 4bcbd0fd5..c9cda1d9c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6406,7 +6406,6 @@ __metadata: "@river-build/proto": "workspace:^" "@river-build/sdk": "workspace:^" "@river-build/web3": "workspace:^" - "@types/jest": ^29.5.3 "@types/node": ^20.5.0 "@types/uuid": ^10.0.0 "@typescript-eslint/parser": ^7.14.1 @@ -6422,17 +6421,14 @@ __metadata: ethers: ^5.7.2 fake-indexeddb: ^4.0.1 fastify: ^4.28.1 - jest: ^29.6.2 - jest-transform-stub: ^2.0.0 lru-cache: ^11.0.1 magic-bytes.js: ^1.10.0 pino: ^8.17.1 pino-pretty: ^10.2.3 prettier: ^2.8.8 - ts-jest: ^29.1.1 - ts-node: ^10.9.1 typescript: ^5.1.6 uuid: ^8.3.2 + vitest: ^2.1.5 zod: ^3.21.4 languageName: unknown linkType: soft @@ -18686,13 +18682,6 @@ __metadata: languageName: node linkType: hard -"jest-transform-stub@npm:^2.0.0": - version: 2.0.0 - resolution: "jest-transform-stub@npm:2.0.0" - checksum: c46134257e5fb047f5952e4a4a1339925dd009da15aa8d3310ba1d3e4d22266ed6c675cd18e58c9d5ca2246718d6cc8ff00ea6a4b6f3b2a6b285b6426f6ae8fe - languageName: node - linkType: hard - "jest-util@npm:>=27, jest-util@npm:^29.0.0, jest-util@npm:^29.6.2, jest-util@npm:^29.7.0": version: 29.7.0 resolution: "jest-util@npm:29.7.0"