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

any showcase for node:test concurrency options? #3837

Open
atian25 opened this issue Apr 22, 2022 · 5 comments
Open

any showcase for node:test concurrency options? #3837

atian25 opened this issue Apr 22, 2022 · 5 comments
Labels

Comments

@atian25
Copy link

atian25 commented Apr 22, 2022

Details

as https://nodejs.org/dist/latest-v18.x/docs/api/test.html#testname-options-fn docs says, subtests could run concurrency.

when try as:

test('multi level test', { concurrency: 2 }, async (t) => {
  await t.test('subtest 1', async () => {
    console.log('1');
    await setTimeout(2000);
    console.log('11');
  });

  await t.test('subtest 2', async () => {
    console.log('2');
    await setTimeout(1000);
    console.log('22');
  });
});

itdon't work, since each subtests is awaited.


but if write test like below:

test('multi level test', { concurrency: 2 }, async (t) => {
  t.test('subtest 1', async () => {
    console.log('1');
    await setTimeout(2000);
    console.log('11');
  });

  t.test('subtest 2', async () => {
    console.log('2');
    await setTimeout(1000);
    console.log('22');
  });
});

got error:

not ok 1 - subtest 1
      ---
      duration_ms: 0.001728458
      failureType: 'cancelledByParent'
      error: "'test did not finish before its parent and was cancelled'"
      code: ERR_TEST_FAILURE
      ...

Node.js version

18.0.0

Example code

No response

Operating system

macOS

Scope

test

Module and version

Not applicable.

@atian25
Copy link
Author

atian25 commented Jul 19, 2022

@targos

@MoLow
Copy link
Member

MoLow commented Jul 20, 2022

Hi.
concurrency can be used with Promise.all:

test('multi level test', { concurrency: 2 }, async (t) => {
	await Promise.all([
		t.test('subtest 1', async () => {
	      console.log('1');
	      await setTimeout(2000);
	      console.log('11');
	    });
	  
	    t.test('subtest 2', async () => {
	      console.log('2');
	      await setTimeout(1000);
	      console.log('22');
	    });
	]);
});

or with describe:

descrie('multi level test', { concurrency: 2 }, async () => {
	it('subtest 1', async () => {
      console.log('1');
      await setTimeout(2000);
      console.log('11');
    });
  
    it('subtest 2', async () => {
      console.log('2');
      await setTimeout(1000);
      console.log('22');
    });
});

@bitliner
Copy link

bitliner commented Nov 16, 2023

I cannot get it work, with the following code:

const test = require('node:test')
const assert = require('node:assert/strict')

const wait = (ms) => new Promise((resolve) => setTimeout(() => resolve(), ms))

const input = [{
  expected: 2,
  actual: 2
}, {
  expected: 'ola',
  actual: 'ciao'
}]

input.forEach((el, index) => {
  test('should work', { concurrency: true }, async (t) => {
    console.log(`start-${index}`)
    await wait(3000)

    assert.strictEqual(el.actual, el.expected)
    console.log(`end-${index}`)
  })
})

node version: v20.9.0

I'm running it with

const { junit } = require('node:test/reporters')
const { run } = require('node:test')
const path = require('node:path')

run({ concurrency: true, files: [path.resolve(__dirname, './test-suite-1.js')] })
  .compose(junit)
  .pipe(process.stdout)

@atian25
Copy link
Author

atian25 commented Nov 17, 2023

Hi. concurrency can be used with Promise.all:

test('multi level test', { concurrency: 2 }, async (t) => {
	await Promise.all([
		t.test('subtest 1', async () => {
	      console.log('1');
	      await setTimeout(2000);
	      console.log('11');
	    });
	  
	    t.test('subtest 2', async () => {
	      console.log('2');
	      await setTimeout(1000);
	      console.log('22');
	    });
	]);
});

or with describe:

descrie('multi level test', { concurrency: 2 }, async () => {
	it('subtest 1', async () => {
      console.log('1');
      await setTimeout(2000);
      console.log('11');
    });
  
    it('subtest 2', async () => {
      console.log('2');
      await setTimeout(1000);
      console.log('22');
    });
});

with the first showcase, { concurrency: 2 } seems to be useless due to it can't affect Promise.all()

Copy link

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.

@github-actions github-actions bot added the stale label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants