Skip to content

Commit

Permalink
chore(jobs): Add e2e test script and CI job (#11348)
Browse files Browse the repository at this point in the history
Adds a test script which will do an end-to-end style test of the redwood
jobs feature. It takes a test project, sets up up jobs, generates a job,
schedules a job, and executes the job.

It can be run locally with just one command. It has been added to CI
too.

---------

Co-authored-by: Rob Cameron <cannikin@fastmail.com>
  • Loading branch information
Josh-Walker-GM and cannikin authored Aug 23, 2024
1 parent 974a245 commit a13b44f
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -901,3 +901,50 @@ jobs:

steps:
- run: echo "Skipped"

background-jobs-e2e:
needs: check

strategy:
matrix:
os: [ubuntu-latest, windows-latest]

name: Background jobs E2E test / ${{ matrix.os }}
runs-on: ${{ matrix.os }}

env:
REDWOOD_CI: 1
REDWOOD_VERBOSE_TELEMETRY: 1

steps:
- uses: actions/checkout@v4
- name: Set up job
uses: ./.github/actions/set-up-job

- name: 🌲 Set up test project
id: set-up-test-project
uses: ./.github/actions/set-up-test-project
with:
canary: true
env:
REDWOOD_DISABLE_TELEMETRY: 1
YARN_ENABLE_IMMUTABLE_INSTALLS: false

- name: Run E2E test script
run: yarn e2e:background-jobs ${{ steps.set-up-test-project.outputs.test-project-path }}
env:
REDWOOD_VERBOSE_TELEMETRY: ''

background-jobs-e2e-skip:
needs: detect-changes
if: needs.detect-changes.outputs.code == 'false'

strategy:
matrix:
os: [ubuntu-latest, windows-latest]

name: Background jobs E2E test / ${{ matrix.os }}
runs-on: ${{ matrix.os }}

steps:
- run: echo "Skipped"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"check:package": "nx run-many -t check:package --output-style static",
"clean:prisma": "rimraf node_modules/.prisma/client && node node_modules/@prisma/client/scripts/postinstall.js",
"e2e": "node ./tasks/run-e2e",
"e2e:background-jobs": "tsx ./tasks/e2e-background-jobs/run.mts",
"format": "prettier . --write",
"format:check": "prettier . --check",
"generate-dependency-graph": "node ./tasks/generateDependencyGraph.mjs",
Expand Down
19 changes: 19 additions & 0 deletions tasks/e2e-background-jobs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# E2E Background jobs

This script runs an E2E style test against the Redwood Jobs feature. Testing that jobs can be setup, generated, scheduled and executed.

## Usage

You can run this locally by creating a test project:

```bash
yarn build:test-project ../rw-test-project
```

Then you can execute the script like so:

```bash
yarn e2e:background-jobs ../rw-test-project
```

You will likely find it helpful to set up git in the test project so you can easily rollback the project to the default state if you need to rerun the test multiple times.
53 changes: 53 additions & 0 deletions tasks/e2e-background-jobs/fixtures.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export const SAMPLE_JOB_PERFORM_ARGS = `async (location: string, data: string)`

export const SAMPLE_JOB_PERFORM_BODY = `
const { default: fs } = await import('node:fs')
fs.writeFileSync(location, data)
`

export const SAMPLE_FUNCTION = `
import type { APIGatewayEvent, Context } from 'aws-lambda'
import { SampleJob } from 'src/jobs/SampleJob/SampleJob'
import { later } from 'src/lib/jobs'
export const handler = async (event: APIGatewayEvent, _context: Context) => {
const { location, data } = JSON.parse(event.body)
await later(SampleJob, [location as string, data as string])
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
location,
data,
}),
}
}
`

export const JOBS_SCRIPT = `
import { db } from 'api/src/lib/db'
export default async () => {
const jobs = await db.backgroundJob.findMany()
console.log(JSON.stringify(jobs))
}
`

export const PRISMA_SCRIPT = `
import { db } from 'api/src/lib/db'
export default async () => {
const model = db.backgroundJob
console.log(
JSON.stringify({
name: model.name,
})
)
}
`
Loading

0 comments on commit a13b44f

Please sign in to comment.