GithubHelp home page GithubHelp logo

Comments (10)

mikeborozdin avatar mikeborozdin commented on August 16, 2024 6

@pautenaPaack,

Thanks a lot for providing your jest.config.js. Could you possibly try using the setupFilesAfterEnv option for pointing to your setup.ts? That should fix the issue. beforeEach() is not available for files specified by setupTests. But is available for the ones used for setupFilesAfterEnv.

@rheilgm ,

Do you also point to setupTests.js|ts as setupTests in your Jest config?

from jest-react-hooks-shallow.

mikeborozdin avatar mikeborozdin commented on August 16, 2024 1

Hi @pautenaPaack ,

Thank for a detailed report! Support for React Native is important - I'll investigate what's wrong in the next couple of days, and hopefully will issue a fix!

from jest-react-hooks-shallow.

mikeborozdin avatar mikeborozdin commented on August 16, 2024

Hi @rheilgm ,

I haven't tried it myself, but it should.

I'll have a look at it.

In the meanwhile, can we confirm that you're using Jest? Because beforeEach should be provided by it.

from jest-react-hooks-shallow.

pautenaPaack avatar pautenaPaack commented on August 16, 2024

Same problem here.

  • react: 16.11.0
  • react-native: 0.62.2
  • jest-react-hooks-shallow: 1.4.1
  • Jest: 26.0.1

I configure jest-react-hooks-shallow in a setup file, after enzyme:

setup.ts

import Adapter from 'enzyme-adapter-react-16';
import enableHooks from 'jest-react-hooks-shallow';

[...]
// Enzyme config
Enzyme.configure({ adapter: new Adapter() });

// pass an instance of jest to `enableHooks()`
enableHooks(jest);
[...]

And my jest.config.js

jest.config.js

module.exports = {
  preset: 'react-native',
  setupFiles: [
    '<rootDir>/node_modules/react-native/jest/setup.js', 
    '<rootDir>/test/setup.ts'
  ],
...
}

If I downgrade the versions to jest 24.9.0 and jest-react-hooks-shallow 1.2.0 the bug disappears.

from jest-react-hooks-shallow.

pautenaPaack avatar pautenaPaack commented on August 16, 2024

Thanks @mikeborozdin, if I put the line

enableHooks(jest);

inside a second setup file and call it in setupFilesAfterEnv I can run all the tests

from jest-react-hooks-shallow.

reduxdj avatar reduxdj commented on August 16, 2024

Following this example, outlined above... I get this error:

const enableHooks = require('jest-react-hooks-shallow');
console.log(enableHooks);
enableHooks(jest);
TypeError: enableHooks is not a function

from jest-react-hooks-shallow.

reduxdj avatar reduxdj commented on August 16, 2024

Same issue as above using: jest v25.5.4

I am trying to provide a configuration file for my test runner, so i can load my data out out of a directory (my mocks) when my services go to fetch.... This has worked for mocha tests but not for jest. Why would creating a setup file blow away the beforeEach in my my setup?

For example:

all-tests.js

if (typeof require.context === 'undefined') {
  /* eslint-disable */
  const fs = require('fs');
  const path = require('path');

  /* eslint-enable */

  require.context = (base = '.', scanSubDirectories = false, regularExpression = /\.js$/) => {
    const files = {};

    function readDirectory(directory) {
      fs.readdirSync(directory).forEach(file => {
        const fullPath = path.resolve(directory, file);
        if (fs.statSync(fullPath).isDirectory()) {
          if (scanSubDirectories) readDirectory(fullPath);
          return;
        }
        if (!regularExpression.test(fullPath)) return;
        files[fullPath] = true;
        // console.log(files);
      });
    }
    readDirectory(path.resolve(__dirname, base));
    function Module(file) {
      return require(file); //eslint-disable-line
    }
    Module.keys = () => Object.keys(files);
    return Module;
  };
}

const context = require.context('./src', true, /\.jsx?$/);
context.keys().forEach(context);

module.exports = context;

My configuration for jest:

{
  "setupFiles": ["<rootDir>/test/unit/all-tests.js"],
  "setupFilesAfterEnv": ["<rootDir>/test/unit/set-hooks.js"],
  "modulePaths": ["/src"],
  "transform": {
    ".+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
    ".+\\.json": "<rootDir>/jest_config/jsonTransform.js"
  },
  "testMatch": ["<rootDir>/test/**/*.[jt]s?(x)", "tests/?(*.).[jt]s?(x)"],
  "moduleFileExtensions": ["js", "jsx"],
  "moduleNameMapper": {
    "^.*[.](jpg|JPG|gif|GIF|png|PNG|less|LESS|css|CSS)$": "EmptyModule"
  },
  "testPathIgnorePatterns": [
    ".json",
    "<rootDir>/test/init.js",
    "<rootDir>/test/all-tests.js",
    "<rootDir>/test/src/student/asmtHistoryDataTest.js",
    "e2e",
    "set-hooks"
  ],

  "transformIgnorePatterns": ["/node_modules/", "jest_preprocessor.js", "init.js", "dist/*", "stories/*", "bundle-test.js", "all-tests.js", "set-hooks.js"],
  "unmockedModulePathPatterns": ["src/node_modules/react", "src/node_modules/react-dom", "src/EmptyModule.js"],
  "reporters": ["default", "jest-stare"],
  "globals": {
    "BASE_URL": "https://cmtdcs-api.cmtdcs-nonprod.collegeboard.org",
    "BASE_PCI_URL": "https://cmtdcs-api-pci.cmtdcs-nonprod.collegeboard.org",
    "ENV": "dev",
    "LOCAL": "local",
    "setupTestFrameworkScriptFile": "test/unit/init.js"
  }
}

from jest-react-hooks-shallow.

MrMicrowaveOven avatar MrMicrowaveOven commented on August 16, 2024

I ran into this same issue, and i'm really glad I found this thread via google. It wasn't under "issues" since it's closed. Is it alright if I add this to a Troubleshooting section of the ReadMe?

from jest-react-hooks-shallow.

mikeborozdin avatar mikeborozdin commented on August 16, 2024

Hi @MrMicrowaveOven,

Please, feel free to submit a PR!

I'm sorry I missed the issue that @reduxdj reported.

from jest-react-hooks-shallow.

mikeborozdin avatar mikeborozdin commented on August 16, 2024

@MrMicrowaveOven ,

Thanks a lot for your PR. Closed!

from jest-react-hooks-shallow.

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.