From 68a9298df9e2177e70333f47dc433a056d76625b Mon Sep 17 00:00:00 2001 From: Samuel Alev Date: Thu, 25 Jan 2024 14:25:24 +0100 Subject: [PATCH] feat: add testing export to package json (#714) * feat: add testing export to package json * fix test * lint fix * fix test again --- .changeset/chatty-jokes-invite.md | 11 + ...uideline-blocks-continuous-integration.yml | 12 +- packages/app-bridge/package.json | 35 +- packages/app-bridge/src/helpers/link.spec.ts | 2 +- .../src/react/useBlockSettings.spec.tsx | 2 +- .../{vite.config.ts => vite.config.mts} | 28 +- .../guideline-blocks-settings/setupTests.ts | 7 +- .../src/hooks/useAttachments.spec.tsx | 12 +- pnpm-lock.yaml | 1491 +++++++++++++++-- 9 files changed, 1428 insertions(+), 172 deletions(-) create mode 100644 .changeset/chatty-jokes-invite.md rename packages/app-bridge/{vite.config.ts => vite.config.mts} (61%) diff --git a/.changeset/chatty-jokes-invite.md b/.changeset/chatty-jokes-invite.md new file mode 100644 index 000000000..85f83521f --- /dev/null +++ b/.changeset/chatty-jokes-invite.md @@ -0,0 +1,11 @@ +--- +"@frontify/app-bridge": minor +--- + +Split the testing utilities out of the main bundle, so they don't end up in production builds. +You will need to update the import paths in your tests: + +```git +- import { AssetDummy, withAppBridgeBlockStubs } from '@frontify/app-bridge'; ++ import { AssetDummy, withAppBridgeBlockStubs } from '@frontify/app-bridge/testing'; +``` diff --git a/.github/workflows/guideline-blocks-continuous-integration.yml b/.github/workflows/guideline-blocks-continuous-integration.yml index fe24015c7..c0ad4c0c6 100644 --- a/.github/workflows/guideline-blocks-continuous-integration.yml +++ b/.github/workflows/guideline-blocks-continuous-integration.yml @@ -17,10 +17,12 @@ env: jobs: lint-typecheck: runs-on: ubuntu-latest + timeout-minutes: 10 + strategy: + fail-fast: false matrix: app-bridge-version: ["3.0.0", "workspace:^"] - timeout-minutes: 10 steps: - name: Checkout default branch @@ -60,10 +62,12 @@ jobs: unit-tests: runs-on: ubuntu-latest + timeout-minutes: 10 + strategy: + fail-fast: false matrix: app-bridge-version: ["3.0.0", "workspace:^"] - timeout-minutes: 10 steps: - name: Checkout current commit @@ -110,10 +114,12 @@ jobs: component-tests: runs-on: ubuntu-latest + timeout-minutes: 10 + strategy: + fail-fast: false matrix: app-bridge-version: ["3.0.0", "workspace:^"] - timeout-minutes: 10 steps: - name: Checkout current commit diff --git a/packages/app-bridge/package.json b/packages/app-bridge/package.json index cb95346d4..122ca5ec8 100644 --- a/packages/app-bridge/package.json +++ b/packages/app-bridge/package.json @@ -21,8 +21,13 @@ "exports": { ".": { "types": "./dist/index.d.ts", - "import": "./dist/index.es.js", - "require": "./dist/index.umd.js" + "import": "./dist/index.mjs", + "require": "./dist/index.js" + }, + "./testing": { + "types": "./dist/testing.d.ts", + "import": "./dist/testing.mjs", + "require": "./dist/testing.js" }, "./upload-worker": "./dist/upload.worker.js" }, @@ -44,20 +49,20 @@ "immer": "^10.0.3", "lodash-es": "^4.17.21", "mitt": "^3.0.1", - "type-fest": "^4.8.2" + "type-fest": "^4.10.1" }, "devDependencies": { "@frontify/eslint-config-react": "^0.16.1", "@testing-library/react": "^14.1.2", "@types/lodash-es": "^4.17.12", - "@types/react": "^18.2.38", - "@types/react-dom": "^18.2.16", - "@types/sinon": "^17.0.2", - "@vitest/coverage-v8": "^0.34.6", - "@vitest/ui": "^0.34.6", - "eslint": "^8.54.0", + "@types/react": "^18.2.48", + "@types/react-dom": "^18.2.18", + "@types/sinon": "^17.0.3", + "@vitest/coverage-v8": "^1.2.1", + "@vitest/ui": "^1.2.1", + "eslint": "^8.56.0", "eslint-plugin-notice": "^0.9.10", - "happy-dom": "^12.10.3", + "happy-dom": "^13.2.1", "mitt": "^3.0.1", "msw": "^1.3.2", "prettier": "^3.1.0", @@ -65,11 +70,11 @@ "react-dom": "^18.2.0", "sinon": "^17.0.1", "ts-json-as-const": "^1.0.7", - "typescript": "^5.3.2", - "vite": "^4.5.0", - "vite-plugin-dts": "^3.6.3", - "vite-plugin-static-copy": "^0.17.1", - "vitest": "^0.34.6" + "typescript": "^5.3.3", + "vite": "^5.0.12", + "vite-plugin-dts": "^3.7.2", + "vite-plugin-static-copy": "^1.0.0", + "vitest": "^1.2.1" }, "peerDependencies": { "react": "^18", diff --git a/packages/app-bridge/src/helpers/link.spec.ts b/packages/app-bridge/src/helpers/link.spec.ts index 1ff583766..58e371d5a 100644 --- a/packages/app-bridge/src/helpers/link.spec.ts +++ b/packages/app-bridge/src/helpers/link.spec.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'; import { CoverPageDummy } from '../tests/CoverPageDummy'; import { computeCoverPageLink, computeDocumentLink, computeDocumentPageLink } from './link'; -import { DocumentCategoryDummy, DocumentDummy, DocumentPageDummy } from '..'; +import { DocumentCategoryDummy, DocumentDummy, DocumentPageDummy } from '../tests'; const PORTAL_ID = 23451; const PORTAL_TOKEN = 'weisdfjsdf'; diff --git a/packages/app-bridge/src/react/useBlockSettings.spec.tsx b/packages/app-bridge/src/react/useBlockSettings.spec.tsx index bd4c0a13b..110fa2c75 100644 --- a/packages/app-bridge/src/react/useBlockSettings.spec.tsx +++ b/packages/app-bridge/src/react/useBlockSettings.spec.tsx @@ -5,7 +5,7 @@ import React, { ReactElement } from 'react'; import sinon from 'sinon'; import { afterEach, describe, expect, it } from 'vitest'; import type { AppBridgeBlock } from '../AppBridgeBlock'; -import { withAppBridgeBlockStubs } from '../tests/'; +import { withAppBridgeBlockStubs } from '../tests/withAppBridgeBlockStubs'; import { useBlockSettings } from './useBlockSettings'; const BLOCK_SETTINGS_DIV_ID = 'block-settings-div'; diff --git a/packages/app-bridge/vite.config.ts b/packages/app-bridge/vite.config.mts similarity index 61% rename from packages/app-bridge/vite.config.ts rename to packages/app-bridge/vite.config.mts index f279daba1..39c6c9369 100644 --- a/packages/app-bridge/vite.config.ts +++ b/packages/app-bridge/vite.config.mts @@ -1,6 +1,5 @@ /* (c) Copyright Frontify Ltd., all rights reserved. */ -import { resolve } from 'path'; import dts from 'vite-plugin-dts'; import { viteStaticCopy } from 'vite-plugin-static-copy'; import { defineConfig } from 'vitest/config'; @@ -23,32 +22,15 @@ export default defineConfig({ ], build: { lib: { - entry: resolve(__dirname, 'src/index.ts'), - fileName: (format: string) => `[name].${format}.js`, + entry: { + index: './src/index.ts', + testing: './src/tests/index.ts', + }, }, sourcemap: true, minify: true, rollupOptions: { external: [...dependencies, ...peerDependencies], - output: [ - { - name: 'AppBridge', - format: 'es', - preserveModules: true, - preserveModulesRoot: 'src', - globals, - }, - { - name: 'AppBridge', - format: 'umd', - globals, - }, - { - name: 'AppBridge', - format: 'cjs', - globals, - }, - ], }, }, test: { @@ -59,6 +41,6 @@ export default defineConfig({ include: ['src/**/*.ts', 'src/**/*.tsx'], exclude: ['src/**/test.ts', 'src/**/test.tsx', 'src/**/spec.ts', 'src/**/spec.tsx'], }, - setupFiles: [resolve(__dirname, 'src/setupTests.ts')], + setupFiles: ['./src/setupTests.ts'], }, }); diff --git a/packages/guideline-blocks-settings/setupTests.ts b/packages/guideline-blocks-settings/setupTests.ts index 78bfeb7d6..478a8863b 100644 --- a/packages/guideline-blocks-settings/setupTests.ts +++ b/packages/guideline-blocks-settings/setupTests.ts @@ -1,11 +1,16 @@ /* (c) Copyright Frontify Ltd., all rights reserved. */ -import { afterEach, vi } from 'vitest'; +import { afterEach, beforeAll, vi } from 'vitest'; +import { configure } from '@testing-library/react'; vi.stubGlobal('crypto', { getRandomValues: vi.fn(), }); +beforeAll(() => { + configure({ testIdAttribute: 'data-test-id' }); +}); + afterEach(() => { vi.restoreAllMocks(); }); diff --git a/packages/guideline-blocks-settings/src/hooks/useAttachments.spec.tsx b/packages/guideline-blocks-settings/src/hooks/useAttachments.spec.tsx index 74282da81..ddf5c1851 100644 --- a/packages/guideline-blocks-settings/src/hooks/useAttachments.spec.tsx +++ b/packages/guideline-blocks-settings/src/hooks/useAttachments.spec.tsx @@ -8,11 +8,7 @@ import { AttachmentsProvider, useAttachments, useAttachmentsContext, withAttachm const MOCK_SETTINGS_ID = 'attachments'; -/** - * @vitest-environment happy-dom - */ - -describe('useAttachments', () => { +describe('useAttachments', async () => { it('should have 1 attachment if attachment is added', async () => { const STUB_WITH_NO_ASSETS = getAppBridgeBlockStub({ blockId: 1, @@ -115,7 +111,7 @@ describe('withAttachmentsProvider', () => { return (