Skip to content

Commit

Permalink
Merge branch 'master' into test-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ugran committed Mar 7, 2024
2 parents a2b4653 + 50a969b commit 10fcf65
Show file tree
Hide file tree
Showing 279 changed files with 10,226 additions and 3,642 deletions.
26 changes: 21 additions & 5 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ steps:
- PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 HUSKY=0 yarn install --immutable
- echo "+++ Run tests"
- yarn constraints
- yarn run test:scripts
- yarn scripts test
- yarn scripts lint
- yarn run test:check-dts
plugins:
- ssh://[email protected]/segmentio/cache-buildkite-plugin#v2.0.0:
Expand Down Expand Up @@ -98,6 +99,21 @@ steps:
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}"
paths: ['.yarn/cache/']

- label: '[Generic Utils] Lint + Test'
agents:
queue: v1
commands:
- npm config set "//registry.npmjs.org/:_authToken" $${NPM_TOKEN}
- echo "--- Install dependencies"
- PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 HUSKY=0 yarn install --immutable
- echo "+++ Run QA"
- yarn turbo run --filter=analytics-generic-utils lint
- yarn turbo run --filter=analytics-generic-utils test
plugins:
- ssh://[email protected]/segmentio/cache-buildkite-plugin#v2.0.0:
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}"
paths: ['.yarn/cache/']

- label: '[Consent] Lint + Test'
agents:
queue: v1
Expand Down Expand Up @@ -148,18 +164,18 @@ steps:
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}"
paths: ['.yarn/cache/']

- label: '[Examples] Lint + Test :hammer:'
key: examples
- label: '[Playgrounds] Lint + Test :hammer:'
key: playgrounds
agents:
queue: v1
commands:
- npm config set "//registry.npmjs.org/:_authToken" $${NPM_TOKEN}
- echo "--- Install dependencies"
- PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 HUSKY=0 yarn install --immutable
- echo "--- Build bundles"
- yarn turbo run --filter='./examples/*' build
- yarn turbo run --filter='./playgrounds/*' build
- echo "+++ Run tests"
- yarn turbo run --filter='./examples/*' lint
- yarn turbo run --filter='./playgrounds/*' lint
plugins:
- ssh://[email protected]/segmentio/cache-buildkite-plugin#v2.0.0:
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}"
Expand Down
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"ignore": ["@example/*"],
"ignore": ["@playground/*"],
"changelog": [
"@changesets/changelog-github",
{
Expand Down
21 changes: 20 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
project: [
'./tsconfig.json',
'./packages/*/tsconfig.json',
'./examples/*/tsconfig.json',
'./playgrounds/*/tsconfig.json',
],
},
extends: [
Expand Down Expand Up @@ -61,11 +61,30 @@ module.exports = {
'jest/no-focused-tests': process.env.CI ? 'error' : 'off',
},
overrides: [
{
files: ['**/src/**'],
rules: {
'@typescript-eslint/no-restricted-imports': [
'error',
{
paths: [
{
// Prevent accidental imports from 'lodash'
name: 'lodash',
message:
'Lodash should only be used for dev-related things, and not in any public library src code (e.g. @segment/analytics-next)',
},
],
},
],
},
},
{
files: ['**/__tests__/**', '**/scripts/**'],
rules: {
'require-await': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/no-restricted-imports': 'off',
},
},
],
Expand Down
33 changes: 26 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
jobs:
analytics-node:
name: "analytics-node QA (Node.js v${{ matrix.node-version }})"
name: 'analytics-node QA (Node.js v${{ matrix.node-version }})'
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 14, 16, 18 ]
node-version: [18]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"
cache: 'yarn'
- run: yarn install --immutable
- name: Turbo cache
uses: actions/cache@v3
Expand All @@ -27,6 +27,28 @@ jobs:
${{ runner.os }}-turbo-
- run: yarn turbo run --filter='./packages/node*' lint
- run: yarn turbo run --filter='./packages/node*' test
- run: yarn turbo run --filter='./packages/node-integration-tests' test:perf-and-durability
analytics-node-cf-workers:
name: 'analytics-node QA (Cloudflare Workers)'
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn install --immutable
- name: Turbo cache
uses: actions/cache@v3
with:
path: node_modules/.cache/turbo
key: ${{ runner.os }}-turbo-${{ github.sha }}
restore-keys: |
${{ runner.os }}-turbo-
- run: yarn turbo run --filter='./packages/node-integration-tests' test:cloudflare-workers
consent-intg-tests:
name: Consent Integration Tests
runs-on: ubuntu-latest
Expand All @@ -37,7 +59,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 16
cache: "yarn"
cache: 'yarn'
- run: yarn install --immutable
- name: Turbo cache
uses: actions/cache@v3
Expand All @@ -47,6 +69,3 @@ jobs:
restore-keys: |
${{ runner.os }}-turbo-
- run: yarn turbo run --filter='consent-tools-integration-tests' test:int



2 changes: 1 addition & 1 deletion .github/workflows/create-github-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
run: |
git config --global user.name "Segment Github"
git config --global user.email "[email protected]"
yarn ts-node-script --files scripts/create-release-from-tags/run.ts
yarn scripts create-release-from-tags
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"eslint.codeActionsOnSave.mode": "all",
"eslint.format.enable": true,
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 🎉 Flagship 🎉
This library is one of Segment’s most popular Flagship libraries. It is actively maintained by Segment, benefitting from new feature releases and ongoing support.


<div style="display:flex;margin-bottom:5px">
<span style="margin: 0 5px 5px">
<a href="https://buildkite.com/segment/analytics-next">
Expand Down
2 changes: 1 addition & 1 deletion constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gen_enforced_dependency(WorkspaceCwd, DependencyIdent, DependencyRange2, Depende
DependencyType2 \= 'peerDependencies',
% A list of exception to same version rule
\+ member(DependencyIdent, [
% Allow examples to use different versions of react and
% Allow playgrounds to use different versions of react and
'react', 'react-dom',
'@types/react',
% Allow the usage of workspace^ -- there is a better way to do this =)
Expand Down
9 changes: 0 additions & 9 deletions examples/with-next-js/README.md

This file was deleted.

2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = () =>
'<rootDir>/packages/core-integration-tests',
'<rootDir>/packages/node',
'<rootDir>/packages/browser',
'<rootDir>/packages/generic-utils',
'<rootDir>/packages/consent/consent-tools',
'<rootDir>/packages/consent/consent-wrapper-onetrust',
'<rootDir>/scripts',
],
})
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
"private": true,
"version": "0.0.0",
"workspaces": [
"examples/*",
"playgrounds/*",
"packages/*",
"packages/consent/*"
"packages/consent/*",
"scripts"
],
"engines": {
"node": "^16.16.0"
},
"scripts": {
"test": "jest",
"test:scripts": "jest --config scripts/jest.config.js",
"test:check-dts": "yarn build && yarn ts-node meta-tests/check-dts.ts",
"test:node-int": "turbo run --filter=node-integration-tests test",
"lint": "yarn constraints && turbo run lint --continue",
"build": "turbo run build --filter='./packages/**'",
"watch": "turbo run watch --filter='./packages/**'",
"dev": "yarn workspace @example/with-next-js run dev",
"dev": "yarn workspace @playground/next-playground run dev",
"postinstall": "husky install",
"changeset": "changeset",
"update-versions-and-changelogs": "changeset version && yarn version-run-all && bash scripts/update-lockfile.sh",
Expand All @@ -27,16 +27,17 @@
"core": "yarn workspace @segment/analytics-core",
"browser": "yarn workspace @segment/analytics-next",
"node": "yarn workspace @segment/analytics-node",
"scripts": "yarn workspace @internal/scripts",
"clean": "bash scripts/clean.sh",
"turbo": "turbo"
},
"packageManager": "[email protected]",
"devDependencies": {
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.2",
"@npmcli/promise-spawn": "^3.0.0",
"@npmcli/promise-spawn": "^7.0.0",
"@types/express": "4",
"@types/jest": "^28.1.1",
"@types/jest": "^29.5.11",
"@types/lodash": "^4",
"@types/node-fetch": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^5.21.0",
Expand All @@ -50,15 +51,15 @@
"express": "^4.18.2",
"get-monorepo-packages": "^1.2.0",
"husky": "^8.0.0",
"jest": "^28.1.0",
"jest": "^29.7.0",
"lint-staged": "^13.0.0",
"lodash": "^4.17.21",
"nock": "^13.3.0",
"node-gyp": "^9.0.0",
"prettier": "^2.6.2",
"ts-jest": "^28.0.4",
"ts-jest": "^29.1.1",
"ts-node": "^10.8.0",
"turbo": "^1.3.1",
"turbo": "^1.10.14",
"typescript": "^4.7.0",
"webpack": "^5.76.0",
"webpack-dev-server": "^4.15.1"
Expand Down
54 changes: 54 additions & 0 deletions packages/browser-integration-tests/src/custom-global-key.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { test, expect } from '@playwright/test'
import { standaloneMock } from './helpers/standalone-mock'
import { extractWriteKeyFromUrl } from './helpers/extract-writekey'
import { CDNSettingsBuilder } from '@internal/test-helpers'

test.describe('Segment with custom global key', () => {
test.beforeEach(standaloneMock)
test.beforeEach(async ({ context }) => {
await context.route(
'https://cdn.segment.com/v1/projects/*/settings',
(route, request) => {
if (request.method().toLowerCase() !== 'get') {
return route.continue()
}

const writeKey = extractWriteKeyFromUrl(request.url()) || 'writeKey'
return route.fulfill({
status: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(new CDNSettingsBuilder({ writeKey }).build()),
})
}
)
})

test('supports using a custom global key', async ({ page }) => {
// Load analytics.js
await page.goto('/standalone-custom-key.html')
await page.evaluate(() => {
;(window as any).segment_analytics.track('track before load')
;(window as any).segment_analytics.load('fake-key')
})

const req = await page.waitForRequest('https://api.segment.io/v1/t')

// confirm that any events triggered before load have been sent
expect(req.postDataJSON().event).toBe('track before load')

const contextObj = await page.evaluate(() =>
(window as any).segment_analytics.track('track after load')
)

// confirm that any events triggered after load return a regular context object
expect(contextObj).toMatchObject(
expect.objectContaining({
attempts: expect.anything(),
event: expect.objectContaining({ event: 'track after load' }),
stats: expect.objectContaining({ metrics: expect.anything() }),
})
)
})
})
Loading

0 comments on commit 10fcf65

Please sign in to comment.