-
Notifications
You must be signed in to change notification settings - Fork 30
Add Node.js and bun tests to release pipeline #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cb-srinaths
wants to merge
10
commits into
master
Choose a base branch
from
test/bun-worker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d29b5ca
add node and bun tests to release pipeline
cb-srinaths 40aa241
fix pipelines
cb-srinaths bb4960a
fix bun test
cb-srinaths 0914945
fix tests on node 20
cb-srinaths bf0b613
fix typo
cb-srinaths 474b1b1
try again
cb-srinaths 302273a
fix worker termination
cb-srinaths 0938da8
try again
cb-srinaths 8ad9c12
try again
cb-srinaths 9476221
fix tests in all runtimes
cb-srinaths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| node: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| node-version: [20, 22, 24] | ||
|
|
||
| steps: | ||
| - name: Checkout repo | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| registry-url: 'https://registry.npmjs.org' | ||
| cache: 'npm' | ||
|
|
||
| - name: Install deps | ||
| run: npm ci | ||
|
|
||
| - name: Build | ||
| run: npm run build | ||
|
|
||
| - name: Run Node.js tests | ||
| run: npm test | ||
|
|
||
| bun: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repo | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
|
|
||
| - name: Install deps | ||
| run: npm ci | ||
|
|
||
| - name: Build | ||
| run: bun run build | ||
|
|
||
| - name: Run Bun tests | ||
| run: bun test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| node_modules | ||
| *js | ||
| !test/fixtures/**/*.mjs | ||
| !test/scripts/**/*.mjs | ||
| cjs | ||
| esm | ||
| .idea | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Runs inside a Node worker thread ({ type: 'module' }) to ensure the ESM | ||
| * worker bundle parses and exposes webhook exports in an isolated context. | ||
| */ | ||
| import { parentPort, workerData } from 'node:worker_threads'; | ||
| import { pathToFileURL } from 'node:url'; | ||
| import path from 'node:path'; | ||
|
|
||
| const required = [ | ||
| 'default', | ||
| 'WebhookEventType', | ||
| 'WebhookContentType', | ||
| 'basicAuthValidator', | ||
| 'WebhookError', | ||
| 'WebhookAuthenticationError', | ||
| 'WebhookPayloadValidationError', | ||
| 'WebhookPayloadParseError', | ||
| ]; | ||
|
|
||
| try { | ||
| const root = workerData.root; | ||
| const bundlePath = path.join(root, 'esm/chargebee.esm.worker.js'); | ||
| const mod = await import(pathToFileURL(bundlePath).href); | ||
| const missing = required.filter((k) => mod[k] === undefined); | ||
| if (missing.length) { | ||
| parentPort.postMessage({ ok: false, error: 'missing exports', missing }); | ||
| } else { | ||
| parentPort.postMessage({ ok: true }); | ||
| } | ||
| } catch (err) { | ||
| parentPort.postMessage({ | ||
| ok: false, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| stack: err instanceof Error ? err.stack : undefined, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { expect } from 'chai'; | ||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
| import { Worker } from 'node:worker_threads'; | ||
|
|
||
| /** `type` is supported at runtime for ESM workers; widen options until typings always include it. */ | ||
| type WorkerThreadOptions = NonNullable<ConstructorParameters<typeof Worker>[1]> & { | ||
| type?: 'module' | 'classic'; | ||
| }; | ||
|
|
||
| const testDir = path.resolve(process.cwd(), 'test'); | ||
| const repoRoot = path.join(testDir, '..'); | ||
| const esmWorkerPath = path.join(repoRoot, 'esm/chargebee.esm.worker.js'); | ||
|
|
||
| const hasBuiltEsmWorker = fs.existsSync(esmWorkerPath); | ||
|
|
||
| function describeIfEsmBuilt(title: string, fn: () => void): void { | ||
| if (hasBuiltEsmWorker) { | ||
| describe(title, fn); | ||
| return; | ||
| } | ||
| describe.skip( | ||
| `${title} (skipped: run \`npm run build\` or \`npm run test:worker-bundle\`)`, | ||
| fn, | ||
| ); | ||
| } | ||
|
|
||
| const REQUIRED_VALUE_EXPORTS = [ | ||
| 'WebhookEventType', | ||
| 'WebhookContentType', | ||
| 'basicAuthValidator', | ||
| 'WebhookError', | ||
| 'WebhookAuthenticationError', | ||
| 'WebhookPayloadValidationError', | ||
| 'WebhookPayloadParseError', | ||
| ] as const; | ||
|
|
||
| function assertWebhookExports(mod: Record<string, unknown>, label: string): void { | ||
| for (const name of REQUIRED_VALUE_EXPORTS) { | ||
| expect(mod, `${label} must export ${name}`).to.have.property(name); | ||
| expect(mod[name], `${label}.${name}`).to.not.equal(undefined); | ||
| } | ||
|
|
||
| expect(mod.default, `${label} default export`).to.be.a('function'); | ||
| expect(mod.WebhookEventType, `${label}.WebhookEventType`).to.be.an('object'); | ||
| expect(mod.WebhookContentType, `${label}.WebhookContentType`).to.equal(mod.WebhookEventType); | ||
| expect(mod.basicAuthValidator, `${label}.basicAuthValidator`).to.be.a('function'); | ||
|
|
||
| const Err = mod.WebhookError as typeof Error; | ||
| expect(new Err('x')).to.be.instanceof(Error); | ||
| expect((new Err('x') as Error).name).to.equal('WebhookError'); | ||
|
|
||
| const AuthErr = mod.WebhookAuthenticationError as typeof Error; | ||
| expect(new AuthErr('a')).to.be.instanceof(Err); | ||
|
|
||
| const ValErr = mod.WebhookPayloadValidationError as typeof Error; | ||
| expect(new ValErr('v')).to.be.instanceof(Err); | ||
|
|
||
| const ParseErr = mod.WebhookPayloadParseError as typeof Error; | ||
| expect(new ParseErr('p')).to.be.instanceof(Err); | ||
| } | ||
|
|
||
| describeIfEsmBuilt('Worker entry bundle (ESM)', () => { | ||
| it('exposes webhook value exports from built ESM worker entry', async function () { | ||
| try { | ||
| const mod = (await import(pathToFileURL(esmWorkerPath).href)) as Record<string, unknown>; | ||
| assertWebhookExports(mod, 'esm/chargebee.esm.worker.js'); | ||
| } catch (error) { | ||
| const mod = require(esmWorkerPath); | ||
| assertWebhookExports(mod, 'esm/chargebee.esm.worker.js'); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describeIfEsmBuilt('Worker thread can load ESM worker bundle', () => { | ||
| it('parses the bundle and receives all webhook exports inside a worker', function (done) { | ||
| const fixture = path.join(testDir, 'fixtures', 'load-esm-worker-bundle.mjs'); | ||
| const workerOptions: WorkerThreadOptions = { | ||
| workerData: { root: repoRoot }, | ||
| type: 'module', | ||
| }; | ||
| const worker = new Worker(fixture, workerOptions); | ||
| let settled = false; | ||
| const finish = (err?: Error): void => { | ||
| if (settled) { | ||
| return; | ||
| } | ||
| settled = true; | ||
| void worker.terminate().finally(() => { | ||
| if (err) { | ||
| done(err); | ||
| } else { | ||
| done(); | ||
| } | ||
| }); | ||
| }; | ||
| worker.on('message', (msg: { ok: boolean; error?: string; missing?: string[]; stack?: string }) => { | ||
| try { | ||
| expect(msg.ok, JSON.stringify(msg)).to.be.true; | ||
| finish(); | ||
| } catch (e) { | ||
| finish(e as Error); | ||
| } | ||
| }); | ||
| worker.on('error', finish); | ||
| worker.on('exit', (code) => { | ||
| if (settled) { | ||
| return; | ||
| } | ||
| if (code !== 0) { | ||
| finish(new Error(`worker exited with code ${code}`)); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| "rootDir": ".", | ||
| "module": "ES2022", | ||
| "moduleResolution": "node", | ||
| "target": "ES2022", | ||
| "types": ["node", "mocha"], | ||
| "esModuleInterop": true, | ||
| "allowSyntheticDefaultImports": true | ||
| }, | ||
| "include": ["src/**/*.ts", "test/**/*.ts"], | ||
| "ts-node": { | ||
| "experimentalResolver": true, | ||
| "compilerOptions": { | ||
| "module": "CommonJS", | ||
| "moduleResolution": "node" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Priority: 🟠 HIGH
Problem: The ESM worker thread is constructed with a filesystem path string while using
type: 'module', instead of a file URL.Why: In Node.js, when
type: 'module'is used forWorker, the first argument must be a file URL (orURLobject). Passing a plain filesystem path can cause the worker to fail to load the module, especially on Windows (where paths likeC:\...are not valid URLs), leading to test failures that are platform-dependent.How to Fix: Convert the
fixturefilesystem path to a file URL when creating theWorker, using the already-importedpathToFileURLhelper.