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
Where do I put setupTests file #1265
Comments
Hey @iiison, jest will handle requiring the file for each test if you tell it where this file lives. See http://facebook.github.io/jest/docs/en/configuration.html and more specifically, http://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string. So in your package.json file, you'd have something like: {
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/path/to/your/file.js",
}
} and in that file you'd require enzyme and use the adapter: const Enzyme = require('enzyme');
const EnzymeAdapter = require('enzyme-adapter-react-15');
// Setup enzyme's react adapter
Enzyme.configure({ adapter: new EnzymeAdapter() }); |
If you use Create React App, you can create a |
What if your using https://github.com/wmonk/create-react-app-typescript ? I have this: but get: |
I believe typescript may be broken by default when importing from CJS - you may need to try |
@quantuminformation Were you able to figure this one out? Having the exact same issue currently. Using '*' in the import didn't work unfortunately. edit: I managed to resolve the error by typing the setup file as shown in this SO answer: |
Made a PR to clarify Jest setup #1396 |
Where would this go for mocha/karma? {
...
files: [
'node_modules/babel-polyfill/dist/polyfill.js',
'test/setupTests.js',
...
],
preprocessors: {
'test/setupTests.js': ['webpack'],
...
}
...
} |
@priyajeet Hi, were you able to solve this issue? |
@yinguangyao I ended up migrating my repo to use Jest instead. Ripped the bandaid off before it gets worse. You may also want to see this bug thread #1257 (comment) and this #1318 (comment) where you can get stuff working with Karma/Mocha, but it seems to force entire test suite to run with no clean path to run individual test files. |
@priyajeet hahaha, thanks. |
I also have same problem using react-script-ts |
It’s possible that CRA doesn’t work with a jest setup file without ejecting; if so, that’s a bug you should file. Since this is both answered here and a part of jest’s (or other frameworks’) documentation that’s unrelated to enzyme, i think this can be closed. |
Can anyone please tell me where to keep |
If you use create-react-app, maybe this solution could help: |
I had the same issue. |
@quantuminformation, @adepatie and everybody who uses Create React App + TypeScript - correct path, that you can use without ejecting from default environment, is Also this is what you put inside (as of now): import { configure } from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() }); |
For those who are not using |
@xploreraj There is small a instruction here http://airbnb.io/enzyme/docs/guides/mocha.html . This is a general configuration file we use for mocha , jsdom: require('babel-register')();
var jsdom = require('jsdom').jsdom;
var exposedProperties = ['window', 'navigator', 'document'];
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js'
};
documentRef = document; |
i can't get it to work in mocha either, I've tried --require setupTests.js in mocha which didn't work |
I've been working on setting up a starter create-react-app + typescript + mocha project for the past two weeks. Put your setup file at src/setupTests.ts if you are using the default jest. You can see that your @mcgodan Add test script to package.json: Pretty much you have to set everything for mocha that jest already does in create-react-app-ts. I don't like putting the enzyme.ts file at src/setupTests.ts because it's for tests and not really app source code. I might end up symlinking it just so jest still works. |
Several hours wasted, and below config worked; package.json has
Note: The jest > setupTestFrameworkScriptFile key in package.json must not be there, remove if it exists. \src\setupTests.js has
Hope that helps. |
@manoharreddyporeddy Thanks, it works!!!! Remove jest > setupTestFrameworkScriptFile key in package.json is very very important. |
@ShawnXiao105 glad to hear Update: Reactjs is definitely better than Angular 6, in developing application faster, writing highly maintainable code, super fast on run time. Used MERN tack instead of MEAN, used Visual Studio 2017. |
For those like me please pay attention to the file name |
I believe it must be "<rootDir>/src/setupTests.js", not just "src/setupTests.js" |
Jest looks for the setupTests.js file when you start the test runner. If you have a terminal open somewhere with the test runner in watch mode, you'll get no love. You don't need to add the following to your package.json unless you've ejected |
Is there anyway to "require" the setupTests file? I'd love to just define this once. |
That’s because it’s incorrect to use |
Just as a precaution, you should restart tests if you add |
Scrolled down to comment it , already here :P |
The solution provided by @kirrosh worked for me, for the exception of the import syntax for the Adapter. I used the following with
|
I've tried every solution I could. Making a What worked is placing the jest configuration inside package.json {
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
}
} But of course you first have to install |
@kikiondo117 I like the font you are using. What's the name of this font(if you don't mind 🙏)? |
Sure, the name is fira code, it's free :D, you can download here -> https://github.com/tonsky/FiraCode |
The answer provided by @kmcq works and fix this issue how ever in favor of the deprecation of "setupTestFrameworkScriptFile" you have to use "setupFilesAfterEnv" which receives an Array of jest setup files: // package.json
{
"jest": {
"setupFilesAfterEnv": "<rootDir>/path/to/your/file.js",
}
} Or if you have a jest.config.js file: // jest.config.js
module.exports = {
"setupFilesAfterEnv": [
"<rootDir>/path/to/your/file.js"
]
} If your React application is generated by CRA then make sure you add |
Hello, I am following this guid to setup tests in my project(with React-15). I was going through ES6:(setup) and I couldn't get where should I put this test file. and how and where to import the testSetup file. I have tried to put it at root level and in the app directory(where all containers and redux logic is written) but none worked. I am using jest with enzyme. and while running tests I am getting this error:
Or do I need to configure adapter every time?
The text was updated successfully, but these errors were encountered: