运行 "jest" after 运行 "serverless offline" in github action 如何?

How to run "jest" after run "serverless offline" in github action?

我写了一个无服务器 API 和一些有趣的测试。这是我的源代码: https://github.com/liou-jia-hao/serverless-typescript-no-webpack/tree/add-dev-skipauth

我写了一个依赖本地服务器 运行ning 的测试。 这是我的测试文件:

import axios, { AxiosRequestConfig } from 'axios';
import fs from 'fs';
import path from 'path';

const API_BASE_URL = `http://localhost:${process.env.PORT || 7070}`;

describe('file', () => {
  jest.setTimeout(30000000);
  let uploadUrl: string;
  const fileId = 'testFile2xxxx';
  const fileExt = 'jpg';
  it('get put file url', async () => {
    const response1 = await axios.put(
      `${API_BASE_URL}/files/signed-url`,
      null,
      {
        params: { fileName: fileId, fileExt },
      },
    );
    expect(response1.status).toEqual(200);
    expect(response1.data).toMatch(/https:\//);
    uploadUrl = response1.data as string;
  });

  it('upload file', async () => {
    const filePath = path.resolve(__dirname, '../assets/IMG20201004134009.jpg');
    const buffer = fs.readFileSync(filePath);
    const config: AxiosRequestConfig<Buffer> = {
      method: 'put',
      url: uploadUrl,
      headers: {
        'Content-Type': 'image/jpeg',
      },
      data: buffer,
    };
    const response2 = await axios(config);
    expect(response2.status).toEqual(200);
  });
  let downloadUrl: string;
  it('get download file url', async () => {
    const res = await axios.get(`${API_BASE_URL}/files/signed-url`, {
      params: { fileName: fileId, fileExt },
    });
    expect(res.status).toEqual(200);
    expect(res.data).toMatch(/https:\//);
    downloadUrl = res.data;
  });

  it('download file', async () => {
    const res = await axios.get(downloadUrl);
    expect(res.status).toEqual(200);
  });

  it('list file', async () => {
    const res = await axios.get(`${API_BASE_URL}/files/list`);
    expect(JSON.parse(res.data).length).toEqual(1);
    expect(res.status).toEqual(200);
  });

  it('delete file', async () => {
    const res = await axios.delete(`${API_BASE_URL}/files`, {
      params: { fileId, fileExt },
    });
    expect(res.status).toEqual(204);
  });

  it('list file', async () => {
    const res = await axios.get(`${API_BASE_URL}/files/list`);
    expect(JSON.parse(res.data).length).toEqual(0);
    expect(res.status).toEqual(200);
  });
});

然后写了一个Github工作流到运行“npm 运行 dev”和“npm 运行 test”。

name: Test

on:
  pull_request:
    branches:
      - master

jobs:
  local-test:
    name: local test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x]
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm i -g serverless@2.69.1
    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ap-northeast-1
    - run: npm run dev
    - run: npm run test

当我把它推到Github时。它停留在“npm 运行 dev”。 当“npm 运行 dev”完成后,我如何运行“npm 运行 test”?

我用jest-dev-server然后解决