Comments (10)
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.
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.
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.
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.
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.
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.
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.
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.
Hi @MrMicrowaveOven,
Please, feel free to submit a PR!
I'm sorry I missed the issue that @reduxdj reported.
from jest-react-hooks-shallow.
Thanks a lot for your PR. Closed!
from jest-react-hooks-shallow.
Related Issues (20)
- script random number in range
- enableHooks not sufficient if jest config contains "resetMocks: true" HOT 11
- beforeEach is not defined HOT 1
- withHooks and withoutHooks do not support async tests / promises HOT 12
- Clash with mocking React to work around useRef problems HOT 2
- Acknowledgement HOT 1
- How to run useEffect cleanup - Help HOT 1
- useEffect run cleanup function in wrong trigger HOT 1
- Consider back documenting to enzyme's open issue HOT 1
- Added plugin to package.json HOT 1
- Doesn't seem to play well with ts-jest HOT 1
- Incompatible with Jest 26+ HOT 7
- Possible way to get around mount issues
- Npm7 & React 17 peer dependency error HOT 1
- dontMockByDefault not working? HOT 2
- Not working correctly with beforeAll
- work with jest 27? HOT 2
- Support for storybook HOT 1
- Support for React 18 ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-react-hooks-shallow.