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

feat: support parallel tests #287

Merged
merged 8 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
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 }}
1 change: 1 addition & 0 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
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);
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

// 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 + linux
// https://github.com/eggjs/bin/actions/runs/13308042479/job/37164115998
if (process.platform === 'linux' && process.version.startsWith('v22.')) {
return;
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
cwd = getFixtures('example-ts-cluster');
return coffee.fork(eggBin, [ 'cov' ], { cwd })
.debug()
Expand Down
Loading