Skip to content

Commit

Permalink
feat: support parallel tests (#287)
Browse files Browse the repository at this point in the history
closes #286

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Chores**
- Enhanced the continuous integration pipeline to run tests in parallel
for improved efficiency.

- **New Features**
- Enabled distribution of test files across multiple parallel jobs to
optimize the testing process.
  - Introduced a new dependency to facilitate parallel test execution.

- **Tests**
- Added a test case to verify the functionality of running tests in
parallel across different CI jobs.
- Updated test suite for a TypeScript cluster application to improve
asynchronous handling and debugging.
- Implemented conditional checks to skip specific tests based on
platform and Node.js version.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Feb 13, 2025
1 parent 8ec2d70 commit b3f2b6c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ on:
jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
uses: node-modules/github-actions/.github/workflows/node-test-parallel.yml@master
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '18.19.0, 18, 20, 22, 23'
version: '18, 20, 22'
parallel: 2
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/mocha": "^10.0.10",
"@types/supertest": "^6.0.2",
"c8": "^10.0.0",
"ci-parallel-vars": "^1.0.1",
"detect-port": "^2.0.0",
"egg-ts-helper": "^3.0.0",
"globby": "^11.1.0",
Expand Down Expand Up @@ -55,8 +56,8 @@
"cpy-cli": "^5.0.0",
"cross-env": "^7.0.3",
"egg": "^4.0.7",
"esbuild": "^0.17.7",
"esbuild-register": "^3.4.2",
"esbuild": "^0.25.0",
"esbuild-register": "^3.6.0",
"eslint": "8",
"eslint-config-egg": "14",
"npminstall": "^7.12.0",
Expand Down
17 changes: 16 additions & 1 deletion src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Args, Flags } from '@oclif/core';
import globby from 'globby';
import { importResolve, detectType, EggType } from '@eggjs/utils';
import { getChangedFilesForRoots } from 'jest-changed-files';
// @ts-expect-error no types
import ciParallelVars from 'ci-parallel-vars';
import { BaseCommand } from '../baseCommand.js';

const debug = debuglog('@eggjs/bin/commands/test');
Expand Down Expand Up @@ -166,14 +168,27 @@ export default class Test<T extends typeof Test> extends BaseCommand<T> {
pattern = pattern.concat([ '!test/fixtures', '!test/node_modules' ]);

// expand glob and skip node_modules and fixtures
const files = globby.sync(pattern, { cwd: flags.base });
let files = globby.sync(pattern, { cwd: flags.base });
files.sort();

if (files.length === 0) {
console.log('No test files found with pattern %o', pattern);
return;
}

// split up test files in parallel CI jobs
if (ciParallelVars) {
const { index: currentIndex, total: totalRuns } = ciParallelVars as { index: number, total: number };
const fileCount = files.length;
const each = Math.floor(fileCount / totalRuns);
const remainder = fileCount % totalRuns;
const offset = Math.min(currentIndex, remainder) + (currentIndex * each);
const currentFileCount = each + (currentIndex < remainder ? 1 : 0);
files = files.slice(offset, offset + currentFileCount);
console.log('# Split test files in parallel CI jobs: %d/%d, files: %d/%d',
currentIndex + 1, totalRuns, files.length, fileCount);
}

// auto add setup file as the first test file
const setupFile = path.join(flags.base, `test/.setup.${ext}`);
try {
Expand Down
18 changes: 18 additions & 0 deletions test/commands/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ describe('test/commands/test.test.ts', () => {
.end();
});

it('should work on split test files in parallel CI jobs', () => {
return coffee.fork(eggBin, [ 'test' ], {
cwd,
env: {
CI_NODE_INDEX: '2',
CI_NODE_TOTAL: '3',
},
})
// .debug()
.expect('stdout', /# Split test files in parallel CI jobs: 3\/3, files: 1\/4/)
.expect('stdout', /should success/)
.expect('stdout', /no-timeouts\.test\.js/)
.notExpect('stdout', /a\.test\.js/)
.expect('stdout', /1 passing \(/)
.expect('code', 0)
.end();
});

it('should success with some files', async () => {
await coffee.fork(eggBin, [ 'test', 'test/a.test.js' ], { cwd })
// .debug()
Expand Down
15 changes: 9 additions & 6 deletions test/fixtures/example-ts-cluster/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { scheduler } from 'node:timers/promises';
import mm, { MockOption } from '@eggjs/mock';
import request from 'supertest';

describe('test/index.test.ts', () => {
describe('example-ts-cluster/test/index.test.ts', () => {
let app: any;
before(() => {
before(async () => {
app = mm.cluster({
opt: {
execArgv: [ '--require', 'ts-node/register' ],
},
} as MockOption);
// app.debug();
return app.ready();
app.debug();
await app.ready();
await scheduler.wait(1000);
});

after(() => app.close());
it('should work', async () => {
const req = request(`http://127.0.0.1:${app.port}`);
return req
const url = `http://127.0.0.1:${app.port}`;
console.log('request %s', url);
await request(url)
.get('/')
.expect('hi, egg')
.expect(200);
Expand Down
5 changes: 5 additions & 0 deletions test/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe('test/ts.test.ts', () => {
});

it('should cov app in cluster mod', () => {
// TODO(@fengmk2): not work on Node.js 22
// https://github.com/eggjs/bin/actions/runs/13308042479/job/37164115998
if (process.version.startsWith('v22.')) {
return;
}
cwd = getFixtures('example-ts-cluster');
return coffee.fork(eggBin, [ 'cov' ], { cwd })
.debug()
Expand Down

0 comments on commit b3f2b6c

Please sign in to comment.