Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 10:45
    juergba synchronize #4165
  • 10:45

    juergba on errSuite

    fix when errSuite is undefined new helper invokeNode additional test (compare)

  • 10:23
    juergba unlabeled #4011
  • 10:22
    juergba edited #4019
  • 10:18
    juergba unlabeled #3917
  • 10:16
    juergba labeled #2906
  • 10:14

    juergba on recover-uncaught

    (compare)

  • 09:45

    juergba on master

    uncaughtException: fix recovery… (compare)

  • 09:45
    juergba closed #4150
  • 09:45
    juergba closed #2906
  • 08:57
    juergba synchronize #4150
  • 08:57

    juergba on recover-uncaught

    remove recovery out of uncaught adapt existing tests additional test and 1 more (compare)

  • 06:38
    juergba demilestoned #4155
  • 06:37
    juergba edited #4165
  • 06:34
    juergba closed #4162
  • 06:34
    juergba labeled #4162
  • 06:34
    juergba unlabeled #4162
  • Jan 22 15:28
    juergba labeled #4166
  • Jan 22 15:28
    juergba unlabeled #4166
  • Jan 22 15:27
    PeterNgTr closed #4166
Jasmin Mustafic
@J-Mustafic
Hi All. Is there a nice way to "reimport" files with mocha? The thing is, I have a private variable that gets changed. And one of my test down the road fails because of this variable, and that variable has changed in previous test cases. Is it possible to somehow "reimport" files, so that I get their default state?
Craig Taub
@craigtaub
@vinaynalla1401 what is the issue? we have a working example of mocha+typescript here (https://github.com/mochajs/mocha-examples/tree/master/packages/typescript) perhaps that can help direct you.
@J-Mustafic if you arent using ESM (new modules) then you could delete the file from the require.cache object. So importing again will import it fresh.
Jasmin Mustafic
@J-Mustafic
@craigtaub Thanks. I will look into it.
Jasmin Mustafic
@J-Mustafic
@craigtaub This seems to work delete require.cache[require.resolve('dependency')];, even with ESM. But mongoose is giving me a challenge now, because I am trying to to overwrite the defined model. A step forward. :)
kakins
@kakins
Is there a way to tell mocha to ignore a .mocharc file if one exists? I have a scenario where I want to use it when debugging locally, but ignore it when deployed
kakins
@kakins
And how do you override the spec array of the .mocharc file via the command line?
Juerg B.
@juergba
@kakins with --no-config option Mocha should not search for a config file.
array options are accumulated over CLI/config files in Mocha, so AFAIK you can't override spec via CLI.
kakins
@kakins
@juergba thanks!
Benjamin Piouffle
@Betree

Hello ! I'm currently working with custom assertions. The doc is great, but I can't find any info in there or anywhere on the Internet about custom async validations.

What I'm trying to achieve is this:

// Make sure user is soft deleted
expect(user).to.be.softDeleted;

My initial implementation, using chai-as-promised, is this:

/**
 * Custom chai assertion to ensure that sequelize object is soft deleted
 */
chai.util.addProperty(chai.Assertion.prototype, 'softDeleted', function() {
  // Make sure we are working with a sequelize model
  new chai.Assertion(this._obj).to.be.instanceOf(Sequelize.Model, 'softDeleted');

  // Check if `deletedAt` is set after reloading
  const promiseGetEntityDeletedAt = this._obj
    .reload({ paranoid: false })
    .then(updatedEntity => updatedEntity.dataValues.deletedAt);

  if (this.__flags.negate) {
    new chai.Assertion(promiseGetEntityDeletedAt, 'Entity should not be deleted in DB').to.eventually.not.exist;
  } else {
    new chai.Assertion(promiseGetEntityDeletedAt, 'Entity should be deleted in DB').to.eventually.exist;
  }
});

An assertion error is properly thrown, but the test doesn't fail. Any idea on what could be the problem?

Juerg B.
@juergba
@Betree Where do you tell Mocha it is an async test?
Shouldn't it be: return expect(user).to.be.softDeleted; ?
Benjamin Piouffle
@Betree
My test runs inside an async function, it actually looks more like that:
it('deletes all data', async () => {
  const user = await createUser();
  await user.destroy();
  expect(user).to.be.softDeleted;
});
Peter Müller
@Munter
@Betree maske your assertion async and return the result of the assertion in mocha
Benjamin Piouffle
@Betree

Same result if I do return expect(user).to.be.softDeleted;. To make the assertion async, I did that:

chai.util.addProperty(chai.Assertion.prototype, 'softDeleted', async function() {

Is it enough?

Peter Müller
@Munter
@Betree You'll need to also await your promise resolution somewhere in your assertion. Also, don't you need to actually call to.be.softDeleted()?
Benjamin Piouffle
@Betree

Also, don't you need to actually call to.be.softDeleted()?

I don't think to because it's a property

It works by doing this:

  if (this.__flags.negate) {
    await new chai.Assertion(promiseGetEntityDeletedAt, 'Entity should not be deleted in DB').to.eventually.not.exist;
  } else {
    await new chai.Assertion(promiseGetEntityDeletedAt, 'Entity should be deleted in DB').to.eventually.exist;
  }

Thanks @Munter !

Marco Faustinelli
@Muzietto
Hello. Got a question: does the "Running Mocha in the browser" feature allow to expand the test description and see the original ES6 code, rather than the transpiled ES5 code?
image.png
This is what i DO NOT wish to see.
It's code I wrote a few years ago.
I wonder whether the situation has evolved.
Devipv
@devipv02
Hi, I would like to know if there is a solution to my use case
I'm using mocha in package.json [ test: mocha './mochaTest.js' --colors --reporter mocha-junit-reporter" ]. When run it returns the test report in .xml format
mochaTest.js has one test which calls a node_module function [say runTests('/mainTest.js')]
runTests() function initialises it's own mocha object and runs the tests in mainTest.js. It returns the test report in dot format in console.
I'm looking for a solution which returns the test report of both mochaTest.js and mainTest.js in single xml [Is this even possible as runTest() exists in node_modules and a mocha object is initialised again in it. I can't add a reporter to runTest()]
Zirak
@Zirak
Hey :) I've recently opened #4133 , it's a tiny improvement to create a better exception in some edge case. Would love to hear feedback. Thanks!
suds0602
@suds0602
Hi, How can we get the name of the test file being executed in a variable (in before function)?
Lars Olesen
@lsolesen
I am going to refactor some code. First I want to put it under a test harness, but I am not sure how to start the test. It is built on an anonymous function. I am just asking about help to get the first test started, and then I should be allright from there.
However, that gives me this right now.
> mocha src/test/*.*

/home/ec2-user/environment/ifire/node_modules/yargs/yargs.js:1163
      else throw err
           ^

ReferenceError: document is not defined
Peter Müller
@Munter
@lsolesen looks like you are testing browser code. Then you need to run the test in a browser
People usually use karma as a runner to orchestrate the browser stuff, which then find mocha in the browser. But you can also write your tests as a web page and run them directly
Uzlopak
@Uzlopak
Hi i run mocha at my work pc and it runs, but when i work on my pc at home it just runs out with a listOnTimeout (internal/timers.js:531:17)
is there any debug mode in mocha?
Craig Taub
@craigtaub
@Uzlopak try DEBUG=mocha:* mocha
bion howard
@bionicles
How do you run CodeceptJS tests inside Cypress? They both depend on Mocha and I'm curious if they might work together using this framework with event listeners or something
Then we could write, maintain, share, this syntax of e2e tests:
Scenario('test app', (I) => {
  I.amOnPage('/chat');
  I.fillField('name', 'davert');
  I.click('Sign In');
  I.see('Hello, davert');
  session('john', () => {
    // another session started
    I.amOnPage('/chat');
    I.fillField('name', 'john');
    I.click('Sign In');
    I.see('Hello, john');
  });
  // switching back to default session
  I.fillField('message', 'Hi, john');
  // there is a message from current user
  I.see('me: Hi, john', '.messages');
  session('john', () => {
    // let's check if john received it
    I.see('davert: Hi, john', '.messages');
  });
});
run it automatically with this github action: https://github.com/marketplace/actions/cypress-io
(in matrix testing mode across many different configurations)
then we could inspect / replay / debug those tests in Cypress GUI https://www.cypress.io/
just curious because testing is sometimes a pain and it would potentially help many developers save time if we make this integration easier
Jared Evans
@JaredCE
Hey, i'm not quite sure how to turn my mocha.opts file to a .mocharc.js file
my mocha.opts file looked like this:
test
--require ./test/common.js
--harmony
--recursive
--bail
Jared Evans
@JaredCE
i'm unsure about the test parameter at the top of the file
Juerg B.
@juergba
@JaredCE examples,
for your test files/directories/globs use spec:
Jared Evans
@JaredCE
@juergba i'm not 100% sure what you mean
Shahar Evron
@shevron
hello, I'm trying to run Mocha with tests written as ES6 modules. I'm getting Error [ERR_REQUIRE_ESM]: Must use import to load ES Module. The Internet tells me using -r esm should solve this, but it doesn't. Is there something obvious I'm missing?
This is Mocha 7.0.0, Node 13.6.0
Juerg B.
@juergba
@shevon have you installed the esm package?
The second option is: install our experimental release v7.0.0-esm1 which supports Node's native ESM and should load your ESM test files correctly, without requiring the esm package.
Shahar Evron
@shevron
@juergba yes I have esm installed
I will try the experimental release
@juergba v7.0.0-esm1 works!