使用 jest-puppeteer 在 GitHub Actions 中构建和启动 2 个应用程序会导致读取环境变量时出现问题
Posted
技术标签:
【中文标题】使用 jest-puppeteer 在 GitHub Actions 中构建和启动 2 个应用程序会导致读取环境变量时出现问题【英文标题】:Building and starting 2 apps in GitHub Actions using jest-puppeteer causes issue reading environment variables 【发布时间】:2022-01-18 15:49:57 【问题描述】:我有一个带有单独 Express API 的 Next.js 应用。 我想在 Github Actions 中使用 Jest 和 puppeteer 运行 e2e 测试,以自动构建和运行这两个应用程序,然后对其进行测试。
仅构建、运行和测试 Next.js 应用程序效果很好,但是当我尝试在构建和启动 Express api 时运行相同的测试时,它似乎无法读取环境变量。 (在GitHub中正确设置为secret)
Next.js 应用程序位于 ./client/ Express api 位于 ./server/
我得到的错误:
Run npm ci
npm ci
npm run build
cd ../client/
npm ci
npm run build
npm run test
shell: /usr/bin/bash -e 0
npm WARN deprecated formidable@1.2.6: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes:
npm WARN deprecated @types/mongoose@5.11.97: Mongoose publishes its own types, so you do not need to install this package.
added 720 packages, and audited 721 packages in 14s
90 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
> server@1.0.0 build
> tsc
npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated jest-dom@4.0.0: ???? jest-dom has moved to @testing-library/jest-dom. Please uninstall jest-dom and install @testing-library/jest-dom instead, or use an older version of jest-dom. Learn more about this change here: https://github.com/testing-library/dom-testing-library/issues/260 Thanks! :)
added 1153 packages, and audited 1154 packages in 23s
118 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
> client@1.0.0 build
> next build
warn - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry
info - Checking validity of types...
info - Creating an optimized production build...
warn - You have enabled the JIT engine which is currently in preview.
warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.
info - Compiled successfully
info - Collecting page data...
info - Generating static pages (0/3)
info - Generating static pages (3/3)
info - Finalizing page optimization...
Page Size First Load JS
┌ ○ / 2.65 kB 94.6 kB
├ /_app 0 B 79.4 kB
├ ○ /404 2.33 kB 94.3 kB
├ λ /api/auth/[...nextauth] 0 B 79.4 kB
├ λ /api/city 0 B 79.4 kB
├ λ /api/file 0 B 79.4 kB
├ λ /api/file/[imageName] 0 B 79.4 kB
├ λ /api/neighborhood 0 B 79.4 kB
├ λ /artikelen 3.29 kB 104 kB
├ λ /artikelen/[...id] 2.46 kB 94.4 kB
├ λ /artikelen/[id]/bewerk 3.51 kB 112 kB
├ λ /artikelen/nieuw 3.26 kB 112 kB
├ λ /auth/signin 2.34 kB 94.3 kB
└ λ /profiel 12.2 kB 115 kB
+ First Load JS shared by all 79.4 kB
├ chunks/framework-8957c350a55da097.js 42.2 kB
├ chunks/main-d98b4a7f39fdfc80.js 28.2 kB
├ chunks/pages/_app-f1a70da55988a293.js 6.99 kB
├ chunks/webpack-1107c7d10389002b.js 1.97 kB
└ css/c71746929b818144.css 5.8 kB
λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
○ (Static) automatically rendered as static html (uses no initial props)
> client@1.0.0 test
> jest test
/home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/node_modules/mongoose/lib/connection.js:684
throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
^
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
at NativeConnection.Connection.openUri (/home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/node_modules/mongoose/lib/connection.js:684:11)
at /home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/node_modules/mongoose/lib/index.js:332:10
at /home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (/home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/node_modules/mongoose/lib/index.js:1153:10)
at Mongoose.connect (/home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/node_modules/mongoose/lib/index.js:331:20)
at Object.<anonymous> (/home/runner/work/sep2021-project-numbat/sep2021-project-numbat/server/dist/index.js:20:20)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
Error: Process completed with exit code 1.
这是我的配置文件。
jest.config.js
module.exports =
preset: 'jest-puppeteer',
collectCoverageFrom: [
'**/*.js,jsx,ts,tsx',
'!**/*.d.ts',
'!**/node_modules/**',
],
moduleNameMapper:
/* Handle CSS imports (with CSS modules)
https://jestjs.io/docs/webpack#mocking-css-modules */
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
// Handle CSS imports (without CSS modules)
'^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',
/* Handle image imports
https://jestjs.io/docs/webpack#handling-static-assets */
'^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$':
'<rootDir>/__mocks__/fileMock.js',
,
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
// testEnvironment: 'jsdom',
transform:
/* Use babel-jest to transpile tests with the next/babel preset
https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object */
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', presets: ['next/babel'] ],
,
transformIgnorePatterns: [
'/node_modules/',
'^.+\\.module\\.(css|sass|scss)$',
],
;
jest-puppeteer.config.js
module.exports =
launch:
headless: true,
ignoreDefaultArgs: ['--disable-extensions'],
args: ['--no-sandbox'],
,
server: [
command: 'npm run start',
launchTimeout: 180000,
,
command: 'cd ../server && node dist/index.js',
launchTimeout: 180000,
,
],
;
这是我的 Github Action
name: ci
on:
push:
branches: [ main ]
pull_request:
branches: [ main, 'feature/*' ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '16.x' ]
os: [ ubuntu-latest ]
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: $ matrix.node-version
- name: e2e test
working-directory: ./server/
run: |
npm ci
npm run build
cd ../client/
npm ci
npm run build
npm run test
npm 运行测试命令
"test": "jest test"
是否可以在 GitHub 操作中运行 2 个这样的应用程序? 如果没有,还有什么办法?
【问题讨论】:
如果您将uri
配置为机密并且您想将其用作环境变量,请确保将其公开为:env: MY_URI: $ secrets.MY_URI
。
【参考方案1】:
我忘记在 Github 操作文件中添加环境变量。
name: ci
on:
push:
branches: [ main ]
pull_request:
branches: [ main, 'feature/*' ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '16.x' ]
os: [ ubuntu-latest ]
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: $ matrix.node-version
- name: e2e test
env:
ATLAS_URI: $ secrets.ATLAS_URI
working-directory: ./server/
run: |
npm ci
npm run build
cd ../client/
npm ci
npm run build
npm run test
【讨论】:
以上是关于使用 jest-puppeteer 在 GitHub Actions 中构建和启动 2 个应用程序会导致读取环境变量时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
sh 使用静态libressl,zlib和pcre的nginx构建脚本。灵感来自https://gist.github.com/nlindblad/9709182,https://gist.githu
beforeEach 和 beforeAll 以啥顺序执行?
javascript UV Index Monitor App订阅PubNub并显示UV索引值。博文的代码片段。在这里查看项目:https:// githu