Skip to content

Commit

Permalink
feat(sdk-analytics) fix test in ci/cd
Browse files Browse the repository at this point in the history
  • Loading branch information
oidacra committed Dec 18, 2024
1 parent 5cf2435 commit a98e606
Show file tree
Hide file tree
Showing 18 changed files with 1,124 additions and 457 deletions.
12 changes: 12 additions & 0 deletions core-web/libs/sdk/analytics/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@nx/react/babel",
{
"runtime": "automatic",
"useBuiltIns": "usage"
}
]
],
"plugins": []
}
15 changes: 0 additions & 15 deletions core-web/libs/sdk/analytics/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": [
"error",
{
"ignoredDependencies": [
"@testing-library/jest-dom",
"@testing-library/react"
]
}
]
}
}
]
}
29 changes: 0 additions & 29 deletions core-web/libs/sdk/analytics/.swcrc

This file was deleted.

36 changes: 14 additions & 22 deletions core-web/libs/sdk/analytics/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
/* eslint-disable */
import { readFileSync } from 'fs';

// Reading the SWC compilation config and remove the "exclude"
// for the test files to be compiled by SWC
const { exclude: _, ...swcJestConfig } = JSON.parse(readFileSync(`${__dirname}/.swcrc`, 'utf-8'));

// disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves.
// If we do not disable this, SWC Core will read .swcrc and won't transform our test files due to "exclude"
if (swcJestConfig.swcrc === undefined) {
swcJestConfig.swcrc = false;
}

// Uncomment if using global setup/teardown files being transformed via swc
// https://nx.dev/nx-api/jest/documents/overview#global-setupteardown-with-nx-libraries
// jest needs EsModule Interop to find the default exported setup/teardown functions
// swcJestConfig.module.noInterop = false;

export default {
displayName: 'analytics',
displayName: 'sdk-analytics',
preset: '../../../jest.preset.js',
transform: {
'^.+\\.[tj]sx?$': ['@swc/jest', swcJestConfig]
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
'^.+\\.[tj]sx?$': [
'babel-jest',
{
presets: ['@nx/react/babel'],
plugins: [
['@babel/plugin-proposal-private-methods', { loose: true }],
['@babel/plugin-proposal-private-property-in-object', { loose: true }],
['@babel/plugin-proposal-class-properties', { loose: true }]
]
}
]
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
testEnvironment: 'jsdom',
coverageDirectory: '../../../coverage/libs/sdk/analytics',
setupFilesAfterEnv: ['<rootDir>/test-setup.ts']
coverageDirectory: '../../../coverage/libs/sdk/analytics'
};
41 changes: 2 additions & 39 deletions core-web/libs/sdk/analytics/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,14 @@
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/sdk/analytics/src",
"projectType": "library",
"tags": ["type:lib", "scope:sdk", "feature:analytics"],
"targets": {
"build": {
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/sdk/analytics",
"main": "libs/sdk/analytics/src/index.ts",
"project": "libs/sdk/analytics/package.json",
"additionalEntryPoints": ["libs/sdk/analytics/src/lib/react/index.ts"],
"tsConfig": "libs/sdk/analytics/tsconfig.lib.json",
"format": ["esm"],
"external": ["react"],
"preserveModules": true,
"preserveModulesRoot": "libs/sdk/analytics/src",
"rollupConfig": "libs/sdk/analytics/rollup.config.js",
"compiler": "swc",
"assets": [
{
"glob": "README.md",
"input": "libs/sdk/analytics",
"output": "."
}
],
"sourceMap": true,
"skipTypeCheck": false,
"deleteOutputPath": true
}
},
"build:standalone": {
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "../../core/dotCMS/src/main/resources/ca/html",
"main": "libs/sdk/analytics/src/lib/standalone.ts",
"tsConfig": "libs/sdk/analytics/tsconfig.lib.json",
"project": "libs/sdk/analytics/package.json"
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/sdk/analytics/jest.config.ts"
}
}
},
"tags": ["type:lib", "scope:sdk", "feature:analytics"]
}
}
12 changes: 0 additions & 12 deletions core-web/libs/sdk/analytics/rollup.config.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest } from '@jest/globals';
import '@testing-library/jest-dom';
import { render, waitFor } from '@testing-library/react';
import React from 'react';

import { DotContentAnalyticsProvider } from './DotContentAnalyticsProvider';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { jest } from '@jest/globals';
import { renderHook } from '@testing-library/react-hooks';
import React, { ReactNode } from 'react';
import { ReactNode } from 'react';

import { useContentAnalytics } from './useContentAnalytics';

import { isInsideEditor } from '../../dotAnalytics/shared/dot-content-analytics.utils';
import DotContentAnalyticsContext from '../contexts/DotContentAnalyticsContext';
jest.mock('../../dotAnalytics/shared/dot-content-analytics.utils', () => {
return {
isInsideEditor: jest.fn()
};
});

const mockIsInsideEditor = jest.mocked(isInsideEditor);

const mockTrack = jest.fn();

interface WrapperProps {
children: ReactNode;
}

const mockTrack = jest.fn();

const wrapper = ({ children }: WrapperProps) => (
<DotContentAnalyticsContext.Provider
value={{
Expand All @@ -21,12 +29,6 @@ const wrapper = ({ children }: WrapperProps) => (
</DotContentAnalyticsContext.Provider>
);

const mockIsInsideEditor = jest.fn();

jest.mock('../../dotAnalytics/shared/dot-content-analytics.utils', () => ({
isInsideEditor: mockIsInsideEditor
}));

describe('useContentAnalytics', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -42,7 +44,7 @@ describe('useContentAnalytics', () => {
});

it('should track with timestamp when outside editor', () => {
mockIsInsideEditor.mockImplementation(() => false);
mockIsInsideEditor.mockReturnValue(false);

const mockDate = '2024-01-01T00:00:00.000Z';
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue(mockDate);
Expand All @@ -57,7 +59,7 @@ describe('useContentAnalytics', () => {
});

it('should handle undefined payload', () => {
mockIsInsideEditor.mockImplementation(() => false);
mockIsInsideEditor.mockReturnValue(false);

const mockDate = '2024-01-01T00:00:00.000Z';
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue(mockDate);
Expand Down
4 changes: 0 additions & 4 deletions core-web/libs/sdk/analytics/test-setup.ts

This file was deleted.

7 changes: 4 additions & 3 deletions core-web/libs/sdk/analytics/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true
"strict": true,
"types": ["vite/client"]
},
"files": [],
"include": [],
Expand All @@ -16,5 +16,6 @@
{
"path": "./tsconfig.spec.json"
}
]
],
"extends": "../../../tsconfig.base.json"
}
30 changes: 17 additions & 13 deletions core-web/libs/sdk/analytics/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/libs/sdk/analytics",
"types": ["node", "@nx/react/typings/cssmodule.d.ts", "@nx/react/typings/image.d.ts"]
"outDir": "../../../dist/out-tsc",
"types": [
"node",
"@nx/react/typings/cssmodule.d.ts",
"@nx/react/typings/image.d.ts",
"vite/client"
]
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": [
"jest.config.ts",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"src/**/*.spec.tsx",
"src/**/*.test.tsx",
"src/**/*.spec.js",
"src/**/*.test.js",
"src/**/*.spec.jsx",
"src/**/*.test.jsx"
]
"**/*.spec.ts",
"**/*.test.ts",
"**/*.spec.tsx",
"**/*.test.tsx",
"**/*.spec.js",
"**/*.test.js",
"**/*.spec.jsx",
"**/*.test.jsx"
],
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
}
13 changes: 12 additions & 1 deletion core-web/libs/sdk/analytics/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,16 @@
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
]
}
48 changes: 48 additions & 0 deletions core-web/libs/sdk/analytics/vite.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// <reference types='vitest' />
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
import react from '@vitejs/plugin-react';
import * as path from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';

export default defineConfig({
root: __dirname,
cacheDir: '../../../node_modules/.vite/libs/sdk/sdk-analytics',

plugins: [
react(),
nxViteTsPaths(),
dts({ entryRoot: 'src', tsconfigPath: path.join(__dirname, 'tsconfig.lib.json') })
],

// Uncomment this if you are using workers.
// worker: {
// plugins: [ nxViteTsPaths() ],
// },

// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
outDir: '../../../dist/libs/sdk/sdk-analytics',
emptyOutDir: true,
reportCompressedSize: true,
commonjsOptions: {
transformMixedEsModules: true
},
lib: {
entry: {
index: 'src/index.ts',
'react/index': 'src/lib/react/index.ts'
},
formats: ['es']
},
rollupOptions: {
external: ['react', 'react-dom', 'react/jsx-runtime'],
output: {
exports: 'named',
preserveModules: true,
preserveModulesRoot: 'src'
}
}
}
});
14 changes: 0 additions & 14 deletions core-web/libs/sdk/analytics/vite.config.ts

This file was deleted.

Loading

0 comments on commit a98e606

Please sign in to comment.