Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e #1549

Merged
merged 6 commits into from
Jul 3, 2023
Merged

e2e #1549

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
dist/
node_modules/
storybook-static/
test/e2e/*app
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
cache: 'yarn'
- run: yarn install
- run: yarn build-storybook
- run: yarn test
# semantic-release skips not configured branches(see: release.config.js) or pull-requests
- run: yarn release
env:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ coverage/
.idea
yarn-error.log
.size-snapshot.json
test/e2e/*app
__diff_output__
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"eslint:ci": "eslint .",
"prettier": "prettier --check .",
"prettier-fix": "prettier --write .",
"test": "npm run eslint:ci && npm run prettier",
"test": "npm run eslint:ci && (cd test/e2e; ./e2e.sh)",
"typecheck": "tsc --noEmit --emitDeclarationOnly false --strict --jsx react",
"typegen": "tsc --emitDeclarationOnly",
"storybook": "NODE_OPTIONS=\"--openssl-legacy-provider\" storybook dev -p 6006",
Expand Down Expand Up @@ -111,7 +111,7 @@
"@typescript-eslint/parser": "^5.4.0",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"copyfiles": "^2.3.0",
"copyfiles": "^2.4.1",
"eslint": "^7.7.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.22.0",
Expand All @@ -120,17 +120,20 @@
"eslint-plugin-react-hooks": "^4.1.0",
"eslint-plugin-storybook": "^0.6.12",
"husky": "^6.0.0",
"jest": "^26.4.1",
"jest": "^29.5.0",
"jest-image-snapshot": "^6.1.0",
"json": "^11.0.0",
"prettier": "^2.4.1",
"pretty-quick": "^3.1.0",
"puppeteer": "^20.7.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"rimraf": "^3.0.2",
"rollup": "^3.25.1",
"rollup-plugin-glslify": "^1.3.0",
"rollup-plugin-multi-input": "^1.4.1",
"semantic-release": "^21.0.6",
"serve": "^14.2.0",
"storybook": "^7.0.12",
"three": "^0.149.0",
"ts-node": "^10.9.1",
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Suspense, useEffect } from 'react'
import { Canvas } from '@react-three/fiber'
import { Box, Environment, CameraControls } from '@react-three/drei' // eslint-disable-line import/no-unresolved

function App() {
return (
<Canvas camera={{ position: [1, 1, 1] }} style={{ width: 300, height: 150, background: 'white' }}>
<Suspense fallback={null}>
<Scene />
</Suspense>
</Canvas>
)
}

function Scene() {
useEffect(() => {
document.dispatchEvent(new Event('puppeteer:r3f'))
}, [])

return (
<>
<Box>
<meshStandardMaterial />
</Box>

<Environment preset="city" />

<CameraControls />
</>
)
}

export default App
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions test/e2e/e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/sh
set -ex

PORT=5188

(cd ../../dist; npm pack)

kill_app() {
kill $(lsof -ti:$PORT)
}

kill_app || echo "ok, no previous running process on port $PORT"

#
# ██╗ ██╗██╗████████╗███████╗
# ██║ ██║██║╚══██╔══╝██╔════╝
# ██║ ██║██║ ██║ █████╗
# ╚██╗ ██╔╝██║ ██║ ██╔══╝
# ╚████╔╝ ██║ ██║ ███████╗
# ╚═══╝ ╚═╝ ╚═╝ ╚══════╝
#

rm -rf viteapp

# create app
npm create vite@latest viteapp -- --template react

# drei
(cd viteapp; npm i; npm i ../../../dist/react-three-drei-0.0.0-semantic-release.tgz)

# App.jsx
cp App.jsx viteapp/src/App.jsx

# build+start+jest
(cd viteapp; npm run build; npm run preview -- --port $PORT &)
npx jest snapshot.test.js || kill_app
kill_app

rm -rf viteapp

#
# ██████╗██████╗ █████╗
# ██╔════╝██╔══██╗██╔══██╗
# ██║ ██████╔╝███████║
# ██║ ██╔══██╗██╔══██║
# ╚██████╗██║ ██║██║ ██║
# ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝
#

rm -rf craapp

# create app
npx create-react-app craapp

# drei
(cd craapp; npm i ../../../dist/react-three-drei-0.0.0-semantic-release.tgz)

# App.jsx
cp App.jsx craapp/src/App.js

# build+start+jest
(cd craapp; npm run build; npx serve -s -p $PORT build &)
npx jest snapshot.test.js || kill_app
kill_app

rm -rf craapp

#
# Teardown
#

echo "✅ e2e ok"
37 changes: 37 additions & 0 deletions test/e2e/snapshot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const puppeteer = require('puppeteer')

const { toMatchImageSnapshot } = require('jest-image-snapshot')
expect.extend({ toMatchImageSnapshot })

async function waitForEvent(page, eventName) {
await page.evaluate(
(eventName) => new Promise((resolve) => document.addEventListener(eventName, resolve, { once: true })),
eventName
)
}

describe('snapshot', () => {
let browser
let page
beforeAll(async () => {
browser = await puppeteer.launch({ headless: 'new' })
page = await browser.newPage()
})

it('should match previous one', async () => {
// ⏳ "r3f" event
await page.goto('http://localhost:5188')
await waitForEvent(page, 'puppeteer:r3f')

// 📸 <canvas>
const $canvas = await page.$('canvas[data-engine]')
const image = await $canvas.screenshot()

// compare
expect(image).toMatchImageSnapshot({})
}, 30000)

afterAll(async () => {
await browser.close()
})
})
Loading