Skip to content

Commit

Permalink
Align ts tests with production bundle (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Oct 9, 2024
1 parent ee2994a commit 9d50a69
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 29 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ jobs:
name: rust-test-results
path: test-results/rust-test-results.xml

test-typescript:
runs-on: ubuntu-latest
typescript:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- name: Init Hermit
Expand All @@ -432,5 +432,6 @@ jobs:
cd bound/typescript
npm install
npm run clean
npm run build:wasm
npm run test:node
npm run build
npm run test:node:cjs
npm run test:node:esm
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ test-typescript: setup
cd bound/typescript
npm install
npm run clean
npm run build:wasm
npm run build
npm run test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"enable-source-maps": true,
"exit": true,
"spec": ["tests/compiled/node/**/*.test.js"]
"spec": ["tests/compiled/cjs/**/*.test.js"]
}
5 changes: 5 additions & 0 deletions bound/typescript/.mocharc.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"enable-source-maps": true,
"exit": true,
"spec": ["tests/compiled/esm/**/*.test.js"]
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import esbuild from "esbuild";

const browserConfig = {
esbuild.build({
entryPoints : ["./src/index.ts"],
bundle : true,
format : "esm",
sourcemap : true,
minify : true,
platform : "browser",
target : ["chrome101", "firefox108", "safari16"],
define : {
"global": "globalThis",
},
};

// esm polyfilled bundle for browser
esbuild.build({
...browserConfig,
outfile: "dist/browser.mjs",
});

// iife polyfilled bundle for browser
esbuild.build({
...browserConfig,
format : "iife",
globalName : "tbDEX",
outfile : "dist/browser.js",
Expand Down
15 changes: 15 additions & 0 deletions bound/typescript/bundle-browser-esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import esbuild from "esbuild";

esbuild.build({
entryPoints : ["./src/index.ts"],
bundle : true,
format : "esm",
sourcemap : true,
minify : true,
platform : "browser",
target : ["chrome101", "firefox108", "safari16"],
define : {
"global": "globalThis",
},
outfile: "dist/browser.mjs",
});
File renamed without changes.
File renamed without changes.
17 changes: 10 additions & 7 deletions bound/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@
},
"scripts": {
"clean": "rimraf pkg tests/compiled dist",
"build:browser": "node bundle-browser.js",
"build:cjs": "node bundle-cjs.js",
"build:esm": "node bundle-esm.js",
"build:browser:cjs": "node bundle-browser-cjs.js",
"build:browser:esm": "node bundle-browser-esm.js",
"build:node:cjs": "node bundle-nodejs-cjs.js",
"build:node:esm": "node bundle-nodejs-esm.js",
"build:types": "tsc --declaration --emitDeclarationOnly --outFile dist/index.d.ts && ./declaration-hack.sh",
"build:wasm": "./bundle-wasm/build.sh && node ./bundle-wasm/bundle.js",
"build": "npm run build:wasm && npm run build:types && npm run build:browser && npm run build:esm && npm run build:cjs",
"test:node": "tsc -p tests/tsconfig.json && node tests/bundle-node.js && mocha",
"test:browser": "tsc -p tests/tsconfig.json && node tests/bundle-browser.js && web-test-runner",
"test": "npm run test:node && npm run test:browser"
"build": "npm run build:wasm && npm run build:types && npm run build:browser:cjs && npm run build:browser:esm && npm run build:node:esm && npm run build:node:cjs",
"test:node:cjs": "tsc -p tests/tsconfig.json && node tests/bundle-nodejs-cjs.js && mocha --config .mocharc.cjs.json",
"test:node:esm": "tsc -p tests/tsconfig.json && node tests/bundle-nodejs-esm.js && mocha --config .mocharc.esm.json",
"test:browser:cjs": "tsc -p tests/tsconfig.json && node tests/bundle-browser-cjs.js && web-test-runner --config web-test-runner.cjs.config.mjs",
"test:browser:esm": "tsc -p tests/tsconfig.json && node tests/bundle-browser-esm.js && web-test-runner --config web-test-runner.esm.config.mjs",
"test": "npm run test:node:cjs && npm run test:node:esm && npm run test:browser:cjs && npm run test:browser:esm"
},
"dependencies": {}
}
24 changes: 24 additions & 0 deletions bound/typescript/tests/bundle-browser-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fileURLToPath } from 'node:url'

import esbuild from 'esbuild'
import path from 'node:path'
import fs from 'node:fs'

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

fs.copyFileSync(`${__dirname}/../src/wasm/generated.d.ts`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.d.ts`)
fs.copyFileSync(`${__dirname}/../src/wasm/generated.js`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.js`)

esbuild.buildSync({
entryPoints: [`${__dirname}/compiled/bound/typescript/tests/**/*.test.js`],
format : 'cjs',
bundle : true,
sourcemap : true,
platform : 'browser',
target : ['chrome101', 'firefox108', 'safari16'],
outdir : `${__dirname}/compiled/browser/cjs`,
define : {
'global': 'globalThis',
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ esbuild.buildSync({
sourcemap : true,
platform : 'browser',
target : ['chrome101', 'firefox108', 'safari16'],
outdir : `${__dirname}/compiled/browser`,
outdir : `${__dirname}/compiled/browser/esm`,
define : {
'global': 'globalThis',
},
Expand Down
20 changes: 20 additions & 0 deletions bound/typescript/tests/bundle-nodejs-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { fileURLToPath } from 'node:url'
import esbuild from 'esbuild'
import path from 'node:path'
import fs from 'node:fs'

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

fs.copyFileSync(`${__dirname}/../src/wasm/generated.d.ts`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.d.ts`)
fs.copyFileSync(`${__dirname}/../src/wasm/generated.js`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.js`)

esbuild.buildSync({
entryPoints: [`${__dirname}/compiled/bound/typescript/tests/**/*.test.js`],
format: 'cjs',
bundle: true,
sourcemap: true,
platform: 'node',
target: ['node18'],
outdir: `${__dirname}/compiled/cjs`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ esbuild.buildSync({
sourcemap: true,
platform: 'node',
target: ['node18'],
outdir: `${__dirname}/compiled/node`,
outdir: `${__dirname}/compiled/esm`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { playwrightLauncher } from '@web/test-runner-playwright'
* @type {import('@web/test-runner').TestRunnerConfig}
*/
export default {
files : ['./tests/compiled/browser/**/*.test.js'],
files : ['./tests/compiled/browser/cjs/**/*.test.js'],
playwright : true,
browsers : [
playwrightLauncher({
Expand Down
27 changes: 27 additions & 0 deletions bound/typescript/web-test-runner.esm.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { playwrightLauncher } from '@web/test-runner-playwright'

/**
* @type {import('@web/test-runner').TestRunnerConfig}
*/
export default {
files : ['./tests/compiled/browser/esm/**/*.test.js'],
playwright : true,
browsers : [
playwrightLauncher({
product: 'chromium',
}),
playwrightLauncher({
product: 'firefox',
}),
playwrightLauncher({
product: 'webkit',
}),
],
testsFinishTimeout : 300000,
concurrentBrowsers : 1,
testFramework : {
config: {
timeout: '30000',
},
}
};

0 comments on commit 9d50a69

Please sign in to comment.