-
Notifications
You must be signed in to change notification settings - Fork 426
test(node): restore test-node as a real Node-runtime suite #173
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
dmythro
wants to merge
1
commit into
main
Choose a base branch
from
test/test-node-runtime
base: main
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,73 +1,55 @@ | ||
| // biome-ignore-all lint/performance/noDynamicNamespaceImportAccess: test file | ||
|
|
||
| import { describe, expect, test } from 'bun:test' | ||
| import fs from 'node:fs' | ||
| import path from 'node:path' | ||
| // Validates the published bundles under the real Node.js runtime (CommonJS + ESM | ||
| // loaders), which the Bun-run test-js suite does not exercise. Imports only built | ||
| // dist artifacts — never the TS source — so it mirrors what a Node consumer resolves. | ||
|
|
||
| import * as mjs from '../../dist/mjs/index.js' | ||
| import * as source from '../countries/src/index.ts' | ||
| import assert from 'node:assert/strict' | ||
| import { createRequire } from 'node:module' | ||
| import { describe, test } from 'node:test' | ||
|
|
||
| const distDir = path.resolve(import.meta.dir, '../../dist') | ||
| const require = createRequire(import.meta.url) | ||
|
|
||
| const exportDataList: Partial<keyof typeof source>[] = ['continents', 'countries', 'languages'] | ||
| const exportFnList: Partial<keyof typeof source>[] = ['getEmojiFlag'] | ||
| const expectedFns = ['getCountryCode', 'getCountryData', 'getCountryDataList', 'getEmojiFlag'] | ||
| const expectedData = ['continents', 'countries', 'languages'] | ||
|
|
||
| function evalIIFE(name = 'Countries') { | ||
| const script = fs.readFileSync(path.join(distDir, 'index.iife.js'), { encoding: 'utf-8' }) | ||
| // biome-ignore lint/security/noGlobalEval: - | ||
| eval(`this.${name} = (function () { ${script}\nreturn ${name}})()`) | ||
| } | ||
| describe('dist under Node.js', () => { | ||
| test('CJS main bundle loads via require()', () => { | ||
| const cjs = require('../../dist/cjs/index.js') | ||
|
|
||
|
dmythro marked this conversation as resolved.
|
||
| describe('dist', () => { | ||
| test('has proper CJS ES6 export', () => { | ||
| expect(typeof mjs).toBe('object') | ||
|
|
||
| for (const prop of exportFnList) { | ||
| expect(Object.hasOwn(mjs, prop)).toBe(true) | ||
| for (const fn of expectedFns) { | ||
| assert.equal(typeof cjs[fn], 'function', `missing function: ${fn}`) | ||
| } | ||
|
|
||
| for (const prop of exportDataList) { | ||
| expect(Object.hasOwn(mjs, prop)).toBe(true) | ||
|
|
||
| const cjsKeys = Object.keys(mjs[prop]) | ||
| const srcKeys = Object.keys(source[prop]) | ||
| expect(cjsKeys).toEqual(srcKeys) | ||
| for (const key of expectedData) { | ||
| assert.equal(typeof cjs[key], 'object', `missing data: ${key}`) | ||
| } | ||
| assert.equal(cjs.getEmojiFlag('UA'), '🇺🇦') | ||
| }) | ||
|
|
||
| test('all English country names should contain only A-Z characters', () => { | ||
| const nameReg = /^[a-z\s.()-]+$/i | ||
| const nonAZ: string[] = [] | ||
| for (const c of Object.values(source.countries)) { | ||
| if (!nameReg.test(c.name)) { | ||
| nonAZ.push(c.name) | ||
| } | ||
| } | ||
| test('ESM main bundle loads via import()', async () => { | ||
| const mjs = await import('../../dist/mjs/index.js') | ||
|
|
||
|
dmythro marked this conversation as resolved.
|
||
| // It helps see incorrect names right away in logs | ||
| expect(nonAZ).toEqual([]) | ||
| for (const fn of expectedFns) { | ||
| assert.equal(typeof mjs[fn], 'function', `missing function: ${fn}`) | ||
| } | ||
| for (const key of expectedData) { | ||
| assert.equal(typeof mjs[key], 'object', `missing data: ${key}`) | ||
| } | ||
| assert.equal(mjs.getEmojiFlag('UA'), '🇺🇦') | ||
| }) | ||
|
|
||
| test('loads ES6 <script> properly', () => { | ||
| const context: { Countries?: typeof source } = {} | ||
| evalIIFE.call(context) | ||
| test('CJS currencies subpath loads via require()', () => { | ||
| const cjs = require('../../dist/cjs/currencies.js') | ||
|
|
||
|
dmythro marked this conversation as resolved.
|
||
| const contextCountries = context.Countries | ||
|
|
||
| expect(contextCountries).toBeTruthy() | ||
| expect(contextCountries).toBeObject() | ||
|
|
||
| for (const prop of exportFnList) { | ||
| expect(contextCountries).toHaveProperty(prop) | ||
| } | ||
| assert.equal(typeof cjs.getCurrency, 'function') | ||
| assert.equal(typeof cjs.getCurrencyByNumeric, 'function') | ||
| assert.equal(cjs.getCurrency('UAH').numeric, '980') | ||
| }) | ||
|
|
||
| for (const prop of exportDataList) { | ||
| expect(contextCountries).toHaveProperty(prop) | ||
| test('ESM currencies subpath loads via import()', async () => { | ||
| const mjs = await import('../../dist/mjs/currencies.js') | ||
|
|
||
|
dmythro marked this conversation as resolved.
|
||
| // biome-ignore lint/style/noNonNullAssertion: - | ||
| const windowProps = Object.keys(contextCountries![prop]) as string[] | ||
| const dataProps = Object.keys(source[prop]) as string[] | ||
| expect(windowProps).toEqual(dataProps) | ||
| } | ||
| assert.equal(typeof mjs.getCurrency, 'function') | ||
| assert.equal(mjs.getCurrencyByNumeric('840')?.code, 'USD') | ||
| }) | ||
| }) | ||
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,8 @@ | ||
| { | ||
| "name": "test-node", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "node --test ./*.test.ts" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.