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

Allow being used inside Jest beforeAll #72

Closed
kopax-polyconseil opened this issue Jul 19, 2022 · 7 comments · Fixed by #76
Closed

Allow being used inside Jest beforeAll #72

kopax-polyconseil opened this issue Jul 19, 2022 · 7 comments · Fixed by #76
Labels
good first issue Good for newcomers, please hop on! 🙌

Comments

@kopax-polyconseil
Copy link
Contributor

kopax-polyconseil commented Jul 19, 2022

Hello, we are trying to ugprade jest to latest and encounter issue in our upgrade branch with consol-fail-test: pass-culture/pass-culture-app-native#3271

example:

  ● Test suite failed to run

    Cannot add a hook after tests have started running. Hooks must be defined synchronously.

      30 |
      31 | global.beforeAll((done) => {
    > 32 |   consoleFailTestModule.cft({
         |                         ^
      33 |     testFramework: 'jest',
      34 |     spyLibrary: 'jest',
      35 |     console: allowConsoleRuntimeConfig,

      at eventHandler (node_modules/jest-circus/build/eventHandler.js:113:11)
      at Object.after (node_modules/console-fail-test/src/environments/jest.js:10:13)
      at Object.<anonymous>.exports.cft (node_modules/console-fail-test/src/cft.js:20:21)
      at Object.cft (src/tests/setupTests.js:32:25)

It seems it does not support the latest jest version, is there a workaroud to keep using console-fail-test or a way we can help?

@kopax-polyconseil
Copy link
Contributor Author

solve by moving consoleFailTestModule.cft({ outside the beforeAll.

@JoshuaKGoldberg
Copy link
Owner

JoshuaKGoldberg commented Jul 19, 2022

Hey thanks for posting this - cft should emit a better error in this case. The error should indicate what is being done wrong. Marking as accepting PRs.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Can't upgrade jest from 26 to 28 because of console-fail-test failing after upgrade Improve error when useful inside Jest beforeAll Jul 19, 2022
@JoshuaKGoldberg JoshuaKGoldberg added good first issue Good for newcomers, please hop on! 🙌 accepting prs labels Jul 19, 2022
@kopax-polyconseil
Copy link
Contributor Author

Hi, after investigating, this change is quite annoying for us.

This is what we expect from console-fail-test:

  1. by default, fail any test if console.* methods are called
  2. per test, hability to re-allow console.* methods calls

Our implementation for (1) looked like this in our setupTests.js:

const allowConsoleDefaultConfig = {
  debug: false,
  error: false,
  log: false,
  warn: false,
}

let allowConsoleRuntimeConfig = Object.assign({}, allowConsoleDefaultConfig)
global.allowConsole = function (config = allowConsoleDefaultConfig) {
  allowConsoleRuntimeConfig = Object.assign({}, allowConsoleDefaultConfig, config)
}

global.beforeAll(() => {
  consoleFailTestModule.cft({
    testFramework: 'jest',
    spyLibrary: 'jest',
    console: allowConsoleRuntimeConfig,
  })
})

Our implementation for (2) is using the global.allowConsole method we added, and we are able to change the "prepared" the option for cfg before it is called by beforeAll by adding in the global scope of our unit test this line:

allowConsole({ error: true })

Since it is not possible after jest 27 upgrade to keep the consoleFailTestModule.cft call inside a beforeAll, we lake a feature that would allow us (2).

If you have any clue how we can fix this, that would be much appreciated as our global suite of tests depend on this.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Improve error when useful inside Jest beforeAll Improve error when used inside Jest beforeAll Jul 23, 2022
@kopax-polyconseil
Copy link
Contributor Author

Hello @JoshuaKGoldberg , this is blocking the jest 26 to 27 upgrade, do you have any indication that could help us move forward?
Thanks in advance!

@JoshuaKGoldberg JoshuaKGoldberg changed the title Improve error when used inside Jest beforeAll Allow being used inside Jest beforeAll Jul 26, 2022
@JoshuaKGoldberg
Copy link
Owner

I took a deeper look at how this gets run in pass-culture/pass-culture-app-native#3306, as mentioned in #64 (comment). If you're interested you should be able to modify environments/jest.ts -- see Development.md for some architecture docs.

Right now, the Jest environment code calls to the global Jest afterEach function inside its after hook:

return {
  after(callback: (afterHooks: TestAfterHooks) => void) {
    afterEach(() => {
      callback({
        reportComplaint({ error }) {
          throw error;
        },
      });
    });
  },
  // ...
};

That after hook is what gets called inside consoleFailTestModule.cft({ ... }). So, if Jest is no longer allowing afterEach to be called where you're calling it, the environment will have to call afterEach earlier.

I'm betting you could modify the code to something like:

const afterEachCallback = [() => {}];

afterEach(() => {
  afterEachCallback[0]();
});

return {
  after(callback: (afterHooks: TestAfterHooks) => void) {
    afterEachCallback[0] = () => callback({
      reportComplaint({ error }) {
        throw error;
      }
    });
  },
  // ...
};

That's just a sketch. I don't have the time to do this myself as your usage pattern isn't one I've come across or needed to use in any of my projects. But this issue is marked as accepting prs if you'd like to contribute it yourself or sponsor me to do it for you. Cheers!

@kopax-polyconseil
Copy link
Contributor Author

Thank you for the detailed explanation. I will take a look tomorrow and let you know for the PR fix. I appreciate that you still try to help during your vacation. One thing, I didn't got why you initialized an array for just a callback function, I'll try without first.

@kopax-polyconseil
Copy link
Contributor Author

kopax-polyconseil commented Jul 27, 2022

I just opened a PR as it seems to work well : #76

Meanwhile waiting for the release, we'll use a patch-package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers, please hop on! 🙌
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants