Code Monkey home page Code Monkey logo

Comments (9)

3cp avatar 3cp commented on August 21, 2024

It seems easy to add an option to pta to turn the Promise.all to a chained await.

  await Promise.all(
    files.map((file) => {
      const filePath = resolve(process.cwd(), file);
      return import(pathToFileURL(filePath));
    })
  );
  for (const file of files) {
    const filePath = resolve(process.cwd(), file);
    await import(pathToFileURL(filePath));
  }

Update: I realised this could not work because all tests in every single file are asynchronously executed.

from zora.

3cp avatar 3cp commented on August 21, 2024

The other option is to add some env support in zora itself, something like ZORA_SERIAL=true.

That's probably a better option than changing pta. Because the env can make sure all test() calls are in serial. The pta change only ensures serialisation between files.

from zora.

lorenzofox3 avatar lorenzofox3 commented on August 21, 2024

HI,

The other option is to add some env support in zora itself, something like ZORA_SERIAL=true.

That's probably a better option than changing pta. Because the env can make sure all test() calls are in serial. The pta change only ensures serialisation between files.

That would be quite challenging with the current design and probably not something that I want. And by the way, if you run all the tests serially it won't make things faster than AvA/tape.

I am not sure to see why you'd need to run the tests serially though? are you restoring the fs at some point ? could you share some code please ?

If you need to setup the mock before you run the test you can have a setup file:

const mock = require('mock-fs');

mock({
  'path/to/fake/dir': {
    'some-file.txt': 'file content here',
    'empty-dir': {/** empty directory */}
  },
  'path/to/some.png': Buffer.from([8, 6, 7, 5, 3, 0, 9]),
  'some/other/path': {/** another empty directory */}
});

and then run node with a require hook: node -r test/setup.js node_modules/.bin/pta.

If you really want to run multiple files serially in isolated environments you can spawn multiple processes with either a basic test runner or a simple bash command (with some caveats on the reporting I assume)

for f in test/*.spec.js; do pta $f; done

Or write your test files in some way they accept the assert object and control yourself how the files get loaded within an entry point:

// some.spec.js

export default (t) => {
    t.ok(true)
}

and entrypoint (or you can build a basic test-runner at this point: pta is only few lines of code if you need inspiration)

// test/index.js
import {test} from 'zora'

// this is a dummy test whose only purpose is to load the test suites and make them run serially
test(`my serial test`, async t => {
    for (const f of files) { // files coming from shell args, config, env var (up to you) 
        const { deafult: suite } = await import(f); // or require depending on your project
        await suite(t); // the await make sure the test suites run serially
    }
})

Hope this gives you some idea to proceed. Again, the easier would be to share with me a reproduction of your current test setup

from zora.

3cp avatar 3cp commented on August 21, 2024

Every test (test()) has it's own mocked set of files, uses mockfs.restore() to clean up at end of every test. It's not a global mock for all tests.

Since mock-fs stubs fs calls, it would not work well when two different mock in two tests (from two test.js files in my case) running in parallel by zora.

Within one test file, I can use await to queue the tests, but currently no solution across files.

from zora.

3cp avatar 3cp commented on August 21, 2024

I found out I can use top level await (no commonjs), plus the suggested change in pta to queue up all tests.

pta is using import() which works well with both commonjs and esm test files.

from zora.

3cp avatar 3cp commented on August 21, 2024

I think the suggested change in pta could simply replace the current Promise.all without introducing an option.

for (const file of files) {
    const filePath = resolve(process.cwd(), file);
    await import(pathToFileURL(filePath));
}

It would not cause noticeable performance lost for test files not using any top level await. I tested with another project (no use of mock-fs), there is no noticeable time change (all around 670ms to 680ms for full unit tests in 34 files).

With the change, the disk IO is definitely queued. But I doubt Nodejs parses all read-in files from Promise.all with more than one process/thread.

With this change, global queued tests across files are possible.

from zora.

lorenzofox3 avatar lorenzofox3 commented on August 21, 2024

oh you mean if you write your test file as so:

// test.js
import {test} from 'zora';

await test('foo', fn); // with top level await

and we change pta with the suggested changes you gain the ability to run your tests in serie ?

Sounds good 👍

from zora.

Alexander-Taran avatar Alexander-Taran commented on August 21, 2024

❤️

from zora.

3cp avatar 3cp commented on August 21, 2024

Latest pta works really well! Has to use --module-loader es to load my tests. That also means I cannot mix cjs and es test files in pta, that's understandable.

Wrong module-loader only messes up the reporter, because of different zora instances (one loaded by pta to hold and report, another one in real tests).

from zora.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.