Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
arpowers committed Feb 12, 2022
1 parent db53e54 commit fd3d374
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
13 changes: 8 additions & 5 deletions @apps/site/test/site.test.ts
@@ -1,11 +1,14 @@
import { expect, it, describe } from "vitest"
import { execaCommandSync, execaCommand, ExecaChildProcess } from "execa"
import { chromium } from "playwright"
import { randomBetween } from "@factor/api"
const serverPort = randomBetween(1000, 9000)
const appPort = randomBetween(1000, 9000)

describe("build tests", () => {
it("prerenders", () => {
const r = execaCommandSync(
"npm exec -w @factor/site -- factor prerender --port 3434",
`npm exec -w @factor/site -- factor prerender --port ${serverPort}`,
{
env: { TEST_ENV: "unit" },
timeout: 30_000,
Expand All @@ -17,16 +20,16 @@ describe("build tests", () => {

it("runs dev", () => {
const r = execaCommandSync(
"npm exec -w @factor/site -- factor rdev --exit --port 1234 --port-app 2345",
`npm exec -w @factor/site -- factor rdev --exit --port ${serverPort} --port-app ${appPort}`,
{
env: { TEST_ENV: "unit" },
timeout: 20_000,
},
)

expect(r.stdout).toContain("build variables")
expect(r.stdout).toContain("1234")
expect(r.stdout).toContain("2345")
expect(r.stdout).toContain(serverPort)
expect(r.stdout).toContain(appPort)
expect(r.stdout).toContain("serving app")
})

Expand All @@ -35,7 +38,7 @@ describe("build tests", () => {

await new Promise<void>((resolve) => {
_process = execaCommand(
"npm exec -w @factor/site -- factor rdev --port 1234 --port-app 2345",
`npm exec -w @factor/site -- factor rdev --port ${serverPort} --port-app ${appPort}`,
{
env: { TEST_ENV: "unit" },
},
Expand Down
5 changes: 4 additions & 1 deletion @core/api/utils.ts
Expand Up @@ -110,6 +110,9 @@ export const objectId = (idLength = 16): string => {
" ".repeat(idLength).replace(/./g, () => nts(Math.random() * idLength))
)
}
export const randomBetween = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min + 1) + min)
}
/**
* Ensure there is a slash at end of string
* @param path
Expand All @@ -119,7 +122,7 @@ export const ensureTrailingSlash = (path: string): string => {
return path
}
// Sort objects in an array by a priority value that defaults to 100
export const sortPriority = <TPri extends any[]>(arr: TPri): TPri => {
export const sortPriority = <T extends { priority?: number }[]>(arr: T): T => {
if (!arr || arr.length === 0) return arr

return arr.sort((a, b) => {
Expand Down
1 change: 1 addition & 0 deletions @core/test/package.json
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"dependencies": {
"@factor/site": "4.0.24",
"@factor/api": "4.0.24",
"@factor/engine": "4.0.24",
"@factor/render": "4.0.24",
"@factor/server": "4.0.24",
Expand Down
7 changes: 2 additions & 5 deletions @core/test/setupTest.ts
@@ -1,17 +1,14 @@
import { beforeAll } from "vitest"
import { setup } from "@factor/server"

const randomIntFromInterval = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min + 1) + min)
}
import { randomBetween } from "@factor/api"

beforeAll(async () => {
process.env.POSTGRES_URL = "http://test:test@localhost:5432/test"
process.env.POSTGRES_PASSWORD = "test"
process.env.TEST_ENV = "unit"
process.env.FACTOR_TOKEN_SECRET = "test"

process.env.PORT = String(randomIntFromInterval(1000, 10_000))
process.env.PORT = String(randomBetween(1000, 10_000))

await setup({ moduleName: "@factor/site" })
})

0 comments on commit fd3d374

Please sign in to comment.