Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat.move-error-re…
Browse files Browse the repository at this point in the history
…porting-to-core-sdk-2826
  • Loading branch information
saikumarrs committed Jan 12, 2025
2 parents 8451e77 + 959b84c commit f27ad1c
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 141 deletions.
5 changes: 0 additions & 5 deletions jest.config.ts

This file was deleted.

105 changes: 34 additions & 71 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
}
},
"targetDefaults": {
"test": {
"inputs": ["default", "^test"],
"test*": {
"inputs": ["test"],
"cache": true
},
"version": [
Expand All @@ -33,62 +33,28 @@
"projects": "dependencies"
}
],
"build": {
"inputs": ["default", "^production"],
"*build*": {
"inputs": ["default"],
"cache": true
},
"build:modern": {
"inputs": ["default", "^production"],
"cache": true
},
"build:browser": {
"inputs": ["default", "^production"],
"cache": true
},
"build:browser:modern": {
"inputs": ["default", "^production"],
"cache": true
},
"build:package": {
"inputs": ["default", "^production"],
"cache": true
},
"build:package:modern": {
"inputs": ["default", "^production"],
"cache": true
},
"check:size:build": {
"inputs": ["default", "^production"],
"cache": true
},
"check:size:json": {
"inputs": [
"default",
"^production",
"{projectRoot}/.size-limit.js",
"{projectRoot}/.size-limit.mjs"
],
"check:size:json*": {
"inputs": ["default", "{projectRoot}/.size-limit.js", "{projectRoot}/.size-limit.mjs"],
"cache": true
},
"check:pub": {
"inputs": ["{projectRoot}/package.json"],
"cache": true
},
"test:pre-commit:affected": {
"inputs": ["default", "^test"],
"check:circular": {
"inputs": ["default"],
"cache": true
},
"check:size:build:pre-commit:affected": {
"inputs": ["default", "^production"],
"check:duplicates": {
"inputs": ["default", "{projectRoot}/.jscpd.json", "{workspaceRoot}/.jscpd.json"],
"cache": true
},
"check:size:json:pre-commit:affected": {
"inputs": [
"default",
"^production",
"{projectRoot}/.size-limit.js",
"{projectRoot}/.size-limit.mjs"
],
"check:lint*": {
"inputs": ["default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/.eslintignore"],
"cache": true
}
},
Expand All @@ -99,49 +65,46 @@
},
"namedInputs": {
"sharedGlobals": [
"{workspaceRoot}/.nvmrc",
{
"runtime": "node --version"
}
},
"{workspaceRoot}/package.json",
"{workspaceRoot}/package-lock.json",
"{workspaceRoot}/nx.json",
"{workspaceRoot}/tsconfig.build.json",
"{workspaceRoot}/tsconfig.json",
"{workspaceRoot}/tsconfig.paths.json"
],
"default": [
"sharedGlobals",
"{projectRoot}/**/*",
"!{projectRoot}/.env.example",
"!{projectRoot}/public/**/*",
"!{projectRoot}/LICENSE.md",
"{projectRoot}/src/**/*",
"{projectRoot}/package.json",
"{projectRoot}/project.json",
"{projectRoot}/rollup.config.mjs",
"{projectRoot}/babel.config.json",
"{projectRoot}/tsconfig.json",
"!{projectRoot}/src/**/*.(spec|test).(j|t)s?(x)",
"!{projectRoot}/**/*.md",
"!{projectRoot}/**/*.txt",
"!{projectRoot}/serve.json",
"!{projectRoot}/.size-limit.js",
"!{projectRoot}/.size-limit.mjs",
"!{projectRoot}/.jscpd.json",
"{workspaceRoot}/tsconfig.build.json",
"{workspaceRoot}/tsconfig.json",
"{workspaceRoot}/tsconfig.paths.json",
"{workspaceRoot}/package.json",
"{workspaceRoot}/package-lock.json",
"{workspaceRoot}/babel.config.json",
"{workspaceRoot}/types/**/*",
"{workspaceRoot}/nx.json"
],
"production": [
"{projectRoot}/src/**/*",
"!{projectRoot}/src/**/*.(spec|test).(j|t)s?(x)",
"!{projectRoot}/__tests__/**/*",
"!{projectRoot}/__fixtures__/**/*",
"!{projectRoot}/__mocks__/**/*",
"!{projectRoot}/jest.config.mjs"
"{workspaceRoot}/babel.config.json"
],
"test": [
"sharedGlobals",
"{projectRoot}/src/**/*",
"{projectRoot}/package.json",
"{projectRoot}/project.json",
"!{projectRoot}/**/*.md",
"!{projectRoot}/**/*.txt",
"{projectRoot}/src/**/*.(spec|test).(j|t)s?(x)",
"{projectRoot}/__tests__/**/*",
"{projectRoot}/__fixtures__/**/*",
"{projectRoot}/__mocks__/**/*",
"{projectRoot}/jest.config.mjs",
"{workspaceRoot}/jest/**/*",
"{workspaceRoot}/jest.preset.js",
"{workspaceRoot}/jest.config.js"
"{projectRoot}/tsconfig.spec.json"
]
}
}
8 changes: 8 additions & 0 deletions packages/analytics-js/__fixtures__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const dummyWriteKey = 'dummyWriteKey';

const dummyDataplaneHost = 'https://dummy.dataplane.host.com';

const dummyCDNHost = 'https://cdn.dummyhost123.com';

const SDK_FILE_BASE_PATH = '../dist/cdn/legacy/iife';
const SDK_FILE_NAME = 'rsa.js';

const dummyInitOptions = {
timeout: false,
flushAt: 1,
Expand Down Expand Up @@ -762,4 +767,7 @@ export {
entriesWithoutCookieStorage,
entriesWithoutCookieAndLocalStorage,
entriesWithMixStorageButWithoutNone,
dummyCDNHost,
SDK_FILE_BASE_PATH,
SDK_FILE_NAME,
};
22 changes: 21 additions & 1 deletion packages/analytics-js/__fixtures__/msw.handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { http, HttpResponse } from 'msw';
import { dummyDataplaneHost, dummySourceConfigResponse } from './fixtures';
import {
dummyCDNHost,
dummyDataplaneHost,
dummySourceConfigResponse,
SDK_FILE_BASE_PATH,
SDK_FILE_NAME,
} from './fixtures';
import fs from 'fs';
import path from 'path';

const handlers = [
http.get(`${dummyDataplaneHost}/rawSample`, () => {
Expand Down Expand Up @@ -103,6 +111,18 @@ const handlers = [
},
});
}),
http.get(`${dummyCDNHost}/modern/${SDK_FILE_NAME}`, () => {
const scriptContent = fs.readFileSync(
path.join(path.resolve(__dirname, SDK_FILE_BASE_PATH), SDK_FILE_NAME),
'utf-8',
);
return new HttpResponse(scriptContent, {
status: 200,
headers: {
'Content-Type': 'text/javascript',
},
});
}),
];

export { handlers };
56 changes: 33 additions & 23 deletions packages/analytics-js/__tests__/browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
import { dummyCDNHost, SDK_FILE_NAME } from '../__fixtures__/fixtures';
import { loadingSnippet } from './nativeSdkLoader';
import { server } from '../__fixtures__/msw.server';

describe('Test suite for the SDK', () => {
beforeAll(() => {
// Start the server before running the tests
// This is necessary to serve the local SDK script on the dummy CDN host
server.listen();
});

afterAll(() => {
server.close();
});

const WRITE_KEY = 'write-key';
const DATA_PLANE_URL = 'https://example.dataplane.com';
const SDK_READY_TIMEOUT = 5000; // 5 seconds

const MOCK_SOURCE_CONFIGURATION = {
updatedAt: new Date().toISOString(),
Expand Down Expand Up @@ -51,21 +62,31 @@ describe('Test suite for the SDK', () => {
'group-trait-key-2': 'group-trait-value-2',
};

const SDK_PATH = '../dist/cdn/legacy/iife/rsa.js';
const loadSDKScript = () => {
loadingSnippet(dummyCDNHost, SDK_FILE_NAME, WRITE_KEY, DATA_PLANE_URL);
};

const loadAndWaitForSDK = async () => {
const readyPromise = new Promise(resolve => {
const waitForSDKReady = async () => {
const readyPromise = new Promise((resolve, reject) => {
// eslint-disable-next-line sonarjs/no-nested-functions
window.rudderanalytics?.ready(() => resolve(true));
});

require(SDK_PATH);
setTimeout(
() => reject(new Error('The SDK did not become ready within the timeout')),
SDK_READY_TIMEOUT,
);
});

await readyPromise;
};

const originalXMLHttpRequest = window.XMLHttpRequest;

beforeEach(() => {
// Mocking the xhr function
window.XMLHttpRequest = jest.fn(() => xhrMock) as unknown as typeof XMLHttpRequest;
});

afterEach(() => {
jest.resetModules();
jest.clearAllMocks();
Expand All @@ -77,15 +98,13 @@ describe('Test suite for the SDK', () => {

describe('preload buffer', () => {
it('should process the buffered API calls when SDK script is loaded', async () => {
// Mocking the xhr function
window.XMLHttpRequest = jest.fn(() => xhrMock) as unknown as typeof XMLHttpRequest;

loadingSnippet(WRITE_KEY, DATA_PLANE_URL);
loadSDKScript();

// Queue up some API calls before the SDK script is loaded
window.rudderanalytics?.page();
window.rudderanalytics?.track('test-event');

await loadAndWaitForSDK();
await waitForSDKReady();

expect((window.rudderanalytics as any).push).not.toBe(Array.prototype.push);

Expand All @@ -96,18 +115,9 @@ describe('Test suite for the SDK', () => {

describe('api', () => {
beforeEach(async () => {
// Mocking the xhr function
window.XMLHttpRequest = jest.fn(() => xhrMock) as unknown as typeof XMLHttpRequest;

loadingSnippet(WRITE_KEY, DATA_PLANE_URL);

await loadAndWaitForSDK();

window.rudderanalytics?.reset();
});
loadSDKScript();

afterEach(() => {
window.XMLHttpRequest = originalXMLHttpRequest;
await waitForSDKReady();
});

it('should make network requests when event APIs are invoked', () => {
Expand Down
Loading

0 comments on commit f27ad1c

Please sign in to comment.