-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IIFE build and browser/node testing framework
This configures an IIFE build that can be used to pull in the SDK library using a script tag. It also sets up AVA as a test runner with three different configurations: - `test:dev` - Run the `test/lib/` tests from `src/lib/` without an explicit build step. - `test:node` - Run the `test/lib/` tests from `dist/lib/` requiring an explicit build. - `test:browser` - Run the `test/browser/` tests in Chromium using `dist/lib/browser/sindri.iife.js` requiring an explicit build. I don't think there's really a way to run the same test suite in both the browser and node environments, so my plan is to do most of the testing in node and have a basic set of sanity check tests that run in puppeteer to make sure that the browser builds don't accidentally depend on node-only libraries or lack any polyfills. Merges #41
- Loading branch information
Showing
17 changed files
with
1,799 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 @@ | ||
nodejs v18.18.2 |
This file contains 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 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 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 +1,7 @@ | ||
console.log("SDK"); | ||
import { InternalService } from "lib/api"; | ||
|
||
export default { | ||
environment: process.env.NODE_ENV, | ||
getSindriManifestSchema: async () => | ||
await InternalService.sindriManifestSchema(), | ||
}; |
This file contains 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,7 @@ | ||
export default { | ||
extensions: { | ||
ts: "module", | ||
}, | ||
files: ["test/**/*.test.ts"], | ||
nodeArguments: ["--loader=tsx"], | ||
}; |
This file contains 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,15 @@ | ||
import path from "path"; | ||
import process from "process"; | ||
import { fileURLToPath } from "url"; | ||
|
||
import baseConfig from "./ava.config.base.mjs"; | ||
|
||
// Use the browser tsconfig import paths. | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
process.env.TSX_TSCONFIG_PATH = path.join(__dirname, "tsconfig.browser.json"); | ||
|
||
export default { | ||
...baseConfig, | ||
files: ["test/browser/**/*.test.ts"], | ||
}; |
This file contains 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,6 @@ | ||
import baseConfig from "./ava.config.base.mjs"; | ||
|
||
export default { | ||
...baseConfig, | ||
files: ["test/lib/**/*.test.ts"], | ||
}; |
This file contains 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,15 @@ | ||
import path from "path"; | ||
import process from "process"; | ||
import { fileURLToPath } from "url"; | ||
|
||
import baseConfig from "./ava.config.base.mjs"; | ||
|
||
// Use the node tsconfig import paths. | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
process.env.TSX_TSCONFIG_PATH = path.join(__dirname, "tsconfig.node.json"); | ||
|
||
export default { | ||
...baseConfig, | ||
files: ["test/lib/**/*.test.ts"], | ||
}; |
This file contains 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,15 @@ | ||
import test from "ava"; | ||
import withPage from "./withPage"; | ||
|
||
import sindriLibrary from "lib"; | ||
|
||
// The `sindri` library is injected in `withPage.ts`, but this tells TypeScript what the type is. | ||
type SindriLibrary = typeof sindriLibrary; | ||
declare const sindri: SindriLibrary; | ||
|
||
test("should get Sindri manifest schema JSON", withPage, async (t, page) => { | ||
const schema = await page.evaluate(async () => | ||
sindri.getSindriManifestSchema(), | ||
); | ||
t.true(schema?.title?.includes("Sindri")); | ||
}); |
This file contains 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,40 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
import { ExecutionContext } from "ava"; | ||
import puppeteer, { Page } from "puppeteer"; | ||
|
||
type RunFunction = (t: ExecutionContext, page: Page) => Promise<void>; | ||
|
||
const getSindriScriptPath = () => { | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const sindriScriptPath = path.join( | ||
__dirname, | ||
"..", | ||
"..", | ||
"dist", | ||
"lib", | ||
"browser", | ||
"sindri.iife.js", | ||
); | ||
if (!fs.existsSync(sindriScriptPath)) { | ||
throw new Error(`Expected IIFE build to exist at "${sindriScriptPath}".`); | ||
} | ||
return sindriScriptPath; | ||
}; | ||
|
||
export default async (t: ExecutionContext, run: RunFunction) => { | ||
const browser = await puppeteer.launch({ headless: "new" }); | ||
const page = await browser.newPage(); | ||
try { | ||
await page.addScriptTag({ | ||
path: getSindriScriptPath(), | ||
}); | ||
await run(t, page); | ||
} finally { | ||
await page.close(); | ||
await browser.close(); | ||
} | ||
}; |
This file contains 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,14 @@ | ||
import test from "ava"; | ||
|
||
import lib from "lib"; | ||
|
||
test("should get Sindri manifest schema JSON", async (t) => { | ||
const schema = await lib.getSindriManifestSchema(); | ||
t.true(schema?.title?.includes("Sindri")); | ||
}); | ||
|
||
test("library can be imported", (t) => { | ||
t.true( | ||
lib.environment && ["development", "production"].includes(lib.environment), | ||
); | ||
}); |
This file contains 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,9 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"paths": { | ||
"lib": ["../dist/lib/browser"], | ||
"lib/*": ["../dist/lib/browser/*"] | ||
} | ||
} | ||
} |
This file contains 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,9 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"paths": { | ||
"lib": ["../dist/lib"], | ||
"lib/*": ["../dist/lib/*"] | ||
} | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.