Skip to content

Commit

Permalink
Fix jest environment
Browse files Browse the repository at this point in the history
  • Loading branch information
yandeu committed Jul 18, 2023
1 parent dd1e916 commit f00a66d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"preReleaseHook": "prepareRelease",
"prepareRelease": "npm run clean && npm i && npm run build && npm run bundle && npm run denoify && npm run test && npm run test:deno && npm run test:browser",
"test": "npm run test:nodejs && npm run test:jsx-runtime:client && npm run test:jsx-runtime:server",
"test:nodejs": "npm run clean:test && tsc --project tsconfig.test.json && jest \"test/.+\\.test.js$\" && npm run clean:test",
"test:nodejs": "npm run clean:test && tsc --project tsconfig.test.json && node ./test/scripts.mjs add-jest-environment && jest \"test/.+\\.ssr.test.js$\" && npm run clean:test",
"test:jsx-runtime:client": "tsc --project tsconfig.test.jsx-runtime.json && jest --coverage=false \"test.jsx-runtime/.+\\.test.js$\"",
"test:jsx-runtime:server": "tsc --project tsconfig.test.jsx-runtime.json && node test.jsx-runtime/nodejs/server.js",
"test:browser": "rimraf .nyc_output coverage && webpack -c webpack/webpack.bundle.instrumented.js && tsc -p scripts/browserTest/tsconfig.json && node scripts/browserTest/index.mjs --coverage && npx nyc report --exclude \"src/{bundles,dev,htm,ui}/**\" --exclude \"src/mod.ts\" --reporter=text --reporter=html --reporter=lcov",
Expand Down
4 changes: 3 additions & 1 deletion src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export const renderSSR = (component: any, options: { pathname?: string; clearSta
initSSR(pathname)
if (clearState) _state.clear()

return render(component, null, true).join('') as string
const tmp = render(component, null, true) as string | string[]
if (Array.isArray(tmp)) return tmp.join('')
else return Array.from(tmp).join('')
}

export const clearState = () => {
Expand Down
31 changes: 31 additions & 0 deletions test/scripts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-check

import { readFile, readdir, writeFile } from "fs/promises"
import { resolve } from "path"

const args = process.argv.splice(2)
const arg0 = args[0]

async function* getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
yield* getFiles(res);
} else {
yield res;
}
}
}

if (arg0 === "add-jest-environment") {
const str = "/**\n* @jest-environment node\n*/\n\n"

for await (const f of getFiles("test")) {
if (/ssr\.test\.js/.test(f)) {
const file = await readFile(f, { encoding: "utf-8" })
await writeFile(f, str + file, { encoding: "utf-8" })
}
}

}

0 comments on commit f00a66d

Please sign in to comment.